fix stack

thread
Brett 2024-07-17 00:54:24 -04:00
parent cbbf7a9cf5
commit 8e5a3f3b7c
3 changed files with 10 additions and 7 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.0.88) project(blt-gp VERSION 0.0.89)
include(CTest) include(CTest)

View File

@ -414,7 +414,7 @@ namespace blt::gp
[[nodiscard]] bool should_thread_terminate() const [[nodiscard]] bool should_thread_terminate() const
{ {
return should_terminate() || thread_helper.lifetime_over; return thread_helper.lifetime_over;
} }
[[nodiscard]] random_t& get_random() const; [[nodiscard]] random_t& get_random() const;

View File

@ -227,19 +227,22 @@ namespace blt::gp
T& from(blt::size_t bytes) T& from(blt::size_t bytes)
{ {
using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>; using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>;
constexpr static auto TYPE_SIZE = aligned_size<NO_REF_T>(); constexpr static auto TYPE_SIZE = aligned_size<NO_REF_T>();
auto remaining_bytes = static_cast<blt::i64>(bytes);
blt::i64 bytes_into_block = 0; auto remaining_bytes = static_cast<blt::ptrdiff_t>(bytes + TYPE_SIZE);
block* blk = head; block* blk = head;
while (remaining_bytes > 0) while (remaining_bytes > 0)
{ {
if (blk == nullptr) if (blk == nullptr)
throw std::runtime_error("Requested size is beyond the scope of this stack!"); throw std::runtime_error("Requested size is beyond the scope of this stack!");
auto bytes_available = blk->used_bytes_in_block() - remaining_bytes; auto bytes_available = blk->used_bytes_in_block() - remaining_bytes;
bytes_into_block = remaining_bytes;
if (bytes_available < 0) if (bytes_available < 0)
{ {
remaining_bytes = -bytes_available; remaining_bytes -= blk->used_bytes_in_block();
blk = head->metadata.prev; blk = head->metadata.prev;
} else } else
break; break;
@ -249,7 +252,7 @@ namespace blt::gp
if (blk->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(TYPE_SIZE)) if (blk->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(TYPE_SIZE))
throw std::runtime_error((std::string("Mismatched Types! Not enough space left in block! Bytes: ") += std::to_string( throw std::runtime_error((std::string("Mismatched Types! Not enough space left in block! Bytes: ") += std::to_string(
blk->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(NO_REF_T))).c_str()); blk->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(NO_REF_T))).c_str());
return *reinterpret_cast<NO_REF_T*>((blk->metadata.offset - bytes_into_block) - TYPE_SIZE); return *reinterpret_cast<NO_REF_T*>(blk->metadata.offset - remaining_bytes);
} }
void pop_bytes(blt::ptrdiff_t bytes) void pop_bytes(blt::ptrdiff_t bytes)