spirv_cross_containers.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * Copyright 2019-2021 Hans-Kristian Arntzen
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * At your option, you may choose to accept this material under either:
  18. * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
  19. * 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
  20. * SPDX-License-Identifier: Apache-2.0 OR MIT.
  21. */
  22. #ifndef SPIRV_CROSS_CONTAINERS_HPP
  23. #define SPIRV_CROSS_CONTAINERS_HPP
  24. #include "spirv_cross_error_handling.hpp"
  25. #include <algorithm>
  26. #include <functional>
  27. #include <iterator>
  28. #include <limits>
  29. #include <memory>
  30. #include <stack>
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <type_traits>
  36. #include <unordered_map>
  37. #include <unordered_set>
  38. #include <utility>
  39. #include <vector>
  40. #ifdef SPIRV_CROSS_NAMESPACE_OVERRIDE
  41. #define SPIRV_CROSS_NAMESPACE SPIRV_CROSS_NAMESPACE_OVERRIDE
  42. #else
  43. #define SPIRV_CROSS_NAMESPACE spirv_cross
  44. #endif
  45. namespace SPIRV_CROSS_NAMESPACE
  46. {
  47. #ifndef SPIRV_CROSS_FORCE_STL_TYPES
  48. // std::aligned_storage does not support size == 0, so roll our own.
  49. template <typename T, size_t N>
  50. class AlignedBuffer
  51. {
  52. public:
  53. T *data()
  54. {
  55. #if defined(_MSC_VER) && _MSC_VER < 1900
  56. // MSVC 2013 workarounds, sigh ...
  57. // Only use this workaround on MSVC 2013 due to some confusion around default initialized unions.
  58. // Spec seems to suggest the memory will be zero-initialized, which is *not* what we want.
  59. return reinterpret_cast<T *>(u.aligned_char);
  60. #else
  61. return reinterpret_cast<T *>(aligned_char);
  62. #endif
  63. }
  64. private:
  65. #if defined(_MSC_VER) && _MSC_VER < 1900
  66. // MSVC 2013 workarounds, sigh ...
  67. union
  68. {
  69. char aligned_char[sizeof(T) * N];
  70. double dummy_aligner;
  71. } u;
  72. #else
  73. alignas(T) char aligned_char[sizeof(T) * N];
  74. #endif
  75. };
  76. template <typename T>
  77. class AlignedBuffer<T, 0>
  78. {
  79. public:
  80. T *data()
  81. {
  82. return nullptr;
  83. }
  84. };
  85. // An immutable version of SmallVector which erases type information about storage.
  86. template <typename T>
  87. class VectorView
  88. {
  89. public:
  90. T &operator[](size_t i) SPIRV_CROSS_NOEXCEPT
  91. {
  92. return ptr[i];
  93. }
  94. const T &operator[](size_t i) const SPIRV_CROSS_NOEXCEPT
  95. {
  96. return ptr[i];
  97. }
  98. bool empty() const SPIRV_CROSS_NOEXCEPT
  99. {
  100. return buffer_size == 0;
  101. }
  102. size_t size() const SPIRV_CROSS_NOEXCEPT
  103. {
  104. return buffer_size;
  105. }
  106. T *data() SPIRV_CROSS_NOEXCEPT
  107. {
  108. return ptr;
  109. }
  110. const T *data() const SPIRV_CROSS_NOEXCEPT
  111. {
  112. return ptr;
  113. }
  114. T *begin() SPIRV_CROSS_NOEXCEPT
  115. {
  116. return ptr;
  117. }
  118. T *end() SPIRV_CROSS_NOEXCEPT
  119. {
  120. return ptr + buffer_size;
  121. }
  122. const T *begin() const SPIRV_CROSS_NOEXCEPT
  123. {
  124. return ptr;
  125. }
  126. const T *end() const SPIRV_CROSS_NOEXCEPT
  127. {
  128. return ptr + buffer_size;
  129. }
  130. T &front() SPIRV_CROSS_NOEXCEPT
  131. {
  132. return ptr[0];
  133. }
  134. const T &front() const SPIRV_CROSS_NOEXCEPT
  135. {
  136. return ptr[0];
  137. }
  138. T &back() SPIRV_CROSS_NOEXCEPT
  139. {
  140. return ptr[buffer_size - 1];
  141. }
  142. const T &back() const SPIRV_CROSS_NOEXCEPT
  143. {
  144. return ptr[buffer_size - 1];
  145. }
  146. // Makes it easier to consume SmallVector.
  147. #if defined(_MSC_VER) && _MSC_VER < 1900
  148. explicit operator std::vector<T>() const
  149. {
  150. // Another MSVC 2013 workaround. It does not understand lvalue/rvalue qualified operations.
  151. return std::vector<T>(ptr, ptr + buffer_size);
  152. }
  153. #else
  154. // Makes it easier to consume SmallVector.
  155. explicit operator std::vector<T>() const &
  156. {
  157. return std::vector<T>(ptr, ptr + buffer_size);
  158. }
  159. // If we are converting as an r-value, we can pilfer our elements.
  160. explicit operator std::vector<T>() &&
  161. {
  162. return std::vector<T>(std::make_move_iterator(ptr), std::make_move_iterator(ptr + buffer_size));
  163. }
  164. #endif
  165. // Avoid sliced copies. Base class should only be read as a reference.
  166. VectorView(const VectorView &) = delete;
  167. void operator=(const VectorView &) = delete;
  168. protected:
  169. VectorView() = default;
  170. T *ptr = nullptr;
  171. size_t buffer_size = 0;
  172. };
  173. // Simple vector which supports up to N elements inline, without malloc/free.
  174. // We use a lot of throwaway vectors all over the place which triggers allocations.
  175. // This class only implements the subset of std::vector we need in SPIRV-Cross.
  176. // It is *NOT* a drop-in replacement in general projects.
  177. template <typename T, size_t N = 8>
  178. class SmallVector : public VectorView<T>
  179. {
  180. public:
  181. SmallVector() SPIRV_CROSS_NOEXCEPT
  182. {
  183. this->ptr = stack_storage.data();
  184. buffer_capacity = N;
  185. }
  186. SmallVector(const T *arg_list_begin, const T *arg_list_end) SPIRV_CROSS_NOEXCEPT : SmallVector()
  187. {
  188. auto count = size_t(arg_list_end - arg_list_begin);
  189. reserve(count);
  190. for (size_t i = 0; i < count; i++, arg_list_begin++)
  191. new (&this->ptr[i]) T(*arg_list_begin);
  192. this->buffer_size = count;
  193. }
  194. SmallVector(std::initializer_list<T> init) SPIRV_CROSS_NOEXCEPT : SmallVector(init.begin(), init.end())
  195. {
  196. }
  197. SmallVector(SmallVector &&other) SPIRV_CROSS_NOEXCEPT : SmallVector()
  198. {
  199. *this = std::move(other);
  200. }
  201. SmallVector &operator=(SmallVector &&other) SPIRV_CROSS_NOEXCEPT
  202. {
  203. clear();
  204. if (other.ptr != other.stack_storage.data())
  205. {
  206. // Pilfer allocated pointer.
  207. if (this->ptr != stack_storage.data())
  208. free(this->ptr);
  209. this->ptr = other.ptr;
  210. this->buffer_size = other.buffer_size;
  211. buffer_capacity = other.buffer_capacity;
  212. other.ptr = nullptr;
  213. other.buffer_size = 0;
  214. other.buffer_capacity = 0;
  215. }
  216. else
  217. {
  218. // Need to move the stack contents individually.
  219. reserve(other.buffer_size);
  220. for (size_t i = 0; i < other.buffer_size; i++)
  221. {
  222. new (&this->ptr[i]) T(std::move(other.ptr[i]));
  223. other.ptr[i].~T();
  224. }
  225. this->buffer_size = other.buffer_size;
  226. other.buffer_size = 0;
  227. }
  228. return *this;
  229. }
  230. SmallVector(const SmallVector &other) SPIRV_CROSS_NOEXCEPT : SmallVector()
  231. {
  232. *this = other;
  233. }
  234. SmallVector &operator=(const SmallVector &other) SPIRV_CROSS_NOEXCEPT
  235. {
  236. if (this == &other)
  237. return *this;
  238. clear();
  239. reserve(other.buffer_size);
  240. for (size_t i = 0; i < other.buffer_size; i++)
  241. new (&this->ptr[i]) T(other.ptr[i]);
  242. this->buffer_size = other.buffer_size;
  243. return *this;
  244. }
  245. explicit SmallVector(size_t count) SPIRV_CROSS_NOEXCEPT : SmallVector()
  246. {
  247. resize(count);
  248. }
  249. ~SmallVector()
  250. {
  251. clear();
  252. if (this->ptr != stack_storage.data())
  253. free(this->ptr);
  254. }
  255. void clear() SPIRV_CROSS_NOEXCEPT
  256. {
  257. for (size_t i = 0; i < this->buffer_size; i++)
  258. this->ptr[i].~T();
  259. this->buffer_size = 0;
  260. }
  261. void push_back(const T &t) SPIRV_CROSS_NOEXCEPT
  262. {
  263. reserve(this->buffer_size + 1);
  264. new (&this->ptr[this->buffer_size]) T(t);
  265. this->buffer_size++;
  266. }
  267. void push_back(T &&t) SPIRV_CROSS_NOEXCEPT
  268. {
  269. reserve(this->buffer_size + 1);
  270. new (&this->ptr[this->buffer_size]) T(std::move(t));
  271. this->buffer_size++;
  272. }
  273. void pop_back() SPIRV_CROSS_NOEXCEPT
  274. {
  275. // Work around false positive warning on GCC 8.3.
  276. // Calling pop_back on empty vector is undefined.
  277. if (!this->empty())
  278. resize(this->buffer_size - 1);
  279. }
  280. template <typename... Ts>
  281. void emplace_back(Ts &&... ts) SPIRV_CROSS_NOEXCEPT
  282. {
  283. reserve(this->buffer_size + 1);
  284. new (&this->ptr[this->buffer_size]) T(std::forward<Ts>(ts)...);
  285. this->buffer_size++;
  286. }
  287. void reserve(size_t count) SPIRV_CROSS_NOEXCEPT
  288. {
  289. if ((count > std::numeric_limits<size_t>::max() / sizeof(T)) ||
  290. (count > std::numeric_limits<size_t>::max() / 2))
  291. {
  292. // Only way this should ever happen is with garbage input, terminate.
  293. std::terminate();
  294. }
  295. if (count > buffer_capacity)
  296. {
  297. size_t target_capacity = buffer_capacity;
  298. if (target_capacity == 0)
  299. target_capacity = 1;
  300. // Weird parens works around macro issues on Windows if NOMINMAX is not used.
  301. target_capacity = (std::max)(target_capacity, N);
  302. // Need to ensure there is a POT value of target capacity which is larger than count,
  303. // otherwise this will overflow.
  304. while (target_capacity < count)
  305. target_capacity <<= 1u;
  306. T *new_buffer =
  307. target_capacity > N ? static_cast<T *>(malloc(target_capacity * sizeof(T))) : stack_storage.data();
  308. // If we actually fail this malloc, we are hosed anyways, there is no reason to attempt recovery.
  309. if (!new_buffer)
  310. std::terminate();
  311. // In case for some reason two allocations both come from same stack.
  312. if (new_buffer != this->ptr)
  313. {
  314. // We don't deal with types which can throw in move constructor.
  315. for (size_t i = 0; i < this->buffer_size; i++)
  316. {
  317. new (&new_buffer[i]) T(std::move(this->ptr[i]));
  318. this->ptr[i].~T();
  319. }
  320. }
  321. if (this->ptr != stack_storage.data())
  322. free(this->ptr);
  323. this->ptr = new_buffer;
  324. buffer_capacity = target_capacity;
  325. }
  326. }
  327. void insert(T *itr, const T *insert_begin, const T *insert_end) SPIRV_CROSS_NOEXCEPT
  328. {
  329. auto count = size_t(insert_end - insert_begin);
  330. if (itr == this->end())
  331. {
  332. reserve(this->buffer_size + count);
  333. for (size_t i = 0; i < count; i++, insert_begin++)
  334. new (&this->ptr[this->buffer_size + i]) T(*insert_begin);
  335. this->buffer_size += count;
  336. }
  337. else
  338. {
  339. if (this->buffer_size + count > buffer_capacity)
  340. {
  341. auto target_capacity = this->buffer_size + count;
  342. if (target_capacity == 0)
  343. target_capacity = 1;
  344. if (target_capacity < N)
  345. target_capacity = N;
  346. while (target_capacity < count)
  347. target_capacity <<= 1u;
  348. // Need to allocate new buffer. Move everything to a new buffer.
  349. T *new_buffer =
  350. target_capacity > N ? static_cast<T *>(malloc(target_capacity * sizeof(T))) : stack_storage.data();
  351. // If we actually fail this malloc, we are hosed anyways, there is no reason to attempt recovery.
  352. if (!new_buffer)
  353. std::terminate();
  354. // First, move elements from source buffer to new buffer.
  355. // We don't deal with types which can throw in move constructor.
  356. auto *target_itr = new_buffer;
  357. auto *original_source_itr = this->begin();
  358. if (new_buffer != this->ptr)
  359. {
  360. while (original_source_itr != itr)
  361. {
  362. new (target_itr) T(std::move(*original_source_itr));
  363. original_source_itr->~T();
  364. ++original_source_itr;
  365. ++target_itr;
  366. }
  367. }
  368. // Copy-construct new elements.
  369. for (auto *source_itr = insert_begin; source_itr != insert_end; ++source_itr, ++target_itr)
  370. new (target_itr) T(*source_itr);
  371. // Move over the other half.
  372. if (new_buffer != this->ptr || insert_begin != insert_end)
  373. {
  374. while (original_source_itr != this->end())
  375. {
  376. new (target_itr) T(std::move(*original_source_itr));
  377. original_source_itr->~T();
  378. ++original_source_itr;
  379. ++target_itr;
  380. }
  381. }
  382. if (this->ptr != stack_storage.data())
  383. free(this->ptr);
  384. this->ptr = new_buffer;
  385. buffer_capacity = target_capacity;
  386. }
  387. else
  388. {
  389. // Move in place, need to be a bit careful about which elements are constructed and which are not.
  390. // Move the end and construct the new elements.
  391. auto *target_itr = this->end() + count;
  392. auto *source_itr = this->end();
  393. while (target_itr != this->end() && source_itr != itr)
  394. {
  395. --target_itr;
  396. --source_itr;
  397. new (target_itr) T(std::move(*source_itr));
  398. }
  399. // For already constructed elements we can move-assign.
  400. std::move_backward(itr, source_itr, target_itr);
  401. // For the inserts which go to already constructed elements, we can do a plain copy.
  402. while (itr != this->end() && insert_begin != insert_end)
  403. *itr++ = *insert_begin++;
  404. // For inserts into newly allocated memory, we must copy-construct instead.
  405. while (insert_begin != insert_end)
  406. {
  407. new (itr) T(*insert_begin);
  408. ++itr;
  409. ++insert_begin;
  410. }
  411. }
  412. this->buffer_size += count;
  413. }
  414. }
  415. void insert(T *itr, const T &value) SPIRV_CROSS_NOEXCEPT
  416. {
  417. insert(itr, &value, &value + 1);
  418. }
  419. T *erase(T *itr) SPIRV_CROSS_NOEXCEPT
  420. {
  421. std::move(itr + 1, this->end(), itr);
  422. this->ptr[--this->buffer_size].~T();
  423. return itr;
  424. }
  425. void erase(T *start_erase, T *end_erase) SPIRV_CROSS_NOEXCEPT
  426. {
  427. if (end_erase == this->end())
  428. {
  429. resize(size_t(start_erase - this->begin()));
  430. }
  431. else
  432. {
  433. auto new_size = this->buffer_size - (end_erase - start_erase);
  434. std::move(end_erase, this->end(), start_erase);
  435. resize(new_size);
  436. }
  437. }
  438. void resize(size_t new_size) SPIRV_CROSS_NOEXCEPT
  439. {
  440. if (new_size < this->buffer_size)
  441. {
  442. for (size_t i = new_size; i < this->buffer_size; i++)
  443. this->ptr[i].~T();
  444. }
  445. else if (new_size > this->buffer_size)
  446. {
  447. reserve(new_size);
  448. for (size_t i = this->buffer_size; i < new_size; i++)
  449. new (&this->ptr[i]) T();
  450. }
  451. this->buffer_size = new_size;
  452. }
  453. private:
  454. size_t buffer_capacity = 0;
  455. AlignedBuffer<T, N> stack_storage;
  456. };
  457. // A vector without stack storage.
  458. // Could also be a typedef-ed to std::vector,
  459. // but might as well use the one we have.
  460. template <typename T>
  461. using Vector = SmallVector<T, 0>;
  462. #else // SPIRV_CROSS_FORCE_STL_TYPES
  463. template <typename T, size_t N = 8>
  464. using SmallVector = std::vector<T>;
  465. template <typename T>
  466. using Vector = std::vector<T>;
  467. template <typename T>
  468. using VectorView = std::vector<T>;
  469. #endif // SPIRV_CROSS_FORCE_STL_TYPES
  470. // An object pool which we use for allocating IVariant-derived objects.
  471. // We know we are going to allocate a bunch of objects of each type,
  472. // so amortize the mallocs.
  473. class ObjectPoolBase
  474. {
  475. public:
  476. virtual ~ObjectPoolBase() = default;
  477. virtual void free_opaque(void *ptr) = 0;
  478. };
  479. template <typename T>
  480. class ObjectPool : public ObjectPoolBase
  481. {
  482. public:
  483. explicit ObjectPool(unsigned start_object_count_ = 16)
  484. : start_object_count(start_object_count_)
  485. {
  486. }
  487. template <typename... P>
  488. T *allocate(P &&... p)
  489. {
  490. if (vacants.empty())
  491. {
  492. unsigned num_objects = start_object_count << memory.size();
  493. T *ptr = static_cast<T *>(malloc(num_objects * sizeof(T)));
  494. if (!ptr)
  495. return nullptr;
  496. for (unsigned i = 0; i < num_objects; i++)
  497. vacants.push_back(&ptr[i]);
  498. memory.emplace_back(ptr);
  499. }
  500. T *ptr = vacants.back();
  501. vacants.pop_back();
  502. new (ptr) T(std::forward<P>(p)...);
  503. return ptr;
  504. }
  505. void free(T *ptr)
  506. {
  507. ptr->~T();
  508. vacants.push_back(ptr);
  509. }
  510. void free_opaque(void *ptr) override
  511. {
  512. free(static_cast<T *>(ptr));
  513. }
  514. void clear()
  515. {
  516. vacants.clear();
  517. memory.clear();
  518. }
  519. protected:
  520. Vector<T *> vacants;
  521. struct MallocDeleter
  522. {
  523. void operator()(T *ptr)
  524. {
  525. ::free(ptr);
  526. }
  527. };
  528. SmallVector<std::unique_ptr<T, MallocDeleter>> memory;
  529. unsigned start_object_count;
  530. };
  531. template <size_t StackSize = 4096, size_t BlockSize = 4096>
  532. class StringStream
  533. {
  534. public:
  535. StringStream()
  536. {
  537. reset();
  538. }
  539. ~StringStream()
  540. {
  541. reset();
  542. }
  543. // Disable copies and moves. Makes it easier to implement, and we don't need it.
  544. StringStream(const StringStream &) = delete;
  545. void operator=(const StringStream &) = delete;
  546. template <typename T, typename std::enable_if<!std::is_floating_point<T>::value, int>::type = 0>
  547. StringStream &operator<<(const T &t)
  548. {
  549. auto s = std::to_string(t);
  550. append(s.data(), s.size());
  551. return *this;
  552. }
  553. // Only overload this to make float/double conversions ambiguous.
  554. StringStream &operator<<(uint32_t v)
  555. {
  556. auto s = std::to_string(v);
  557. append(s.data(), s.size());
  558. return *this;
  559. }
  560. StringStream &operator<<(char c)
  561. {
  562. append(&c, 1);
  563. return *this;
  564. }
  565. StringStream &operator<<(const std::string &s)
  566. {
  567. append(s.data(), s.size());
  568. return *this;
  569. }
  570. StringStream &operator<<(const char *s)
  571. {
  572. append(s, strlen(s));
  573. return *this;
  574. }
  575. template <size_t N>
  576. StringStream &operator<<(const char (&s)[N])
  577. {
  578. append(s, strlen(s));
  579. return *this;
  580. }
  581. std::string str() const
  582. {
  583. std::string ret;
  584. size_t target_size = 0;
  585. for (auto &saved : saved_buffers)
  586. target_size += saved.offset;
  587. target_size += current_buffer.offset;
  588. ret.reserve(target_size);
  589. for (auto &saved : saved_buffers)
  590. ret.insert(ret.end(), saved.buffer, saved.buffer + saved.offset);
  591. ret.insert(ret.end(), current_buffer.buffer, current_buffer.buffer + current_buffer.offset);
  592. return ret;
  593. }
  594. void reset()
  595. {
  596. for (auto &saved : saved_buffers)
  597. if (saved.buffer != stack_buffer)
  598. free(saved.buffer);
  599. if (current_buffer.buffer != stack_buffer)
  600. free(current_buffer.buffer);
  601. saved_buffers.clear();
  602. current_buffer.buffer = stack_buffer;
  603. current_buffer.offset = 0;
  604. current_buffer.size = sizeof(stack_buffer);
  605. }
  606. private:
  607. struct Buffer
  608. {
  609. char *buffer = nullptr;
  610. size_t offset = 0;
  611. size_t size = 0;
  612. };
  613. Buffer current_buffer;
  614. char stack_buffer[StackSize];
  615. SmallVector<Buffer> saved_buffers;
  616. void append(const char *s, size_t len)
  617. {
  618. size_t avail = current_buffer.size - current_buffer.offset;
  619. if (avail < len)
  620. {
  621. if (avail > 0)
  622. {
  623. memcpy(current_buffer.buffer + current_buffer.offset, s, avail);
  624. s += avail;
  625. len -= avail;
  626. current_buffer.offset += avail;
  627. }
  628. saved_buffers.push_back(current_buffer);
  629. size_t target_size = len > BlockSize ? len : BlockSize;
  630. current_buffer.buffer = static_cast<char *>(malloc(target_size));
  631. if (!current_buffer.buffer)
  632. SPIRV_CROSS_THROW("Out of memory.");
  633. memcpy(current_buffer.buffer, s, len);
  634. current_buffer.offset = len;
  635. current_buffer.size = target_size;
  636. }
  637. else
  638. {
  639. memcpy(current_buffer.buffer + current_buffer.offset, s, len);
  640. current_buffer.offset += len;
  641. }
  642. }
  643. };
  644. } // namespace SPIRV_CROSS_NAMESPACE
  645. #endif