diff --git a/barretenberg/cpp/src/barretenberg/serialize/cbind.hpp b/barretenberg/cpp/src/barretenberg/serialize/cbind.hpp index be7374f6d41..974cebeb89c 100644 --- a/barretenberg/cpp/src/barretenberg/serialize/cbind.hpp +++ b/barretenberg/cpp/src/barretenberg/serialize/cbind.hpp @@ -13,7 +13,6 @@ #include "msgpack_impl/schema_impl.hpp" #include "msgpack_impl/schema_name.hpp" #include "msgpack_impl/struct_map_impl.hpp" -#include "msgpack_impl/variant_impl.hpp" #include #include diff --git a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/msgpack_impl.hpp b/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/msgpack_impl.hpp index 1db14a898ad..430dd80635b 100644 --- a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/msgpack_impl.hpp +++ b/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/msgpack_impl.hpp @@ -1,4 +1,3 @@ #pragma once // Note: Meant to only be included in compilation units that need msgpack -#include "struct_map_impl.hpp" -#include "variant_impl.hpp" \ No newline at end of file +#include "struct_map_impl.hpp" \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/struct_map_impl.hpp b/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/struct_map_impl.hpp index cd2d7c5c348..1c4ac87b03c 100644 --- a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/struct_map_impl.hpp +++ b/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/struct_map_impl.hpp @@ -6,6 +6,7 @@ #include #include #define MSGPACK_NO_BOOST +#define MSGPACK_USE_STD_VARIANT_ADAPTOR #include "concepts.hpp" #include "drop_keys.hpp" #include diff --git a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/variant_impl.hpp b/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/variant_impl.hpp deleted file mode 100644 index 8b5d466b537..00000000000 --- a/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/variant_impl.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -// Note: Meant to only be included in compilation units that need msgpack -#define MSGPACK_NO_BOOST -#include -#include - -namespace msgpack::adaptor { -// writes std::variant to msgpack format (TODO should we read std::variant?) -template struct pack> { - auto& operator()(auto& o, std::variant const& variant) const - { - std::visit([&o](const auto& arg) { o.pack(arg); }, variant); - return o; - } -}; -} // namespace msgpack::adaptor diff --git a/barretenberg/cpp/src/msgpack-c/.clang-format b/barretenberg/cpp/src/msgpack-c/.clang-format deleted file mode 100644 index a43d914ec38..00000000000 --- a/barretenberg/cpp/src/msgpack-c/.clang-format +++ /dev/null @@ -1,2 +0,0 @@ -DisableFormat: true -SortIncludes: false \ No newline at end of file diff --git a/barretenberg/cpp/src/msgpack-c/.github/depends/zlib.sh b/barretenberg/cpp/src/msgpack-c/.github/depends/zlib.sh index fce05159c70..3c2c8b25989 100755 --- a/barretenberg/cpp/src/msgpack-c/.github/depends/zlib.sh +++ b/barretenberg/cpp/src/msgpack-c/.github/depends/zlib.sh @@ -27,9 +27,9 @@ while getopts "b:t:p:" c; do done mkdir $prefix || exit 1 -wget https://zlib.net/zlib-1.2.13.tar.gz || exit 1 -tar -xf zlib-1.2.13.tar.gz || exit 1 -cd zlib-1.2.13 +wget https://zlib.net/zlib-1.3.tar.gz || exit 1 +tar -xf zlib-1.3.tar.gz || exit 1 +cd zlib-1.3 build() { diff --git a/barretenberg/cpp/src/msgpack-c/.github/workflows/coverage.yml b/barretenberg/cpp/src/msgpack-c/.github/workflows/coverage.yml index 8c3bf1eda1f..e286d279d72 100644 --- a/barretenberg/cpp/src/msgpack-c/.github/workflows/coverage.yml +++ b/barretenberg/cpp/src/msgpack-c/.github/workflows/coverage.yml @@ -56,6 +56,7 @@ jobs: -D MSGPACK_BUILD_TESTS=ON \ -D CMAKE_BUILD_TYPE=Debug \ -D MSGPACK_GEN_COVERAGE=ON \ + -D MSGPACK_USE_STD_VARIANT_ADAPTOR=ON \ -D CMAKE_PREFIX_PATH="$HOME/zlib-prefix/64;$HOME/boost-prefix/64" \ -B build \ -S . || exit 1 diff --git a/barretenberg/cpp/src/msgpack-c/.github/workflows/gha.yml b/barretenberg/cpp/src/msgpack-c/.github/workflows/gha.yml index 1fbb5ebc593..29fd31bf8e9 100644 --- a/barretenberg/cpp/src/msgpack-c/.github/workflows/gha.yml +++ b/barretenberg/cpp/src/msgpack-c/.github/workflows/gha.yml @@ -145,6 +145,7 @@ jobs: 3) export CXX="g++-10" export MSGPACK_CXX_VERSION="MSGPACK_CXX17=ON" + export MSGPACK_USE_STD_VARIANT_ADAPTOR="MSGPACK_USE_STD_VARIANT_ADAPTOR=ON" ;; 4) export CXX="clang++-10" diff --git a/barretenberg/cpp/src/msgpack-c/.gitignore b/barretenberg/cpp/src/msgpack-c/.gitignore index 40f5e0af3be..7b96d6dfc33 100644 --- a/barretenberg/cpp/src/msgpack-c/.gitignore +++ b/barretenberg/cpp/src/msgpack-c/.gitignore @@ -49,3 +49,8 @@ Makefile /test/streaming_c /test/version /test/zone + +build +*-build +.cache +compile_commands.json diff --git a/barretenberg/cpp/src/msgpack-c/CHANGELOG.md b/barretenberg/cpp/src/msgpack-c/CHANGELOG.md index f29bffe8ad9..73199b017cc 100644 --- a/barretenberg/cpp/src/msgpack-c/CHANGELOG.md +++ b/barretenberg/cpp/src/msgpack-c/CHANGELOG.md @@ -1,3 +1,9 @@ +# 2023-07-08 version 6.1.0 + * Remove dependency on boost in chrono.hpp (#1076) + * Add support for std::variant behavior (#1075) + * Fix msgpack::type::variant behavior to respect MessagePack format (#1071) + * Add rebind allocators (#1065) + # 2023-03-02 version 6.0.0 ## << breaking changes >> * Change CMake package name of C++ library to msgpack-cxx (#1054) diff --git a/barretenberg/cpp/src/msgpack-c/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/CMakeLists.txt index d44df4e21cf..8dc6d610abe 100644 --- a/barretenberg/cpp/src/msgpack-c/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/CMakeLists.txt @@ -23,16 +23,17 @@ OPTION (MSGPACK_CXX14 "Using c++14 compiler" OFF) OPTION (MSGPACK_CXX17 "Using c++17 compiler" OFF) OPTION (MSGPACK_CXX20 "Using c++20 compiler" OFF) -OPTION (MSGPACK_32BIT "32bit compile" OFF) -OPTION (MSGPACK_USE_BOOST "Use Boost libraried" ON) -OPTION (MSGPACK_USE_X3_PARSE "Use Boost X3 parse" OFF) -OPTION (MSGPACK_BUILD_TESTS "Build tests" OFF) -OPTION (MSGPACK_BUILD_DOCS "Build Doxygen documentation" ON) -OPTION (MSGPACK_FUZZ_REGRESSION "Enable regression testing" OFF) -OPTION (MSGPACK_BUILD_EXAMPLES "Build msgpack examples" OFF) -OPTION (MSGPACK_GEN_COVERAGE "Generate coverage report" OFF) -OPTION (MSGPACK_USE_STATIC_BOOST "Statically link with boost libraries" OFF) -OPTION (MSGPACK_CHAR_SIGN "Char sign to use (signed or unsigned)") +OPTION (MSGPACK_32BIT "32bit compile" OFF) +OPTION (MSGPACK_USE_BOOST "Use Boost libraried" ON) +OPTION (MSGPACK_USE_X3_PARSE "Use Boost X3 parse" OFF) +OPTION (MSGPACK_BUILD_TESTS "Build tests" OFF) +OPTION (MSGPACK_BUILD_DOCS "Build Doxygen documentation" ON) +OPTION (MSGPACK_FUZZ_REGRESSION "Enable regression testing" OFF) +OPTION (MSGPACK_BUILD_EXAMPLES "Build msgpack examples" OFF) +OPTION (MSGPACK_GEN_COVERAGE "Generate coverage report" OFF) +OPTION (MSGPACK_USE_STATIC_BOOST "Statically link with boost libraries" OFF) +OPTION (MSGPACK_CHAR_SIGN "Char sign to use (signed or unsigned)") +OPTION (MSGPACK_USE_STD_VARIANT_ADAPTOR "Enable the adaptor for std::variant" OFF) SET (CMAKE_CXX_STANDARD_REQUIRED ON) @@ -92,6 +93,10 @@ ELSE () TARGET_COMPILE_DEFINITIONS(msgpack-cxx INTERFACE MSGPACK_DEFAULT_API_VERSION=3) ENDIF () +IF (MSGPACK_USE_STD_VARIANT_ADAPTOR) + TARGET_COMPILE_DEFINITIONS(msgpack-cxx INTERFACE MSGPACK_USE_STD_VARIANT_ADAPTOR) +ENDIF () + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") IF (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.1) INCLUDE (CheckCXXSourceCompiles) diff --git a/barretenberg/cpp/src/msgpack-c/Files.cmake b/barretenberg/cpp/src/msgpack-c/Files.cmake index 5cdf80afd86..721a31fbf50 100644 --- a/barretenberg/cpp/src/msgpack-c/Files.cmake +++ b/barretenberg/cpp/src/msgpack-c/Files.cmake @@ -33,6 +33,7 @@ SET (msgpack-cxx_HEADERS include/msgpack/adaptor/cpp17/carray_byte.hpp include/msgpack/adaptor/cpp17/optional.hpp include/msgpack/adaptor/cpp17/string_view.hpp + include/msgpack/adaptor/cpp17/variant.hpp include/msgpack/adaptor/cpp17/vector_byte.hpp include/msgpack/adaptor/cpp20/span.hpp include/msgpack/adaptor/define.hpp @@ -542,6 +543,7 @@ SET (msgpack-cxx_HEADERS include/msgpack/v1/adaptor/cpp17/carray_byte.hpp include/msgpack/v1/adaptor/cpp17/optional.hpp include/msgpack/v1/adaptor/cpp17/string_view.hpp + include/msgpack/v1/adaptor/cpp17/variant.hpp include/msgpack/v1/adaptor/cpp17/vector_byte.hpp include/msgpack/v1/adaptor/cpp20/span.hpp include/msgpack/v1/adaptor/define.hpp diff --git a/barretenberg/cpp/src/msgpack-c/README.md b/barretenberg/cpp/src/msgpack-c/README.md index 9f6a459b631..cf578c5409d 100644 --- a/barretenberg/cpp/src/msgpack-c/README.md +++ b/barretenberg/cpp/src/msgpack-c/README.md @@ -1,7 +1,7 @@ `msgpack` for C++ =================== -Version 6.0.0 [![Build Status](https://github.com/msgpack/msgpack-c/workflows/CI/badge.svg?branch=cpp_master)](https://github.com/msgpack/msgpack-c/actions) [![Build status](https://ci.appveyor.com/api/projects/status/8kstcgt79qj123mw/branch/cpp_master?svg=true)](https://ci.appveyor.com/project/redboltz/msgpack-c/branch/cpp_master) +Version 6.1.0 [![Build Status](https://github.com/msgpack/msgpack-c/workflows/CI/badge.svg?branch=cpp_master)](https://github.com/msgpack/msgpack-c/actions) [![Build status](https://ci.appveyor.com/api/projects/status/8kstcgt79qj123mw/branch/cpp_master?svg=true)](https://ci.appveyor.com/project/redboltz/msgpack-c/branch/cpp_master) [![codecov](https://codecov.io/gh/msgpack/msgpack-c/branch/cpp_master/graph/badge.svg)](https://codecov.io/gh/msgpack/msgpack-c/branch/cpp_master) It's like JSON but smaller and faster. diff --git a/barretenberg/cpp/src/msgpack-c/appveyor.yml b/barretenberg/cpp/src/msgpack-c/appveyor.yml index 4218f3a3d91..6f5034b8c0e 100644 --- a/barretenberg/cpp/src/msgpack-c/appveyor.yml +++ b/barretenberg/cpp/src/msgpack-c/appveyor.yml @@ -1,4 +1,4 @@ -version: 6.0.0.{build} +version: 6.1.0.{build} branches: only: @@ -23,10 +23,10 @@ environment: boost_subdir: lib32-msvc-14.0 build_script: - ps: | - appveyor DownloadFile http://zlib.net/zlib-1.2.13.tar.gz -FileName zlib-1.2.13.tar.gz - 7z x zlib-1.2.13.tar.gz 2> $null - 7z x zlib-1.2.13.tar 2> $null - cd zlib-1.2.13 + appveyor DownloadFile http://zlib.net/zlib-1.3.tar.gz -FileName zlib-1.3.tar.gz + 7z x zlib-1.3.tar.gz 2> $null + 7z x zlib-1.3.tar 2> $null + cd zlib-1.3 md build md prefix @@ -34,7 +34,7 @@ build_script: cmake ` -G $env:msvc ` - -D CMAKE_INSTALL_PREFIX="$env:APPVEYOR_BUILD_FOLDER\zlib-1.2.13\prefix" ` + -D CMAKE_INSTALL_PREFIX="$env:APPVEYOR_BUILD_FOLDER\zlib-1.3\prefix" ` .. if ($LastExitCode -ne 0) { exit $LastExitCode } @@ -52,7 +52,7 @@ build_script: -D MSGPACK_BUILD_EXAMPLES=ON ` -D MSGPACK_BUILD_TESTS=ON ` -D CMAKE_EXE_LINKER_FLAGS=/LIBPATH:"$env:boost_prefix\$env:boost_subdir" ` - -D CMAKE_PREFIX_PATH="$env:boost_prefix;$env:APPVEYOR_BUILD_FOLDER\zlib-1.2.13\prefix" ` + -D CMAKE_PREFIX_PATH="$env:boost_prefix;$env:APPVEYOR_BUILD_FOLDER\zlib-1.3\prefix" ` -D CMAKE_INSTALL_PREFIX="$env:APPVEYOR_BUILD_FOLDER\prefix" ` -D CMAKE_CXX_FLAGS="/D_VARIADIC_MAX=10 /EHsc /DBOOST_ALL_DYN_LINK" ` .. @@ -62,5 +62,5 @@ build_script: if ($LastExitCode -ne 0) { exit $LastExitCode } test_script: -- set PATH=%PATH%;%APPVEYOR_BUILD_FOLDER%\zlib-1.2.13\build\Release;%APPVEYOR_BUILD_FOLDER%\build\release;%boost_prefix%\%boost_subdir% +- set PATH=%PATH%;%APPVEYOR_BUILD_FOLDER%\zlib-1.3\build\Release;%APPVEYOR_BUILD_FOLDER%\build\release;%boost_prefix%\%boost_subdir% - ctest -VV -C Release diff --git a/barretenberg/cpp/src/msgpack-c/ci/build_cmake.sh b/barretenberg/cpp/src/msgpack-c/ci/build_cmake.sh index a89c44641f1..b6ad78c50a2 100755 --- a/barretenberg/cpp/src/msgpack-c/ci/build_cmake.sh +++ b/barretenberg/cpp/src/msgpack-c/ci/build_cmake.sh @@ -21,6 +21,7 @@ cmake \ -D MSGPACK_CHAR_SIGN=${CHAR_SIGN} \ -D MSGPACK_DEFAULT_API_VERSION=${API_VERSION} \ -D MSGPACK_USE_X3_PARSE=${X3_PARSE} \ + -D MSGPACK_USE_STD_VARIANT_ADAPTOR=${STD_VARIANT_ADAPTOR} \ -D CMAKE_CXX_FLAGS="${CXXFLAGS} ${ARCH_FLAG}" \ -D CMAKE_INSTALL_PREFIX=$prefix_dir \ -B $build_dir \ diff --git a/barretenberg/cpp/src/msgpack-c/erb/v1/cpp03_zone.hpp.erb b/barretenberg/cpp/src/msgpack-c/erb/v1/cpp03_zone.hpp.erb index 1081f72457f..86bc8328631 100644 --- a/barretenberg/cpp/src/msgpack-c/erb/v1/cpp03_zone.hpp.erb +++ b/barretenberg/cpp/src/msgpack-c/erb/v1/cpp03_zone.hpp.erb @@ -29,121 +29,101 @@ MSGPACK_API_VERSION_NAMESPACE(v1) { class zone { struct finalizer { - finalizer(void (*func)(void*), void* data):m_func(func), m_data(data) {} + finalizer(void (*func)(void*), void* data, finalizer* next): m_func(func), m_data(data), m_next(next) {} void operator()() { m_func(m_data); } void (*m_func)(void*); void* m_data; + finalizer* m_next; }; + struct finalizer_array { - finalizer_array():m_tail(MSGPACK_NULLPTR), m_end(MSGPACK_NULLPTR), m_array(MSGPACK_NULLPTR) {} - void call() { - finalizer* fin = m_tail; - for(; fin != m_array; --fin) (*(fin-1))(); - } + finalizer_array(): m_head(MSGPACK_NULLPTR) {} + ~finalizer_array() { - call(); - ::free(m_array); - } - void clear() { - call(); - m_tail = m_array; + clear(); } - void push(void (*func)(void* data), void* data) - { - finalizer* fin = m_tail; - if(fin == m_end) { - push_expand(func, data); - return; + void clear() { + finalizer* fin = m_head; + finalizer* tmp = MSGPACK_NULLPTR; + while(fin) { + (*fin)(); + tmp = fin; + fin = fin->m_next; + delete tmp; } + m_head = MSGPACK_NULLPTR; + } - fin->m_func = func; - fin->m_data = data; - - ++m_tail; + void push(void (*func)(void* data), void* data) { + m_head = new finalizer(func, data, m_head); } - void push_expand(void (*func)(void*), void* data) { - const size_t nused = static_cast(m_end - m_array); - size_t nnext; - if(nused == 0) { - nnext = (sizeof(finalizer) < 72/2) ? - 72 / sizeof(finalizer) : 8; - } else { - nnext = nused * 2; - } - finalizer* tmp = - static_cast(::realloc(m_array, sizeof(finalizer) * nnext)); - if(!tmp) { - throw std::bad_alloc(); - } - m_array = tmp; - m_end = tmp + nnext; - m_tail = tmp + nused; - new (m_tail) finalizer(func, data); - ++m_tail; + void pop() { + finalizer* n = m_head->m_next; + delete m_head; + m_head = n; } - finalizer* m_tail; - finalizer* m_end; - finalizer* m_array; + + finalizer* m_head; + private: + finalizer_array(const finalizer_array&); + finalizer_array& operator=(const finalizer_array&); }; + struct chunk { chunk* m_next; }; - struct chunk_list { - chunk_list(size_t chunk_size) - { - chunk* c = static_cast(::malloc(sizeof(chunk) + chunk_size)); - if(!c) { - throw std::bad_alloc(); - } - m_head = c; - m_free = chunk_size; - m_ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = MSGPACK_NULLPTR; - } - ~chunk_list() - { + struct chunk_list { + chunk_list(size_t chunk_size, char* ptr): m_free(chunk_size), m_ptr(ptr), m_head(MSGPACK_NULLPTR) {} + ~chunk_list() { chunk* c = m_head; while(c) { chunk* n = c->m_next; ::free(c); c = n; } + m_head = MSGPACK_NULLPTR; } - void clear(size_t chunk_size) - { + + void clear(size_t chunk_size, char* ptr) { chunk* c = m_head; - while(true) { + while(c) { chunk* n = c->m_next; - if(n) { - ::free(c); - c = n; - } else { - m_head = c; - break; - } + ::free(c); + c = n; } - m_head->m_next = MSGPACK_NULLPTR; + m_head = MSGPACK_NULLPTR; m_free = chunk_size; - m_ptr = reinterpret_cast(m_head) + sizeof(chunk); + m_ptr = ptr; } + size_t m_free; char* m_ptr; chunk* m_head; + + private: + chunk_list(const chunk_list&); + chunk_list& operator=(const chunk_list&); }; + size_t m_chunk_size; - chunk_list m_chunk_list; + chunk_list* m_chunk_list; finalizer_array m_finalizer_array; public: zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + ~zone(); -public: void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); + void* allocate_no_align(size_t size); + bool allocated() { + return m_chunk_list != MSGPACK_NULLPTR; + } + void push_finalizer(void (*func)(void*), void* data); template @@ -152,24 +132,23 @@ public: void clear(); void swap(zone& o); - static void* operator new(std::size_t size) - { + + static void* operator new(std::size_t size) { void* p = ::malloc(size); if (!p) throw std::bad_alloc(); return p; } - static void operator delete(void *p) /* throw() */ - { + + static void operator delete(void *p) /* throw() */ { ::free(p); } - static void* operator new(std::size_t size, void* place) /* throw() */ - { - return ::operator new(size, place); - } - static void operator delete(void* p, void* place) /* throw() */ - { - ::operator delete(p, place); + + static void* operator new(std::size_t /*size*/, void* mem) /* throw() */ { + return mem; } + + static void operator delete(void * /*p*/, void* /*mem*/) /* throw() */ {} + /// @cond <%0.upto(GENERATION_LIMIT) {|i|%> template , typename A<%=j%><%}%>> @@ -188,18 +167,26 @@ private: static char* get_aligned(char* ptr, size_t align); + chunk_list& get_chank_lst(); + char* allocate_expand(size_t size); private: zone(const zone&); zone& operator=(const zone&); }; -inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(m_chunk_size) -{ +inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(MSGPACK_NULLPTR) {} + +inline zone::~zone() { + m_finalizer_array.~finalizer_array(); + if(m_chunk_list) { + m_chunk_list->~chunk_list(); + ::free(m_chunk_list); + m_chunk_list = MSGPACK_NULLPTR; + } } -inline char* zone::get_aligned(char* ptr, size_t align) -{ +inline char* zone::get_aligned(char* ptr, size_t align) { MSGPACK_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0) return reinterpret_cast( @@ -207,37 +194,45 @@ inline char* zone::get_aligned(char* ptr, size_t align) ); } -inline void* zone::allocate_align(size_t size, size_t align) -{ - char* aligned = get_aligned(m_chunk_list.m_ptr, align); - size_t adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); - if (m_chunk_list.m_free < adjusted_size) { +inline zone::chunk_list& zone::get_chank_lst() { + if (!m_chunk_list) { + void* ptr = ::malloc(sizeof(chunk_list) + m_chunk_size); + if (!ptr) + throw std::bad_alloc(); + m_chunk_list = new (ptr) chunk_list(m_chunk_size, reinterpret_cast(ptr) + sizeof(chunk_list)); + } + return *m_chunk_list; +} + +inline void* zone::allocate_align(size_t size, size_t align) { + chunk_list& chank_lst = get_chank_lst(); + char* aligned = get_aligned(chank_lst.m_ptr, align); + size_t adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); + if (chank_lst.m_free < adjusted_size) { size_t enough_size = size + align - 1; char* ptr = allocate_expand(enough_size); aligned = get_aligned(ptr, align); - adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); + adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); } - m_chunk_list.m_free -= adjusted_size; - m_chunk_list.m_ptr += adjusted_size; + chank_lst.m_free -= adjusted_size; + chank_lst.m_ptr += adjusted_size; return aligned; } -inline void* zone::allocate_no_align(size_t size) -{ - char* ptr = m_chunk_list.m_ptr; - if(m_chunk_list.m_free < size) { +inline void* zone::allocate_no_align(size_t size) { + chunk_list& chank_lst = get_chank_lst(); + char* ptr = chank_lst.m_ptr; + if(chank_lst.m_free < size) { ptr = allocate_expand(size); } - m_chunk_list.m_free -= size; - m_chunk_list.m_ptr += size; + chank_lst.m_free -= size; + chank_lst.m_ptr += size; return ptr; } -inline char* zone::allocate_expand(size_t size) -{ - chunk_list* const cl = &m_chunk_list; - +inline char* zone::allocate_expand(size_t size) { + chunk_list& cl = get_chank_lst(); size_t sz = m_chunk_size; while(sz < size) { @@ -254,60 +249,54 @@ inline char* zone::allocate_expand(size_t size) char* ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = cl->m_head; - cl->m_head = c; - cl->m_free = sz; - cl->m_ptr = ptr; + c->m_next = cl.m_head; + cl.m_head = c; + cl.m_free = sz; + cl.m_ptr = ptr; return ptr; } -inline void zone::push_finalizer(void (*func)(void*), void* data) -{ +inline void zone::push_finalizer(void (*func)(void*), void* data) { m_finalizer_array.push(func, data); } template -inline void zone::push_finalizer(msgpack::unique_ptr obj) -{ +inline void zone::push_finalizer(msgpack::unique_ptr obj) { m_finalizer_array.push(&zone::object_delete, obj.release()); } -inline void zone::clear() -{ +inline void zone::clear() { m_finalizer_array.clear(); - m_chunk_list.clear(m_chunk_size); + if (m_chunk_list) { + m_chunk_list->clear(m_chunk_size, reinterpret_cast(m_chunk_list) + sizeof(chunk_list)); + } } -inline void zone::swap(zone& o) -{ +inline void zone::swap(zone& o) { using std::swap; swap(m_chunk_size, o.m_chunk_size); swap(m_chunk_list, o.m_chunk_list); - swap(m_finalizer_array, o.m_finalizer_array); + swap(m_finalizer_array.m_head, o.m_finalizer_array.m_head); } template -void zone::object_destruct(void* obj) -{ - static_cast(obj)->~T(); +void zone::object_delete(void* obj) { + delete static_cast(obj); } template -void zone::object_delete(void* obj) -{ - delete static_cast(obj); +void zone::object_destruct(void* obj) { + static_cast(obj)->~T(); } -inline void zone::undo_allocate(size_t size) -{ - m_chunk_list.m_ptr -= size; - m_chunk_list.m_free += size; +inline void zone::undo_allocate(size_t size) { + chunk_list& cl = get_chank_lst(); + cl.m_ptr -= size; + cl.m_free += size; } -inline std::size_t aligned_size( - std::size_t size, - std::size_t align) { +inline std::size_t aligned_size(std::size_t size, std::size_t align) { return (size + align - 1) / align * align; } @@ -326,7 +315,7 @@ T* zone::allocate(<%=(1..i).map{|j|"A#{j} a#{j}"}.join(', ')%>) try { return new (x) T(<%=(1..i).map{|j|"a#{j}"}.join(', ')%>); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } diff --git a/barretenberg/cpp/src/msgpack-c/example/boost/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/example/boost/CMakeLists.txt index ad9c979ce47..6464513b326 100644 --- a/barretenberg/cpp/src/msgpack-c/example/boost/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/example/boost/CMakeLists.txt @@ -1,4 +1,4 @@ -FIND_PACKAGE (Boost REQUIRED COMPONENTS system) +FIND_PACKAGE (Boost REQUIRED) FIND_PACKAGE (Threads REQUIRED) FIND_PACKAGE (ZLIB REQUIRED) @@ -22,7 +22,6 @@ FOREACH (source_file ${exec_PROGRAMS}) ) TARGET_LINK_LIBRARIES (${source_file_we} PRIVATE msgpack-cxx - Boost::system Threads::Threads ) IF (ZLIB_FOUND) diff --git a/barretenberg/cpp/src/msgpack-c/example/x3/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/example/x3/CMakeLists.txt index 5fc6c1afa40..963d6da4f4b 100644 --- a/barretenberg/cpp/src/msgpack-c/example/x3/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/example/x3/CMakeLists.txt @@ -1,5 +1,5 @@ IF (MSGPACK_USE_X3_PARSE AND MSGPACK_DEFAULT_API_VERSION VERSION_GREATER 1) - FIND_PACKAGE (Boost REQUIRED COMPONENTS context system) + FIND_PACKAGE (Boost REQUIRED COMPONENTS context) FIND_PACKAGE (Threads REQUIRED) LIST (APPEND exec_PROGRAMS @@ -42,7 +42,6 @@ IF (MSGPACK_USE_X3_PARSE AND MSGPACK_DEFAULT_API_VERSION VERSION_GREATER 1) TARGET_LINK_LIBRARIES (${source_file_we} PRIVATE msgpack-cxx Boost::context - Boost::system Threads::Threads ) IF ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") diff --git a/barretenberg/cpp/src/msgpack-c/fuzz/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/fuzz/CMakeLists.txt index 819130ed2fd..9779687417d 100644 --- a/barretenberg/cpp/src/msgpack-c/fuzz/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/fuzz/CMakeLists.txt @@ -1,5 +1,5 @@ FIND_PACKAGE (Threads REQUIRED) -FIND_PACKAGE (Boost REQUIRED COMPONENTS system filesystem unit_test_framework) +FIND_PACKAGE (Boost REQUIRED COMPONENTS filesystem unit_test_framework) LIST (APPEND check_PROGRAMS regression_runner.cpp @@ -19,7 +19,6 @@ FOREACH (source_file ${check_PROGRAMS}) msgpack-cxx Threads::Threads Boost::filesystem - Boost::system Boost::unit_test_framework ) diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/adaptor/cpp17/variant.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/adaptor/cpp17/variant.hpp new file mode 100644 index 00000000000..bd73ff9ae5e --- /dev/null +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/adaptor/cpp17/variant.hpp @@ -0,0 +1,16 @@ +// +// MessagePack for C++ static resolution routine +// +// Copyright (C) 2023 Uy Ha +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef MSGPACK_TYPE_CPP17_VARIANT_HPP +#define MSGPACK_TYPE_CPP17_VARIANT_HPP + +#include "msgpack/v1/adaptor/cpp17/variant.hpp" + +#endif // MSGPACK_TYPE_CPP17_VARIANT_HPP diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/assert.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/assert.hpp index 180e345c7a9..0b2d0bcd0c1 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/assert.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/assert.hpp @@ -16,7 +16,7 @@ #include #define MSGPACK_ASSERT assert -#else // defined(MSGPACK_NO_BOOST) +#else // defined(MSGPACK_NO_BOOST) #include #define MSGPACK_ASSERT BOOST_ASSERT @@ -25,16 +25,15 @@ #ifdef __wasm__ struct AbortStream { - void operator<< [[noreturn]] (const auto& error) - { - (void)error; // TODO how to print this? + void operator<< [[noreturn]] (const auto& error) { + info(error.what()); std::abort(); } }; #define THROW AbortStream() << #define try if (true) #define catch(...) if (false) -#define RETHROW +#define RETHROW #else #define THROW throw #define RETHROW THROW diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/type.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/type.hpp index e48bdade9a5..1ab49745fb1 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/type.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/type.hpp @@ -37,9 +37,7 @@ #include "adaptor/cpp11/array_char.hpp" #include "adaptor/cpp11/array_unsigned_char.hpp" -#if !defined(MSGPACK_NO_BOOST) #include "adaptor/cpp11/chrono.hpp" -#endif // !defined(MSGPACK_NO_BOOST) #include "adaptor/cpp11/forward_list.hpp" #include "adaptor/cpp11/reference_wrapper.hpp" @@ -63,6 +61,10 @@ #include "adaptor/cpp17/carray_byte.hpp" #include "adaptor/cpp17/vector_byte.hpp" +#if MSGPACK_HAS_INCLUDE() +#include "adaptor/cpp17/variant.hpp" +#endif // MSGPACK_HAS_INCLUDE() + #if MSGPACK_HAS_INCLUDE() #include "adaptor/cpp20/span.hpp" #endif // MSGPACK_HAS_INCLUDE() diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp index a87a4e44513..509183ff8b0 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp @@ -122,6 +122,12 @@ struct basic_variant : int_init(v); } basic_variant(unsigned long long v):base(uint64_t(v)) {} + basic_variant(float v) { + double_init(v); + } + basic_variant(double v) { + double_init(v); + } bool is_nil() const { return boost::get(this) != MSGPACK_NULLPTR; @@ -177,71 +183,50 @@ struct basic_variant : int64_t as_int64_t() const { return boost::get(*this); } - int64_t& as_int64_t() { - return boost::get(*this); - } uint64_t as_uint64_t() const { return boost::get(*this); } - uint64_t& as_uint64_t() { - return boost::get(*this); - } double as_double() const { - return boost::get(*this); - } - double& as_double() { - return boost::get(*this); + if (is_double()) { + return boost::get(*this); + } + if (is_int64_t()) { + return static_cast(boost::get(*this)); + } + if (is_uint64_t()) { + return static_cast(boost::get(*this)); + } + THROW msgpack::type_error(); } std::string const& as_string() const { return boost::get(*this); } - std::string& as_string() { - return boost::get(*this); - } #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 boost::string_ref const& as_boost_string_ref() const { return boost::get(*this); } - boost::string_ref& as_boost_string_ref() { - return boost::get(*this); - } #endif // (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 53 std::vector const& as_vector_char() const { return boost::get >(*this); } - std::vector& as_vector_char() { - return boost::get >(*this); - } raw_ref const& as_raw_ref() const { return boost::get(*this); } ext const& as_ext() const { return boost::get(*this); } - ext& as_ext() { - return boost::get(*this); - } ext_ref const& as_ext_ref() const { return boost::get(*this); } std::vector > const& as_vector() const { return boost::get > >(*this); } - std::vector >& as_vector() { - return boost::get > >(*this); - } std::map, basic_variant > const& as_map() const { return boost::get, basic_variant > >(*this); } - std::map, basic_variant >& as_map() { - return boost::get, basic_variant > >(*this); - } std::multimap, basic_variant > const& as_multimap() const { return boost::get, basic_variant > >(*this); } - std::multimap, basic_variant >& as_multimap() { - return boost::get, basic_variant > >(*this); - } private: template void int_init(T v) { @@ -252,6 +237,19 @@ struct basic_variant : static_cast(*this) = uint64_t(v); } } + void double_init(double v) { + if (v == v) { // check for nan + if (v >= 0 && v <= double(std::numeric_limits::max()) && v == double(uint64_t(v))) { + static_cast(*this) = uint64_t(v); + return; + } + else if (v < 0 && v >= double(std::numeric_limits::min()) && v == double(int64_t(v))) { + static_cast(*this) = int64_t(v); + return; + } + } + static_cast(*this) = v; + } }; template diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp11/chrono.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp11/chrono.hpp index 9607022daca..e543c0ac99b 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp11/chrono.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp11/chrono.hpp @@ -11,17 +11,14 @@ #ifndef MSGPACK_V1_TYPE_CPP11_CHRONO_HPP #define MSGPACK_V1_TYPE_CPP11_CHRONO_HPP -#if !defined(MSGPACK_NO_BOOST) - #include "msgpack/versioning.hpp" #include "msgpack/adaptor/adaptor_base.hpp" #include "msgpack/object.hpp" #include "msgpack/adaptor/check_container_size.hpp" +#include #include -#include - namespace msgpack { /// @cond @@ -30,6 +27,113 @@ MSGPACK_API_VERSION_NAMESPACE(v1) { namespace adaptor { +namespace detail { +template < + typename Target, + typename Source, + bool target_is_signed = std::is_signed::value, + bool source_is_signed = std::is_signed::value, + typename = typename std::enable_if< + std::is_integral::value && + std::is_integral::value + >::type +> +struct would_underflow { + // The default case includes the cases that Source being unsigned, and since Source + // is unsigned, no underflow can happen + would_underflow(Source) : value{false} {} + bool value; +}; + +template +struct would_underflow { + // When Source is signed and Target is unsigned, we only need to compare with 0 to + // detect underflow, this works correctly and also avoids warnings from the compiler + would_underflow(Source source) : value{source < 0} {} + bool value; +}; +template +struct would_underflow { + // When Source and Target are signed, the promotion rules apply sensibly so we do + // not need to do anything + would_underflow(Source source) + : value{source < std::numeric_limits::min()} {} + bool value; +}; + +template < + typename Target, + typename Source, + bool target_is_signed = std::is_signed::value, + bool source_is_signed = std::is_signed::value, + typename = typename std::enable_if< + std::is_integral::value && + std::is_integral::value + >::type +> +struct would_overflow { + // The default case is Source and Target having the same signedness, the promotion + // rule also apply sensibly here so nothing special needs to be done + would_overflow(Source source) + : value{source > std::numeric_limits::max()} {} + bool value; +}; +template +struct would_overflow { + // When Target is unsigned and Source is signed, we cannot rely on the promotion + // rule. + would_overflow(Source source) + : value{ + sizeof(Target) >= sizeof(Source) + // Given Source is signed, Target being unsigned and having at least the + // same size makes impossible to overflow + ? false + // Source being larger than Target makes it safe to cast the maximum value + // of Target to Source + : source > static_cast(std::numeric_limits::max()) + } {} + bool value; +}; +template +struct would_overflow { + // When Target is signed and Source is unsigned, we cannot rely on the promotion + // rule. + would_overflow(Source source) + : value{ + sizeof(Target) > sizeof(Source) + // Target being larger than Source makes it impossible to overflow + ? false + // Source being unsigned and having at least the size of Target makes it + // safe to cast the maximum value of Target to Source + : source > static_cast(std::numeric_limits::max()) + } {} + bool value; +}; + +template < + typename Target, + typename Source, + typename = typename std::enable_if< + std::is_integral::value && + std::is_integral::value + >::type +> +Target integral_cast(Source source) { + if (would_underflow(source).value) { + THROW std::underflow_error{ + "casting from Source to Target causes an underflow error" + }; + } + if(would_overflow(source).value) { + THROW std::overflow_error{ + "casting from Source to Target causes an overflow error" + }; + } + + return static_cast(source); +} +} // namespace detail + template struct as> { typename std::chrono::time_point operator()(msgpack::object const& o) const { @@ -45,7 +149,7 @@ struct as> { case 8: { uint64_t value; _msgpack_load64(uint64_t, o.via.ext.data(), &value); - uint32_t nanosec = boost::numeric_cast(value >> 34); + uint32_t nanosec = detail::integral_cast(value >> 34); uint64_t sec = value & 0x00000003ffffffffLL; tp += std::chrono::duration_cast( std::chrono::nanoseconds(nanosec)); @@ -69,7 +173,7 @@ struct as> { else { ++sec; tp += std::chrono::seconds(sec); - int64_t ns = boost::numeric_cast(nanosec) - 1000000000L; + int64_t ns = detail::integral_cast(nanosec) - 1000000000L; tp += std::chrono::duration_cast( std::chrono::nanoseconds(ns)); } @@ -98,7 +202,7 @@ struct convert> { case 8: { uint64_t value; _msgpack_load64(uint64_t, o.via.ext.data(), &value); - uint32_t nanosec = boost::numeric_cast(value >> 34); + uint32_t nanosec = detail::integral_cast(value >> 34); uint64_t sec = value & 0x00000003ffffffffLL; tp += std::chrono::duration_cast( std::chrono::nanoseconds(nanosec)); @@ -123,7 +227,7 @@ struct convert> { else { ++sec; tp += std::chrono::seconds(sec); - int64_t ns = boost::numeric_cast(nanosec) - 1000000000L; + int64_t ns = detail::integral_cast(nanosec) - 1000000000L; tp += std::chrono::duration_cast( std::chrono::nanoseconds(ns)); } @@ -142,7 +246,7 @@ template struct pack> { template msgpack::packer& operator()(msgpack::packer& o, std::chrono::time_point const& v) const { - int64_t count = boost::numeric_cast(v.time_since_epoch().count()); + int64_t count = detail::integral_cast(v.time_since_epoch().count()); int64_t nano_num = Duration::period::ratio::num * (1000000000L / Duration::period::ratio::den); @@ -158,11 +262,11 @@ struct pack> { / Duration::period::ratio::den; if ((sec >> 34) == 0) { - uint64_t data64 = (boost::numeric_cast(nanosec) << 34) | boost::numeric_cast(sec); + uint64_t data64 = (detail::integral_cast(nanosec) << 34) | detail::integral_cast(sec); if ((data64 & 0xffffffff00000000L) == 0) { // timestamp 32 o.pack_ext(4, -1); - uint32_t data32 = boost::numeric_cast(data64); + uint32_t data32 = detail::integral_cast(data64); char buf[4]; _msgpack_store32(buf, data32); o.pack_ext_body(buf, 4); @@ -181,7 +285,7 @@ struct pack> { char buf[12]; - _msgpack_store32(&buf[0], boost::numeric_cast(nanosec)); + _msgpack_store32(&buf[0], detail::integral_cast(nanosec)); _msgpack_store64(&buf[4], sec); o.pack_ext_body(buf, 12); } @@ -192,7 +296,7 @@ struct pack> { template struct object_with_zone> { void operator()(msgpack::object::with_zone& o, const std::chrono::time_point& v) const { - int64_t count = boost::numeric_cast(v.time_since_epoch().count()); + int64_t count = detail::integral_cast(v.time_since_epoch().count()); int64_t nano_num = Duration::period::ratio::num * @@ -208,14 +312,14 @@ struct object_with_zone> { * Duration::period::ratio::num / Duration::period::ratio::den; if ((sec >> 34) == 0) { - uint64_t data64 = (boost::numeric_cast(nanosec) << 34) | boost::numeric_cast(sec); + uint64_t data64 = (detail::integral_cast(nanosec) << 34) | detail::integral_cast(sec); if ((data64 & 0xffffffff00000000L) == 0) { // timestamp 32 o.type = msgpack::type::EXT; o.via.ext.size = 4; char* p = static_cast(o.zone.allocate_no_align(o.via.ext.size + 1)); p[0] = static_cast(-1); - uint32_t data32 = boost::numeric_cast(data64); + uint32_t data32 = detail::integral_cast(data64); _msgpack_store32(&p[1], data32); o.via.ext.ptr = p; } @@ -235,7 +339,7 @@ struct object_with_zone> { o.via.ext.size = 12; char* p = static_cast(o.zone.allocate_no_align(o.via.ext.size + 1)); p[0] = static_cast(-1); - _msgpack_store32(&p[1], boost::numeric_cast(nanosec)); + _msgpack_store32(&p[1], detail::integral_cast(nanosec)); _msgpack_store64(&p[1 + 4], sec); o.via.ext.ptr = p; } @@ -250,6 +354,4 @@ struct object_with_zone> { } // namespace msgpack -#endif // !defined(MSGPACK_NO_BOOST) - #endif // MSGPACK_V1_TYPE_CPP11_CHRONO_HPP diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp17/variant.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp17/variant.hpp new file mode 100644 index 00000000000..d8bb33dc33a --- /dev/null +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/adaptor/cpp17/variant.hpp @@ -0,0 +1,119 @@ +// +// MessagePack for C++ static resolution routine +// +// Copyright (C) 2023 Uy Ha +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef MSGPACK_V1_TYPE_VARIANT_HPP +#define MSGPACK_V1_TYPE_VARIANT_HPP + +#if defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) + +#include "msgpack/cpp_version.hpp" + +#if MSGPACK_CPP_VERSION >= 201703 + +#include "msgpack/adaptor/adaptor_base.hpp" +#include "msgpack/object.hpp" +#include "msgpack/versioning.hpp" + +#include + +namespace msgpack { +MSGPACK_API_VERSION_NAMESPACE(v1) +{ + namespace adaptor { + namespace detail { + template + Variant construct_variant(std::size_t index, + msgpack::object& object, + std::index_sequence) + { + if constexpr (sizeof...(Ts) == 0) { + return object.as(); + } else { + if (index == current_index) { + return object.as(); + } + return construct_variant(index, object, std::index_sequence()); + } + } + + struct object_variant_overload { + object_variant_overload(msgpack::object& obj, msgpack::zone& zone) + : obj{ obj } + , zone{ zone } + {} + + template void operator()(T const& value) { obj = msgpack::object(value, zone); } + + msgpack::object& obj; + msgpack::zone& zone; + }; + } // namespace detail + + template + struct as, typename std::enable_if<(msgpack::has_as::value && ...)>::type> { + std::variant operator()(msgpack::object const& o) const + { + if (o.type != msgpack::type::ARRAY || o.via.array.size != 2 || + o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER || + o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { + THROW msgpack::type_error{}; + } + + return detail::construct_variant, Ts...>( + o.via.array.ptr[0].as(), o.via.array.ptr[1], std::make_index_sequence()); + } + }; + + template struct convert> { + msgpack::object const& operator()(msgpack::object const& o, std::variant& v) const + { + if (o.type != msgpack::type::ARRAY || o.via.array.size != 2 || + o.via.array.ptr[0].type != msgpack::type::POSITIVE_INTEGER || + o.via.array.ptr[0].via.u64 >= sizeof...(Ts)) { + THROW msgpack::type_error{}; + } + + v = detail::construct_variant, Ts...>( + o.via.array.ptr[0].as(), o.via.array.ptr[1], std::make_index_sequence()); + return o; + } + }; + + template struct pack> { + template + msgpack::packer& operator()(msgpack::packer& o, std::variant const& v) const + { + o.pack_array(2); + o.pack_uint64(v.index()); + std::visit([&o](auto const& value) { o.pack(value); }, v); + return o; + } + }; + + template struct object_with_zone> { + void operator()(msgpack::object::with_zone& o, std::variant const& v) const + { + msgpack::object* p = static_cast( + o.zone.allocate_align(sizeof(msgpack::object) * 2, MSGPACK_ZONE_ALIGNOF(msgpack::object))); + + o.type = msgpack::type::ARRAY; + o.via.array.size = 2; + o.via.array.ptr = p; + o.via.array.ptr[0] = msgpack::object(v.index(), o.zone); + std::visit(detail::object_variant_overload(o.via.array.ptr[1], o.zone), v); + } + }; + } // namespace adaptor +} +} // namespace msgpack + +#endif // MSGPACK_CPP_VERSION >= 201703 +#endif // defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) +#endif // MSGPACK_V1_TYPE_VARIANT_HPP diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp03_zone.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp03_zone.hpp index 75ad3af3f86..45a75a12cec 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp03_zone.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp03_zone.hpp @@ -29,121 +29,101 @@ MSGPACK_API_VERSION_NAMESPACE(v1) { class zone { struct finalizer { - finalizer(void (*func)(void*), void* data):m_func(func), m_data(data) {} + finalizer(void (*func)(void*), void* data, finalizer* next): m_func(func), m_data(data), m_next(next) {} void operator()() { m_func(m_data); } void (*m_func)(void*); void* m_data; + finalizer* m_next; }; + struct finalizer_array { - finalizer_array():m_tail(MSGPACK_NULLPTR), m_end(MSGPACK_NULLPTR), m_array(MSGPACK_NULLPTR) {} - void call() { - finalizer* fin = m_tail; - for(; fin != m_array; --fin) (*(fin-1))(); - } + finalizer_array(): m_head(MSGPACK_NULLPTR) {} + ~finalizer_array() { - call(); - ::free(m_array); - } - void clear() { - call(); - m_tail = m_array; + clear(); } - void push(void (*func)(void* data), void* data) - { - finalizer* fin = m_tail; - if(fin == m_end) { - push_expand(func, data); - return; + void clear() { + finalizer* fin = m_head; + finalizer* tmp = MSGPACK_NULLPTR; + while(fin) { + (*fin)(); + tmp = fin; + fin = fin->m_next; + delete tmp; } + m_head = MSGPACK_NULLPTR; + } - fin->m_func = func; - fin->m_data = data; - - ++m_tail; + void push(void (*func)(void* data), void* data) { + m_head = new finalizer(func, data, m_head); } - void push_expand(void (*func)(void*), void* data) { - const size_t nused = static_cast(m_end - m_array); - size_t nnext; - if(nused == 0) { - nnext = (sizeof(finalizer) < 72/2) ? - 72 / sizeof(finalizer) : 8; - } else { - nnext = nused * 2; - } - finalizer* tmp = - static_cast(::realloc(m_array, sizeof(finalizer) * nnext)); - if(!tmp) { - THROW std::bad_alloc(); - } - m_array = tmp; - m_end = tmp + nnext; - m_tail = tmp + nused; - new (m_tail) finalizer(func, data); - ++m_tail; + void pop() { + finalizer* n = m_head->m_next; + delete m_head; + m_head = n; } - finalizer* m_tail; - finalizer* m_end; - finalizer* m_array; + + finalizer* m_head; + private: + finalizer_array(const finalizer_array&); + finalizer_array& operator=(const finalizer_array&); }; + struct chunk { chunk* m_next; }; - struct chunk_list { - chunk_list(size_t chunk_size) - { - chunk* c = static_cast(::malloc(sizeof(chunk) + chunk_size)); - if(!c) { - THROW std::bad_alloc(); - } - m_head = c; - m_free = chunk_size; - m_ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = MSGPACK_NULLPTR; - } - ~chunk_list() - { + struct chunk_list { + chunk_list(size_t chunk_size, char* ptr): m_free(chunk_size), m_ptr(ptr), m_head(MSGPACK_NULLPTR) {} + ~chunk_list() { chunk* c = m_head; while(c) { chunk* n = c->m_next; ::free(c); c = n; } + m_head = MSGPACK_NULLPTR; } - void clear(size_t chunk_size) - { + + void clear(size_t chunk_size, char* ptr) { chunk* c = m_head; - while(true) { + while(c) { chunk* n = c->m_next; - if(n) { - ::free(c); - c = n; - } else { - m_head = c; - break; - } + ::free(c); + c = n; } - m_head->m_next = MSGPACK_NULLPTR; + m_head = MSGPACK_NULLPTR; m_free = chunk_size; - m_ptr = reinterpret_cast(m_head) + sizeof(chunk); + m_ptr = ptr; } + size_t m_free; char* m_ptr; chunk* m_head; + + private: + chunk_list(const chunk_list&); + chunk_list& operator=(const chunk_list&); }; + size_t m_chunk_size; - chunk_list m_chunk_list; + chunk_list* m_chunk_list; finalizer_array m_finalizer_array; public: zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + ~zone(); -public: void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); + void* allocate_no_align(size_t size); + bool allocated() { + return m_chunk_list != MSGPACK_NULLPTR; + } + void push_finalizer(void (*func)(void*), void* data); template @@ -152,24 +132,23 @@ class zone { void clear(); void swap(zone& o); - static void* operator new(std::size_t size) - { + + static void* operator new(std::size_t size) { void* p = ::malloc(size); if (!p) THROW std::bad_alloc(); return p; } - static void operator delete(void *p) /* throw() */ - { + + static void operator delete(void *p) /* throw() */ { ::free(p); } - static void* operator new(std::size_t size, void* place) /* throw() */ - { - return ::operator new(size, place); - } - static void operator delete(void* p, void* place) /* throw() */ - { - ::operator delete(p, place); + + static void* operator new(std::size_t /*size*/, void* mem) /* throw() */ { + return mem; } + + static void operator delete(void * /*p*/, void* /*mem*/) /* throw() */ {} + /// @cond template @@ -233,18 +212,26 @@ class zone { static char* get_aligned(char* ptr, size_t align); + chunk_list& get_chank_lst(); + char* allocate_expand(size_t size); private: zone(const zone&); zone& operator=(const zone&); }; -inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(m_chunk_size) -{ +inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(MSGPACK_NULLPTR) {} + +inline zone::~zone() { + m_finalizer_array.~finalizer_array(); + if(m_chunk_list) { + m_chunk_list->~chunk_list(); + ::free(m_chunk_list); + m_chunk_list = MSGPACK_NULLPTR; + } } -inline char* zone::get_aligned(char* ptr, size_t align) -{ +inline char* zone::get_aligned(char* ptr, size_t align) { MSGPACK_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0) return reinterpret_cast( @@ -252,37 +239,45 @@ inline char* zone::get_aligned(char* ptr, size_t align) ); } -inline void* zone::allocate_align(size_t size, size_t align) -{ - char* aligned = get_aligned(m_chunk_list.m_ptr, align); - size_t adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); - if (m_chunk_list.m_free < adjusted_size) { +inline zone::chunk_list& zone::get_chank_lst() { + if (!m_chunk_list) { + void* ptr = ::malloc(sizeof(chunk_list) + m_chunk_size); + if (!ptr) + THROW std::bad_alloc(); + m_chunk_list = new (ptr) chunk_list(m_chunk_size, reinterpret_cast(ptr) + sizeof(chunk_list)); + } + return *m_chunk_list; +} + +inline void* zone::allocate_align(size_t size, size_t align) { + chunk_list& chank_lst = get_chank_lst(); + char* aligned = get_aligned(chank_lst.m_ptr, align); + size_t adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); + if (chank_lst.m_free < adjusted_size) { size_t enough_size = size + align - 1; char* ptr = allocate_expand(enough_size); aligned = get_aligned(ptr, align); - adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); + adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); } - m_chunk_list.m_free -= adjusted_size; - m_chunk_list.m_ptr += adjusted_size; + chank_lst.m_free -= adjusted_size; + chank_lst.m_ptr += adjusted_size; return aligned; } -inline void* zone::allocate_no_align(size_t size) -{ - char* ptr = m_chunk_list.m_ptr; - if(m_chunk_list.m_free < size) { +inline void* zone::allocate_no_align(size_t size) { + chunk_list& chank_lst = get_chank_lst(); + char* ptr = chank_lst.m_ptr; + if(chank_lst.m_free < size) { ptr = allocate_expand(size); } - m_chunk_list.m_free -= size; - m_chunk_list.m_ptr += size; + chank_lst.m_free -= size; + chank_lst.m_ptr += size; return ptr; } -inline char* zone::allocate_expand(size_t size) -{ - chunk_list* const cl = &m_chunk_list; - +inline char* zone::allocate_expand(size_t size) { + chunk_list& cl = get_chank_lst(); size_t sz = m_chunk_size; while(sz < size) { @@ -299,60 +294,54 @@ inline char* zone::allocate_expand(size_t size) char* ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = cl->m_head; - cl->m_head = c; - cl->m_free = sz; - cl->m_ptr = ptr; + c->m_next = cl.m_head; + cl.m_head = c; + cl.m_free = sz; + cl.m_ptr = ptr; return ptr; } -inline void zone::push_finalizer(void (*func)(void*), void* data) -{ +inline void zone::push_finalizer(void (*func)(void*), void* data) { m_finalizer_array.push(func, data); } template -inline void zone::push_finalizer(msgpack::unique_ptr obj) -{ +inline void zone::push_finalizer(msgpack::unique_ptr obj) { m_finalizer_array.push(&zone::object_delete, obj.release()); } -inline void zone::clear() -{ +inline void zone::clear() { m_finalizer_array.clear(); - m_chunk_list.clear(m_chunk_size); + if (m_chunk_list) { + m_chunk_list->clear(m_chunk_size, reinterpret_cast(m_chunk_list) + sizeof(chunk_list)); + } } -inline void zone::swap(zone& o) -{ +inline void zone::swap(zone& o) { using std::swap; swap(m_chunk_size, o.m_chunk_size); swap(m_chunk_list, o.m_chunk_list); - swap(m_finalizer_array, o.m_finalizer_array); + swap(m_finalizer_array.m_head, o.m_finalizer_array.m_head); } template -void zone::object_destruct(void* obj) -{ - static_cast(obj)->~T(); +void zone::object_delete(void* obj) { + delete static_cast(obj); } template -void zone::object_delete(void* obj) -{ - delete static_cast(obj); +void zone::object_destruct(void* obj) { + static_cast(obj)->~T(); } -inline void zone::undo_allocate(size_t size) -{ - m_chunk_list.m_ptr -= size; - m_chunk_list.m_free += size; +inline void zone::undo_allocate(size_t size) { + chunk_list& cl = get_chank_lst(); + cl.m_ptr -= size; + cl.m_free += size; } -inline std::size_t aligned_size( - std::size_t size, - std::size_t align) { +inline std::size_t aligned_size(std::size_t size, std::size_t align) { return (size + align - 1) / align * align; } @@ -371,7 +360,7 @@ T* zone::allocate() try { return new (x) T(); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -390,7 +379,7 @@ T* zone::allocate(A1 a1) try { return new (x) T(a1); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -409,7 +398,7 @@ T* zone::allocate(A1 a1, A2 a2) try { return new (x) T(a1, a2); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -428,7 +417,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3) try { return new (x) T(a1, a2, a3); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -447,7 +436,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4) try { return new (x) T(a1, a2, a3, a4); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -466,7 +455,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) try { return new (x) T(a1, a2, a3, a4, a5); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -485,7 +474,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) try { return new (x) T(a1, a2, a3, a4, a5, a6); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -504,7 +493,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) try { return new (x) T(a1, a2, a3, a4, a5, a6, a7); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -523,7 +512,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -542,7 +531,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -561,7 +550,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -580,7 +569,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -599,7 +588,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -618,7 +607,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -637,7 +626,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } @@ -656,7 +645,7 @@ T* zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, try { return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp11_zone.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp11_zone.hpp index 0a0658e69da..65572279759 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp11_zone.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/detail/cpp11_zone.hpp @@ -29,154 +29,113 @@ MSGPACK_API_VERSION_NAMESPACE(v1) { class zone { private: struct finalizer { - finalizer(void (*func)(void*), void* data):m_func(func), m_data(data) {} + finalizer(void (*func)(void*), void* data, finalizer* next): m_func(func), m_data(data), m_next(next) {} void operator()() { m_func(m_data); } void (*m_func)(void*); void* m_data; + finalizer* m_next; }; + struct finalizer_array { - finalizer_array():m_tail(MSGPACK_NULLPTR), m_end(MSGPACK_NULLPTR), m_array(MSGPACK_NULLPTR) {} - void call() { - finalizer* fin = m_tail; - for(; fin != m_array; --fin) (*(fin-1))(); - } + finalizer_array(): m_head(MSGPACK_NULLPTR) {} + ~finalizer_array() { - call(); - ::free(m_array); + clear(); } - void clear() { - call(); - m_tail = m_array; - } - void push(void (*func)(void* data), void* data) - { - finalizer* fin = m_tail; - if(fin == m_end) { - push_expand(func, data); - return; + void clear() { + finalizer* fin = m_head; + finalizer* tmp = MSGPACK_NULLPTR; + while(fin) { + (*fin)(); + tmp = fin; + fin = fin->m_next; + delete tmp; } + m_head = MSGPACK_NULLPTR; + } - fin->m_func = func; - fin->m_data = data; - - ++m_tail; + void push(void (*func)(void* data), void* data) { + m_head = new finalizer(func, data, m_head); } - void push_expand(void (*func)(void*), void* data) { - const size_t nused = static_cast(m_end - m_array); - size_t nnext; - if(nused == 0) { - nnext = (sizeof(finalizer) < 72/2) ? - 72 / sizeof(finalizer) : 8; - } else { - nnext = nused * 2; - } - finalizer* tmp = - static_cast(::realloc(m_array, sizeof(finalizer) * nnext)); - if(!tmp) { - THROW std::bad_alloc(); - } - m_array = tmp; - m_end = tmp + nnext; - m_tail = tmp + nused; - new (m_tail) finalizer(func, data); - ++m_tail; + void pop() { + auto n = m_head->m_next; + delete m_head; + m_head = n; } - finalizer_array(finalizer_array&& other) noexcept - :m_tail(other.m_tail), m_end(other.m_end), m_array(other.m_array) - { - other.m_tail = MSGPACK_NULLPTR; - other.m_end = MSGPACK_NULLPTR; - other.m_array = MSGPACK_NULLPTR; + + finalizer_array(finalizer_array&& other) noexcept: m_head(other.m_head) { + other.m_head = MSGPACK_NULLPTR; } - finalizer_array& operator=(finalizer_array&& other) noexcept - { - this->~finalizer_array(); - new (this) finalizer_array(std::move(other)); + + finalizer_array& operator=(finalizer_array&& other) noexcept { + m_head = other.m_head; + other.m_head = MSGPACK_NULLPTR; return *this; } - finalizer* m_tail; - finalizer* m_end; - finalizer* m_array; - private: + finalizer* m_head; finalizer_array(const finalizer_array&); finalizer_array& operator=(const finalizer_array&); }; + struct chunk { chunk* m_next; }; - struct chunk_list { - chunk_list(size_t chunk_size) - { - chunk* c = static_cast(::malloc(sizeof(chunk) + chunk_size)); - if(!c) { - THROW std::bad_alloc(); - } - m_head = c; - m_free = chunk_size; - m_ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = MSGPACK_NULLPTR; - } - ~chunk_list() - { + struct chunk_list { + chunk_list(size_t chunk_size, char* ptr): m_free(chunk_size), m_ptr(ptr), m_head(MSGPACK_NULLPTR) {} + ~chunk_list() { chunk* c = m_head; while(c) { chunk* n = c->m_next; ::free(c); c = n; } + m_head = MSGPACK_NULLPTR; } - void clear(size_t chunk_size) - { + + void clear(size_t chunk_size, char* ptr) { chunk* c = m_head; - while(true) { + while(c) { chunk* n = c->m_next; - if(n) { - ::free(c); - c = n; - } else { - m_head = c; - break; - } + ::free(c); + c = n; } - m_head->m_next = MSGPACK_NULLPTR; + m_head = MSGPACK_NULLPTR; m_free = chunk_size; - m_ptr = reinterpret_cast(m_head) + sizeof(chunk); - } - chunk_list(chunk_list&& other) noexcept - :m_free(other.m_free), m_ptr(other.m_ptr), m_head(other.m_head) - { - other.m_head = MSGPACK_NULLPTR; - } - chunk_list& operator=(chunk_list&& other) noexcept - { - this->~chunk_list(); - new (this) chunk_list(std::move(other)); - return *this; + m_ptr = ptr; } size_t m_free; char* m_ptr; chunk* m_head; + private: + chunk_list(chunk_list&& other) noexcept = delete; + chunk_list& operator=(chunk_list&& other) noexcept = delete; chunk_list(const chunk_list&); chunk_list& operator=(const chunk_list&); }; + size_t m_chunk_size; - chunk_list m_chunk_list; + chunk_list* m_chunk_list{}; finalizer_array m_finalizer_array; public: zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE); + ~zone(); -public: void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); + void* allocate_no_align(size_t size); + bool allocated() { + return m_chunk_list != MSGPACK_NULLPTR; + } + void push_finalizer(void (*func)(void*), void* data); template @@ -186,23 +145,21 @@ class zone { void swap(zone& o); - static void* operator new(std::size_t size) - { + static void* operator new(std::size_t size) { void* p = ::malloc(size); if (!p) THROW std::bad_alloc(); return p; } - static void operator delete(void *p) noexcept - { + + static void operator delete(void *p) noexcept { ::free(p); } - static void* operator new(std::size_t /*size*/, void* mem) noexcept - { + + static void* operator new(std::size_t /*size*/, void* mem) noexcept { return mem; } - static void operator delete(void * /*p*/, void* /*mem*/) noexcept - { - } + + static void operator delete(void * /*p*/, void* /*mem*/) noexcept {} template T* allocate(Args... args); @@ -223,15 +180,23 @@ class zone { static char* get_aligned(char* ptr, size_t align); + chunk_list& get_chank_lst(); + char* allocate_expand(size_t size); }; -inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(m_chunk_size) -{ +inline zone::zone(size_t chunk_size):m_chunk_size(chunk_size), m_chunk_list(MSGPACK_NULLPTR) {} + +inline zone::~zone() { + m_finalizer_array.~finalizer_array(); + if(m_chunk_list) { + m_chunk_list->~chunk_list(); + ::free(m_chunk_list); + m_chunk_list = MSGPACK_NULLPTR; + } } -inline char* zone::get_aligned(char* ptr, size_t align) -{ +inline char* zone::get_aligned(char* ptr, size_t align) { MSGPACK_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0) return reinterpret_cast( @@ -239,37 +204,45 @@ inline char* zone::get_aligned(char* ptr, size_t align) ); } -inline void* zone::allocate_align(size_t size, size_t align) -{ - char* aligned = get_aligned(m_chunk_list.m_ptr, align); - size_t adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); - if (m_chunk_list.m_free < adjusted_size) { +inline zone::chunk_list& zone::get_chank_lst() { + if (!m_chunk_list) { + auto ptr = ::malloc(sizeof(chunk_list) + m_chunk_size); + if (!ptr) + THROW std::bad_alloc(); + m_chunk_list = new (ptr) chunk_list(m_chunk_size, reinterpret_cast(ptr) + sizeof(chunk_list)); + } + return *m_chunk_list; +} + +inline void* zone::allocate_align(size_t size, size_t align) { + chunk_list& chank_lst = get_chank_lst(); + char* aligned = get_aligned(chank_lst.m_ptr, align); + size_t adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); + if (chank_lst.m_free < adjusted_size) { size_t enough_size = size + align - 1; char* ptr = allocate_expand(enough_size); aligned = get_aligned(ptr, align); - adjusted_size = size + static_cast(aligned - m_chunk_list.m_ptr); + adjusted_size = size + static_cast(aligned - chank_lst.m_ptr); } - m_chunk_list.m_free -= adjusted_size; - m_chunk_list.m_ptr += adjusted_size; + chank_lst.m_free -= adjusted_size; + chank_lst.m_ptr += adjusted_size; return aligned; } -inline void* zone::allocate_no_align(size_t size) -{ - char* ptr = m_chunk_list.m_ptr; - if(m_chunk_list.m_free < size) { +inline void* zone::allocate_no_align(size_t size) { + chunk_list& chank_lst = get_chank_lst(); + char* ptr = chank_lst.m_ptr; + if(chank_lst.m_free < size) { ptr = allocate_expand(size); } - m_chunk_list.m_free -= size; - m_chunk_list.m_ptr += size; + chank_lst.m_free -= size; + chank_lst.m_ptr += size; return ptr; } -inline char* zone::allocate_expand(size_t size) -{ - chunk_list* const cl = &m_chunk_list; - +inline char* zone::allocate_expand(size_t size) { + chunk_list& cl = get_chank_lst(); size_t sz = m_chunk_size; while(sz < size) { @@ -286,58 +259,56 @@ inline char* zone::allocate_expand(size_t size) char* ptr = reinterpret_cast(c) + sizeof(chunk); - c->m_next = cl->m_head; - cl->m_head = c; - cl->m_free = sz; - cl->m_ptr = ptr; + c->m_next = cl.m_head; + cl.m_head = c; + cl.m_free = sz; + cl.m_ptr = ptr; return ptr; } -inline void zone::push_finalizer(void (*func)(void*), void* data) -{ +inline void zone::push_finalizer(void (*func)(void*), void* data) { m_finalizer_array.push(func, data); } template -inline void zone::push_finalizer(msgpack::unique_ptr obj) -{ +inline void zone::push_finalizer(msgpack::unique_ptr obj) { m_finalizer_array.push(&zone::object_delete, obj.release()); } -inline void zone::clear() -{ +inline void zone::clear() { m_finalizer_array.clear(); - m_chunk_list.clear(m_chunk_size); + if (m_chunk_list) { + m_chunk_list->clear(m_chunk_size, reinterpret_cast(m_chunk_list) + sizeof(chunk_list)); + } } -inline void zone::swap(zone& o) -{ +inline void zone::swap(zone& o) { std::swap(*this, o); } template -void zone::object_delete(void* obj) -{ +void zone::object_delete(void* obj) { delete static_cast(obj); } template -void zone::object_destruct(void* obj) -{ +void zone::object_destruct(void* obj) { static_cast(obj)->~T(); } -inline void zone::undo_allocate(size_t size) -{ - m_chunk_list.m_ptr -= size; - m_chunk_list.m_free += size; +inline void zone::undo_allocate(size_t size) { + chunk_list& cl = get_chank_lst(); + cl.m_ptr -= size; + cl.m_free += size; } +inline std::size_t aligned_size(std::size_t size, std::size_t align) { + return (size + align - 1) / align * align; +} template -T* zone::allocate(Args... args) -{ +T* zone::allocate(Args... args) { void* x = allocate_align(sizeof(T), MSGPACK_ZONE_ALIGNOF(T)); try { m_finalizer_array.push(&zone::object_destruct, x); @@ -348,18 +319,12 @@ T* zone::allocate(Args... args) try { return new (x) T(args...); } catch (...) { - --m_finalizer_array.m_tail; + m_finalizer_array.pop(); undo_allocate(sizeof(T)); RETHROW; } } -inline std::size_t aligned_size( - std::size_t size, - std::size_t align) { - return (size + align - 1) / align * align; -} - /// @cond } // MSGPACK_API_VERSION_NAMESPACE(v1) /// @endcond diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/object_fwd.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/object_fwd.hpp index 2562b41642b..951dca0e108 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/object_fwd.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/object_fwd.hpp @@ -53,16 +53,16 @@ template struct has_as { private: template - static auto check(U*) -> + static auto check_(U*) -> // Check v1 specialization typename std::is_same< decltype(adaptor::as()(std::declval())), T >::type; template - static std::false_type check(...); + static std::false_type check_(...); public: - using type = decltype(check(MSGPACK_NULLPTR)); + using type = decltype(check_(MSGPACK_NULLPTR)); static constexpr bool value = type::value; }; diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/pack.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/pack.hpp index 7c1d0f6b6e8..41cff85f800 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/pack.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v1/pack.hpp @@ -1138,11 +1138,11 @@ inline packer& packer::pack_unsigned_long_long(unsigned long lon template inline packer& packer::pack_float(float d) { - if(d == d) { // check for nan + if(d == d) { // check for nan // compare d to limits to avoid undefined behaviour if(d >= 0 && d <= float(std::numeric_limits::max()) && d == float(uint64_t(d))) { pack_imp_uint64(uint64_t(d)); - return *this; + return *this; } else if(d < 0 && d >= float(std::numeric_limits::min()) && d == float(int64_t(d))) { pack_imp_int64(int64_t(d)); return *this; @@ -1160,11 +1160,11 @@ inline packer& packer::pack_float(float d) template inline packer& packer::pack_double(double d) { - if(d == d) { // check for nan + if(d == d) { // check for nan // compare d to limits to avoid undefined behaviour if(d >= 0 && d <= double(std::numeric_limits::max()) && d == double(uint64_t(d))) { pack_imp_uint64(uint64_t(d)); - return *this; + return *this; } else if(d < 0 && d >= double(std::numeric_limits::min()) && d == double(int64_t(d))) { pack_imp_int64(int64_t(d)); return *this; diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v2/object_fwd.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v2/object_fwd.hpp index 9c44aae6099..45a63118088 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v2/object_fwd.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v2/object_fwd.hpp @@ -79,7 +79,7 @@ template struct has_as { private: template - static auto check(U*) -> + static auto check_(U*) -> typename std::enable_if< // check v2 specialization std::is_same< @@ -92,9 +92,9 @@ struct has_as { std::true_type >::type; template - static std::false_type check(...); + static std::false_type check_(...); public: - using type = decltype(check(MSGPACK_NULLPTR)); + using type = decltype(check_(MSGPACK_NULLPTR)); static constexpr bool value = type::value; }; diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/v3/object_fwd.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/v3/object_fwd.hpp index d282f1b4a47..a0ed44aebb7 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/v3/object_fwd.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/v3/object_fwd.hpp @@ -36,7 +36,7 @@ template struct has_as { private: template - static auto check(U*) -> + static auto check_(U*) -> typename std::enable_if< // check v3 specialization std::is_same< @@ -52,9 +52,9 @@ struct has_as { std::true_type >::type; template - static std::false_type check(...); + static std::false_type check_(...); public: - using type = decltype(check(MSGPACK_NULLPTR)); + using type = decltype(check_(MSGPACK_NULLPTR)); static constexpr bool value = type::value; }; diff --git a/barretenberg/cpp/src/msgpack-c/include/msgpack/version_master.hpp b/barretenberg/cpp/src/msgpack-c/include/msgpack/version_master.hpp index fdab088941d..ad76f7373a6 100644 --- a/barretenberg/cpp/src/msgpack-c/include/msgpack/version_master.hpp +++ b/barretenberg/cpp/src/msgpack-c/include/msgpack/version_master.hpp @@ -1,3 +1,3 @@ #define MSGPACK_VERSION_MAJOR 6 -#define MSGPACK_VERSION_MINOR 0 +#define MSGPACK_VERSION_MINOR 1 #define MSGPACK_VERSION_REVISION 0 diff --git a/barretenberg/cpp/src/msgpack-c/preprocess.rb b/barretenberg/cpp/src/msgpack-c/preprocess.rb new file mode 100644 index 00000000000..95cd313bd22 --- /dev/null +++ b/barretenberg/cpp/src/msgpack-c/preprocess.rb @@ -0,0 +1,17 @@ +require 'erb' + +files = { + "erb/v1/cpp03_msgpack_tuple_decl.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple_decl.hpp", + "erb/v1/cpp03_msgpack_tuple.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple.hpp", + "erb/v1/cpp03_define_array_decl.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_define_array_decl.hpp", + "erb/v1/cpp03_define_array.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_define_array.hpp", + "erb/v1/cpp03_define_map_decl.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_define_map_decl.hpp", + "erb/v1/cpp03_define_map.hpp" => "include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp", + "erb/v1/cpp03_zone_decl.hpp" => "include/msgpack/v1/detail/cpp03_zone_decl.hpp", + "erb/v1/cpp03_zone.hpp" => "include/msgpack/v1/detail/cpp03_zone.hpp" +} + +files.map { |erb, hpp| + res = ERB.new(File.open(erb+".erb").read).result + File.write(hpp, res) +} \ No newline at end of file diff --git a/barretenberg/cpp/src/msgpack-c/test/CMakeLists.txt b/barretenberg/cpp/src/msgpack-c/test/CMakeLists.txt index e0ebf2583ce..a0233dd3557 100644 --- a/barretenberg/cpp/src/msgpack-c/test/CMakeLists.txt +++ b/barretenberg/cpp/src/msgpack-c/test/CMakeLists.txt @@ -1,6 +1,6 @@ FIND_PACKAGE (Threads REQUIRED) FIND_PACKAGE (ZLIB) -FIND_PACKAGE (Boost REQUIRED COMPONENTS unit_test_framework system) +FIND_PACKAGE (Boost REQUIRED COMPONENTS unit_test_framework) LIST (APPEND check_PROGRAMS array_ref.cpp @@ -79,7 +79,6 @@ FOREACH (source_file ${check_PROGRAMS}) TARGET_LINK_LIBRARIES (${source_file_we} msgpack-cxx - Boost::system Boost::unit_test_framework Threads::Threads ZLIB::ZLIB diff --git a/barretenberg/cpp/src/msgpack-c/test/boost_variant.cpp b/barretenberg/cpp/src/msgpack-c/test/boost_variant.cpp index 9ab3dc611e7..647fa474104 100644 --- a/barretenberg/cpp/src/msgpack-c/test/boost_variant.cpp +++ b/barretenberg/cpp/src/msgpack-c/test/boost_variant.cpp @@ -264,7 +264,7 @@ BOOST_AUTO_TEST_CASE(pack_convert_variant_float) BOOST_CHECK(val2.is_double()); BOOST_CHECK(fabs(12.34 - val2.as_double()) <= kEPS); BOOST_CHECK_NO_THROW(boost::get(val2)); - BOOST_CHECK(fabs(val2.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(fabs(val1.as_double() - val2.as_double()) <= kEPS); } BOOST_AUTO_TEST_CASE(object_variant_float) @@ -277,7 +277,8 @@ BOOST_AUTO_TEST_CASE(object_variant_float) BOOST_CHECK(val2.is_double()); BOOST_CHECK(fabs(12.34 - val2.as_double()) <= kEPS); BOOST_CHECK_NO_THROW(boost::get(val2)); - BOOST_CHECK(fabs(val2.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(fabs(val1.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(val1 == val2); } BOOST_AUTO_TEST_CASE(object_with_zone_variant_float) @@ -291,7 +292,116 @@ BOOST_AUTO_TEST_CASE(object_with_zone_variant_float) BOOST_CHECK(val2.is_double()); BOOST_CHECK(fabs(12.34 - val2.as_double()) <= kEPS); BOOST_CHECK_NO_THROW(boost::get(val2)); - BOOST_CHECK(fabs(val2.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(fabs(val1.as_double() - val2.as_double()) <= kEPS); + BOOST_CHECK(val1 == val2); +} + +BOOST_AUTO_TEST_CASE(pack_convert_variant_float_zero_atdp_positive) +{ + std::stringstream ss; + msgpack::type::variant val1 = 12.0; + BOOST_CHECK(val1.is_uint64_t()); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), 12); + BOOST_CHECK(fabs(12.0 - val1.as_double()) <= kEPS); + + msgpack::pack(ss, val1); + + std::string const& str = ss.str(); + msgpack::object_handle oh = + msgpack::unpack(str.data(), str.size()); + msgpack::type::variant val2 = oh.get().as(); + BOOST_CHECK(val2.is_uint64_t()); + BOOST_CHECK_EQUAL(val2.as_uint64_t(), 12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), val2.as_uint64_t()); +} + +BOOST_AUTO_TEST_CASE(object_variant_float_zero_atdp_positive) +{ + msgpack::type::variant val1 = 12.0; + BOOST_CHECK(val1.is_uint64_t()); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), 12); + BOOST_CHECK(fabs(12.0 - val1.as_double()) <= kEPS); + msgpack::object obj(val1); + msgpack::type::variant val2 = obj.as(); + BOOST_CHECK(val2.is_uint64_t()); + BOOST_CHECK_EQUAL(val2.as_uint64_t(), 12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), val2.as_uint64_t()); + BOOST_CHECK(val1 == val2); +} + +BOOST_AUTO_TEST_CASE(object_with_zone_variant_float_zero_atdp_positive) +{ + msgpack::zone z; + msgpack::type::variant val1 = 12.0; + BOOST_CHECK(val1.is_uint64_t()); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), 12); + BOOST_CHECK(fabs(12.0 - val1.as_double()) <= kEPS); + msgpack::object obj(val1, z); + msgpack::type::variant val2 = obj.as(); + BOOST_CHECK(val2.is_uint64_t()); + BOOST_CHECK_EQUAL(val2.as_uint64_t(), 12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK_EQUAL(val1.as_uint64_t(), val2.as_uint64_t()); + BOOST_CHECK(fabs(12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK(val1 == val2); +} + +BOOST_AUTO_TEST_CASE(pack_convert_variant_float_zero_atdp_negative) +{ + std::stringstream ss; + msgpack::type::variant val1 = -12.0; + BOOST_CHECK(val1.is_int64_t()); + BOOST_CHECK_EQUAL(val1.as_int64_t(), -12); + BOOST_CHECK(fabs(-12.0 - val1.as_double()) <= kEPS); + + msgpack::pack(ss, val1); + + std::string const& str = ss.str(); + msgpack::object_handle oh = + msgpack::unpack(str.data(), str.size()); + msgpack::type::variant val2 = oh.get().as(); + BOOST_CHECK(val2.is_int64_t()); + BOOST_CHECK_EQUAL(val2.as_int64_t(), -12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(-12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_int64_t(), val2.as_int64_t()); +} + +BOOST_AUTO_TEST_CASE(object_variant_float_zero_atdp_negative) +{ + msgpack::type::variant val1 = -12.0; + BOOST_CHECK(val1.is_int64_t()); + BOOST_CHECK_EQUAL(val1.as_int64_t(), -12); + BOOST_CHECK(fabs(-12.0 - val1.as_double()) <= kEPS); + msgpack::object obj(val1); + msgpack::type::variant val2 = obj.as(); + BOOST_CHECK(val2.is_int64_t()); + BOOST_CHECK_EQUAL(val2.as_int64_t(), -12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(-12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_int64_t(), val2.as_int64_t()); + BOOST_CHECK(val1 == val2); +} + +BOOST_AUTO_TEST_CASE(object_with_zone_variant_float_zero_atdp_negative) +{ + msgpack::zone z; + msgpack::type::variant val1 = -12.0; + BOOST_CHECK(val1.is_int64_t()); + BOOST_CHECK_EQUAL(val1.as_int64_t(), -12); + BOOST_CHECK(fabs(-12.0 - val1.as_double()) <= kEPS); + msgpack::object obj(val1, z); + msgpack::type::variant val2 = obj.as(); + BOOST_CHECK(val2.is_int64_t()); + BOOST_CHECK_EQUAL(val2.as_int64_t(), -12); + BOOST_CHECK_NO_THROW(boost::get(val2)); + BOOST_CHECK(fabs(-12.0 - val2.as_double()) <= kEPS); + BOOST_CHECK_EQUAL(val1.as_int64_t(), val2.as_int64_t()); + BOOST_CHECK(val1 == val2); } // str diff --git a/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp11.cpp b/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp11.cpp index 2a856cb8ba8..3375a27bfdd 100644 --- a/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp11.cpp +++ b/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp11.cpp @@ -252,6 +252,9 @@ struct equal_to : std::equal_to { template struct set_allocator : std::allocator { using std::allocator::allocator; + + template + struct rebind { using other = set_allocator; }; }; // C++ named requirement Allocator implies that the first template type @@ -262,6 +265,9 @@ struct set_allocator : std::allocator { template struct map_allocator_impl : std::allocator { using std::allocator::allocator; + + template + struct rebind { using other = map_allocator_impl; }; }; template @@ -270,6 +276,9 @@ using map_allocator = map_allocator_impl>; template struct allocator : std::allocator { using std::allocator::allocator; + + template + struct rebind { using other = allocator; }; }; } // namespace test @@ -861,8 +870,6 @@ BOOST_AUTO_TEST_CASE(no_def_con_array_simple_buffer) BOOST_CHECK(val1 == val2); } -#if !defined(MSGPACK_NO_BOOST) - BOOST_AUTO_TEST_CASE(system_clock) { std::chrono::system_clock::time_point val1; @@ -1428,8 +1435,6 @@ BOOST_AUTO_TEST_CASE(high_resolution_clock_impl_now) BOOST_CHECK(val1 == val3); } -#endif // !defined(MSGPACK_NO_BOOST) - BOOST_AUTO_TEST_CASE(timespec_pack_convert_zero) { diff --git a/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp17.cpp b/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp17.cpp index c90a55ad4ae..b20c04ccd7e 100644 --- a/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp17.cpp +++ b/barretenberg/cpp/src/msgpack-c/test/msgpack_cpp17.cpp @@ -461,4 +461,30 @@ BOOST_AUTO_TEST_CASE(carray_byte_object_with_zone) } } +#if defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) + +BOOST_AUTO_TEST_CASE(variant_pack_unpack_as) { + std::stringstream ss; + std::variant val1{1.0}; + msgpack::pack(ss, val1); + std::string const& str = ss.str(); + msgpack::object_handle oh = + msgpack::unpack(str.data(), str.size()); + std::variant val2 = + oh.get().as >(); + BOOST_CHECK(val1 == val2); + BOOST_CHECK_THROW((oh.get().as>()), msgpack::type_error); +} + +BOOST_AUTO_TEST_CASE(variant_with_zone) { + msgpack::zone z; + std::variant val1{1.0}; + msgpack::object obj(val1, z); + std::variant val2 = obj.as>(); + BOOST_CHECK(val1 == val2); + BOOST_CHECK_THROW((obj.as>()), msgpack::type_error); +} + +#endif // defined(MSGPACK_USE_STD_VARIANT_ADAPTOR) + #endif // MSGPACK_CPP_VERSION >= 201703 diff --git a/yarn-project/circuits.js/src/cbind/circuits.gen.ts b/yarn-project/circuits.js/src/cbind/circuits.gen.ts index ab26c840d22..b56887f7d1f 100644 --- a/yarn-project/circuits.js/src/cbind/circuits.gen.ts +++ b/yarn-project/circuits.js/src/cbind/circuits.gen.ts @@ -61,7 +61,6 @@ import { TxContext, TxRequest, VerificationKeyData, - isCircuitError, toBuffer, } from './types.js'; @@ -3236,8 +3235,10 @@ export function privateKernelSimInit( wasm: IWasmModule, arg0: PrivateKernelInputsInit, ): CircuitError | KernelCircuitPublicInputs { - return ((v: MsgpackCircuitError | MsgpackKernelCircuitPublicInputs) => - isCircuitError(v) ? toCircuitError(v) : toKernelCircuitPublicInputs(v))( + return ((v: [number, MsgpackCircuitError | MsgpackKernelCircuitPublicInputs]) => + v[0] == 0 + ? toCircuitError(v[1] as MsgpackCircuitError) + : toKernelCircuitPublicInputs(v[1] as MsgpackKernelCircuitPublicInputs))( callCbind(wasm, 'private_kernel__sim_init', [fromPrivateKernelInputsInit(arg0)]), ); } @@ -3245,8 +3246,10 @@ export function privateKernelSimInner( wasm: IWasmModule, arg0: PrivateKernelInputsInner, ): CircuitError | KernelCircuitPublicInputs { - return ((v: MsgpackCircuitError | MsgpackKernelCircuitPublicInputs) => - isCircuitError(v) ? toCircuitError(v) : toKernelCircuitPublicInputs(v))( + return ((v: [number, MsgpackCircuitError | MsgpackKernelCircuitPublicInputs]) => + v[0] == 0 + ? toCircuitError(v[1] as MsgpackCircuitError) + : toKernelCircuitPublicInputs(v[1] as MsgpackKernelCircuitPublicInputs))( callCbind(wasm, 'private_kernel__sim_inner', [fromPrivateKernelInputsInner(arg0)]), ); } @@ -3254,20 +3257,26 @@ export function privateKernelSimOrdering( wasm: IWasmModule, arg0: PrivateKernelInputsOrdering, ): CircuitError | KernelCircuitPublicInputsFinal { - return ((v: MsgpackCircuitError | MsgpackKernelCircuitPublicInputsFinal) => - isCircuitError(v) ? toCircuitError(v) : toKernelCircuitPublicInputsFinal(v))( + return ((v: [number, MsgpackCircuitError | MsgpackKernelCircuitPublicInputsFinal]) => + v[0] == 0 + ? toCircuitError(v[1] as MsgpackCircuitError) + : toKernelCircuitPublicInputsFinal(v[1] as MsgpackKernelCircuitPublicInputsFinal))( callCbind(wasm, 'private_kernel__sim_ordering', [fromPrivateKernelInputsOrdering(arg0)]), ); } export function publicKernelSim(wasm: IWasmModule, arg0: PublicKernelInputs): CircuitError | KernelCircuitPublicInputs { - return ((v: MsgpackCircuitError | MsgpackKernelCircuitPublicInputs) => - isCircuitError(v) ? toCircuitError(v) : toKernelCircuitPublicInputs(v))( + return ((v: [number, MsgpackCircuitError | MsgpackKernelCircuitPublicInputs]) => + v[0] == 0 + ? toCircuitError(v[1] as MsgpackCircuitError) + : toKernelCircuitPublicInputs(v[1] as MsgpackKernelCircuitPublicInputs))( callCbind(wasm, 'public_kernel__sim', [fromPublicKernelInputs(arg0)]), ); } export function baseRollupSim(wasm: IWasmModule, arg0: BaseRollupInputs): CircuitError | BaseOrMergeRollupPublicInputs { - return ((v: MsgpackCircuitError | MsgpackBaseOrMergeRollupPublicInputs) => - isCircuitError(v) ? toCircuitError(v) : toBaseOrMergeRollupPublicInputs(v))( + return ((v: [number, MsgpackCircuitError | MsgpackBaseOrMergeRollupPublicInputs]) => + v[0] == 0 + ? toCircuitError(v[1] as MsgpackCircuitError) + : toBaseOrMergeRollupPublicInputs(v[1] as MsgpackBaseOrMergeRollupPublicInputs))( callCbind(wasm, 'base_rollup__sim', [fromBaseRollupInputs(arg0)]), ); } @@ -3275,14 +3284,18 @@ export function mergeRollupSim( wasm: IWasmModule, arg0: MergeRollupInputs, ): CircuitError | BaseOrMergeRollupPublicInputs { - return ((v: MsgpackCircuitError | MsgpackBaseOrMergeRollupPublicInputs) => - isCircuitError(v) ? toCircuitError(v) : toBaseOrMergeRollupPublicInputs(v))( + return ((v: [number, MsgpackCircuitError | MsgpackBaseOrMergeRollupPublicInputs]) => + v[0] == 0 + ? toCircuitError(v[1] as MsgpackCircuitError) + : toBaseOrMergeRollupPublicInputs(v[1] as MsgpackBaseOrMergeRollupPublicInputs))( callCbind(wasm, 'merge_rollup__sim', [fromMergeRollupInputs(arg0)]), ); } export function rootRollupSim(wasm: IWasmModule, arg0: RootRollupInputs): CircuitError | RootRollupPublicInputs { - return ((v: MsgpackCircuitError | MsgpackRootRollupPublicInputs) => - isCircuitError(v) ? toCircuitError(v) : toRootRollupPublicInputs(v))( + return ((v: [number, MsgpackCircuitError | MsgpackRootRollupPublicInputs]) => + v[0] == 0 + ? toCircuitError(v[1] as MsgpackCircuitError) + : toRootRollupPublicInputs(v[1] as MsgpackRootRollupPublicInputs))( callCbind(wasm, 'root_rollup__sim', [fromRootRollupInputs(arg0)]), ); } diff --git a/yarn-project/circuits.js/src/cbind/compiler.ts b/yarn-project/circuits.js/src/cbind/compiler.ts index 832ee9d925c..46dff5bec47 100644 --- a/yarn-project/circuits.js/src/cbind/compiler.ts +++ b/yarn-project/circuits.js/src/cbind/compiler.ts @@ -78,13 +78,6 @@ export interface TypeInfo { * If so, stores the variant's subtype elements. */ variantSubtypes?: TypeInfo[]; - /** - * Was this used in a variant type? - * Typically a variant in C++ will have an easy to distinguish type as - * one of two structs e.g. [Error, T]. In that case, a isError method would be imported. Only if a third type was - * added would we need to distinguish T as well. - */ - usedInDiscriminatedVariant?: boolean; /** * Key-value pair of types that represent the keys and values in a map schema. */ @@ -136,14 +129,17 @@ function msgpackConverterExpr(typeInfo: TypeInfo, value: string): string { } } else if (typeInfo.variantSubtypes) { const { variantSubtypes } = typeInfo; - // Handle the last variant type: just assume it is this type... - let expr = msgpackConverterExpr(variantSubtypes[variantSubtypes.length - 1], 'v'); - // ... because we check every other type: + // Handle the last variant type: we assume it is this type after checking everything else + let expr = msgpackConverterExpr( + variantSubtypes[variantSubtypes.length - 1], + 'v[1] as ' + variantSubtypes[variantSubtypes.length - 1].msgpackTypeName, + ); for (let i = 0; i < variantSubtypes.length - 1; i++) { - // mark this as needing an import - variantSubtypes[i].usedInDiscriminatedVariant = true; // make the expr a compound expression with a discriminator - expr = `(is${variantSubtypes[i].typeName}(v) ? ${msgpackConverterExpr(variantSubtypes[i], 'v')} : ${expr})`; + expr = `(v[0] == ${i} ? ${msgpackConverterExpr( + variantSubtypes[i], + 'v[1] as ' + variantSubtypes[i].msgpackTypeName, + )} : ${expr})`; } return `((v: ${typeInfo.msgpackTypeName}) => ${expr})(${value})`; } else if (typeInfo.mapSubtypes) { @@ -186,7 +182,7 @@ function classConverterExpr(typeInfo: TypeInfo, value: string): string { return `${value}.map(${convFn})`; } } else if (typeInfo.variantSubtypes) { - throw new Error('TODO - variant parameters to C++ not yet supported'); + throw new Error('TODO(AD) - variant parameters to C++ not yet supported.'); } else if (typeInfo.mapSubtypes) { const { typeName } = typeInfo.mapSubtypes[1]; const convFn = `(v: ${typeName}) => ${classConverterExpr(typeInfo.mapSubtypes[1], 'v')}`; @@ -239,8 +235,10 @@ export class CbindCompiler { } else if (type[0] === 'variant') { // fixed-size array case const [_array, variantSchemas] = type; + // TODO(AD): This could be a discriminated union if we also allow writing C++ variants. const typeName = variantSchemas.map(vs => this.getTypeName(vs)).join(' | '); - const msgpackTypeName = variantSchemas.map(vs => this.getMsgpackTypename(vs)).join(' | '); + const msgpackUnion = variantSchemas.map(vs => this.getMsgpackTypename(vs)).join(' | '); + const msgpackTypeName = `[number, ${msgpackUnion}]`; return { typeName, msgpackTypeName, @@ -495,9 +493,6 @@ import { IWasmModule } from '@aztec/foundation/wasm'; if (typeInfo.isImport) { imports.push(typeInfo.typeName); } - if (typeInfo.usedInDiscriminatedVariant) { - imports.push(`is${typeInfo.typeName}`); - } if (typeInfo.declaration) { outputs.push(typeInfo.declaration); outputs.push('\n'); diff --git a/yarn-project/circuits.js/src/cbind/types.ts b/yarn-project/circuits.js/src/cbind/types.ts index e30ef43cc1f..3900db0c5a3 100644 --- a/yarn-project/circuits.js/src/cbind/types.ts +++ b/yarn-project/circuits.js/src/cbind/types.ts @@ -139,25 +139,3 @@ export type RawPointerProverBase = number & { */ __RawPointerProverBaseBrand: any; }; - -/** - * Type representing raw error messages returned by circuits. - */ -type RawError = { - /** - * An error code representing the specific issue encountered by the circuit. - */ - code: number; - /** - * A descriptive error message providing details about the encountered issue in the circuit. - */ - message: string; -}; - -/** - * Distinguisher function for union types. - * @param v - the distinguished type. - */ -export function isCircuitError(v: any): v is RawError { - return v.code !== undefined && v.message !== undefined; -}