Line data Source code
1 : #ifdef __PLUMED_HAS_ASMJIT 2 : // This file is part of AsmJit project <https://asmjit.com> 3 : // 4 : // See asmjit.h or LICENSE.md for license and copyright information 5 : // SPDX-License-Identifier: Zlib 6 : 7 : #ifndef ASMJIT_CORE_ZONESTRING_H_INCLUDED 8 : #define ASMJIT_CORE_ZONESTRING_H_INCLUDED 9 : 10 : #include "globals.h" 11 : #include "zone.h" 12 : 13 : ASMJIT_BEGIN_NAMESPACE 14 : 15 : //! \addtogroup asmjit_zone 16 : //! \{ 17 : 18 : //! A helper class used by \ref ZoneString implementation. 19 : struct ZoneStringBase { 20 : union { 21 : struct { 22 : uint32_t _size; 23 : char _embedded[sizeof(void*) * 2 - 4]; 24 : }; 25 : struct { 26 : void* _dummy; 27 : char* _external; 28 : }; 29 : }; 30 : 31 : inline void reset() noexcept { 32 273777 : _dummy = nullptr; 33 273777 : _external = nullptr; 34 : } 35 : 36 273777 : Error setData(Zone* zone, uint32_t maxEmbeddedSize, const char* str, size_t size) noexcept { 37 273777 : if (size == SIZE_MAX) 38 0 : size = strlen(str); 39 : 40 273777 : if (size <= maxEmbeddedSize) { 41 273777 : memcpy(_embedded, str, size); 42 273777 : _embedded[size] = '\0'; 43 : } 44 : else { 45 0 : char* external = static_cast<char*>(zone->dup(str, size, true)); 46 0 : if (ASMJIT_UNLIKELY(!external)) 47 : return DebugUtils::errored(kErrorOutOfMemory); 48 0 : _external = external; 49 : } 50 : 51 273777 : _size = uint32_t(size); 52 273777 : return kErrorOk; 53 : } 54 : }; 55 : 56 : //! A string template that can be zone allocated. 57 : //! 58 : //! Helps with creating strings that can be either statically allocated if they are small, or externally allocated 59 : //! in case their size exceeds the limit. The `N` represents the size of the whole `ZoneString` structure, based on 60 : //! that size the maximum size of the internal buffer is determined. 61 : template<size_t N> 62 : class ZoneString { 63 : public: 64 : //! \name Constants 65 : //! \{ 66 : 67 : enum : uint32_t { 68 : kWholeSize = (N > sizeof(ZoneStringBase)) ? uint32_t(N) : uint32_t(sizeof(ZoneStringBase)), 69 : kMaxEmbeddedSize = kWholeSize - 5 70 : }; 71 : 72 : //! \} 73 : 74 : //! \name Members 75 : //! \{ 76 : 77 : union { 78 : ZoneStringBase _base; 79 : char _wholeData[kWholeSize]; 80 : }; 81 : 82 : //! \} 83 : 84 : //! \name Construction & Destruction 85 : //! \{ 86 : 87 273777 : inline ZoneString() noexcept { reset(); } 88 : inline void reset() noexcept { _base.reset(); } 89 : 90 : //! \} 91 : 92 : //! \name Accessors 93 : //! \{ 94 : 95 : //! Tests whether the string is empty. 96 0 : inline bool empty() const noexcept { return _base._size == 0; } 97 : 98 : //! Returns the string data. 99 0 : inline const char* data() const noexcept { return _base._size <= kMaxEmbeddedSize ? _base._embedded : _base._external; } 100 : //! Returns the string size. 101 271314 : inline uint32_t size() const noexcept { return _base._size; } 102 : 103 : //! Tests whether the string is embedded (e.g. no dynamically allocated). 104 : inline bool isEmbedded() const noexcept { return _base._size <= kMaxEmbeddedSize; } 105 : 106 : //! Copies a new `data` of the given `size` to the string. 107 : //! 108 : //! If the `size` exceeds the internal buffer the given `zone` will be used to duplicate the data, otherwise 109 : //! the internal buffer will be used as a storage. 110 : inline Error setData(Zone* zone, const char* data, size_t size) noexcept { 111 273777 : return _base.setData(zone, kMaxEmbeddedSize, data, size); 112 : } 113 : 114 : //! \} 115 : }; 116 : 117 : //! \} 118 : 119 : ASMJIT_END_NAMESPACE 120 : 121 : #endif // ASMJIT_CORE_ZONESTRING_H_INCLUDED 122 : 123 : #endif // __PLUMED_HAS_ASMJIT