found a broken example
parent
9f41bec462
commit
2571df3b09
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.25)
|
cmake_minimum_required(VERSION 3.25)
|
||||||
project(blt-gp VERSION 0.0.11)
|
project(blt-gp VERSION 0.0.12)
|
||||||
|
|
||||||
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
|
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
|
||||||
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)
|
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)
|
||||||
|
|
|
@ -273,8 +273,24 @@ void test()
|
||||||
|
|
||||||
struct silly
|
struct silly
|
||||||
{
|
{
|
||||||
long nyah;
|
long nyah = 50;
|
||||||
int bruh;
|
int bruh = 10;
|
||||||
|
|
||||||
|
friend std::ostream& operator<<(std::ostream& stream, const silly& silly)
|
||||||
|
{
|
||||||
|
stream << "[" << silly.nyah << " " << silly.bruh << "]";
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct large
|
||||||
|
{
|
||||||
|
unsigned char data[2048];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct super_large
|
||||||
|
{
|
||||||
|
unsigned char data[9582];
|
||||||
};
|
};
|
||||||
|
|
||||||
blt::gp::stack_allocator alloc;
|
blt::gp::stack_allocator alloc;
|
||||||
|
@ -283,17 +299,37 @@ int main()
|
||||||
{
|
{
|
||||||
test();
|
test();
|
||||||
std::cout << alignof(silly) << " " << sizeof(silly) << std::endl;
|
std::cout << alignof(silly) << " " << sizeof(silly) << std::endl;
|
||||||
|
std::cout << alignof(super_large) << " " << sizeof(super_large) << std::endl;
|
||||||
std::cout << alignof(void*) << " " << sizeof(void*) << std::endl;
|
std::cout << alignof(void*) << " " << sizeof(void*) << std::endl;
|
||||||
std::cout << blt::type_string<decltype(&"SillString")>() << std::endl;
|
std::cout << blt::type_string<decltype(&"SillString")>() << std::endl;
|
||||||
|
|
||||||
alloc.push(50);
|
alloc.push(50);
|
||||||
alloc.push(550.0f);
|
alloc.push(550.3f);
|
||||||
alloc.push(20.1230345);
|
alloc.push(20.1230345);
|
||||||
alloc.push(std::string("SillyString"));
|
alloc.push(std::string("SillyString"));
|
||||||
alloc.push(&"SillyString");
|
alloc.push(&"SillyString");
|
||||||
|
|
||||||
std::cout << std::endl << std::endl;
|
std::cout << std::endl;
|
||||||
std::cout << alloc.pop<decltype(&"SillString")>() << std::endl;
|
std::cout << *alloc.pop<decltype(&"SillString")>() << std::endl;
|
||||||
|
std::cout << alloc.pop<std::string>() << std::endl;
|
||||||
|
std::cout << alloc.pop<double>() << std::endl;
|
||||||
|
std::cout << alloc.pop<float>() << std::endl;
|
||||||
|
std::cout << alloc.pop<int>() << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::cout << "Is empty? " << alloc.empty() << std::endl << std::endl;
|
||||||
|
alloc.push(silly{});
|
||||||
|
alloc.push(large{});
|
||||||
|
alloc.push(super_large{});
|
||||||
|
alloc.push(silly{25, 24});
|
||||||
|
alloc.push(large{});
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
alloc.pop<large>();
|
||||||
|
std::cout << alloc.pop<silly>() << std::endl;
|
||||||
|
alloc.pop<super_large>();
|
||||||
|
alloc.pop<large>();
|
||||||
|
std::cout << alloc.pop<silly>() << std::endl;
|
||||||
|
|
||||||
blt::size_t remaining_bytes = 4096;
|
blt::size_t remaining_bytes = 4096;
|
||||||
//auto* pointer = static_cast<void*>(head->metadata.offset);
|
//auto* pointer = static_cast<void*>(head->metadata.offset);
|
||||||
|
|
|
@ -109,15 +109,15 @@ namespace blt::gp
|
||||||
constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT);
|
constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT);
|
||||||
if (head == nullptr)
|
if (head == nullptr)
|
||||||
throw std::runtime_error("Silly boi the stack is empty!");
|
throw std::runtime_error("Silly boi the stack is empty!");
|
||||||
if (head->remaining_bytes_in_block() - head->storage_size() < static_cast<blt::ptrdiff_t>(sizeof(T)))
|
if (head->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(sizeof(T)))
|
||||||
throw std::runtime_error("Mismatched Types!");
|
throw std::runtime_error("Mismatched Types! Not enough space left in block!");
|
||||||
T t = *reinterpret_cast<T*>(head->metadata.offset - offset);
|
T t = *reinterpret_cast<T*>(head->metadata.offset - offset);
|
||||||
head->metadata.offset -= offset;
|
head->metadata.offset -= offset;
|
||||||
if (head->used_bytes_in_block() == static_cast<blt::ptrdiff_t>(head->storage_size()))
|
if (head->used_bytes_in_block() == static_cast<blt::ptrdiff_t>(head->storage_size()))
|
||||||
{
|
{
|
||||||
auto ptr = head;
|
auto ptr = head;
|
||||||
head = head->metadata.prev;
|
head = head->metadata.prev;
|
||||||
delete ptr;
|
std::free(ptr);
|
||||||
}
|
}
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
@ -182,7 +182,7 @@ namespace blt::gp
|
||||||
{
|
{
|
||||||
block* ptr = current;
|
block* ptr = current;
|
||||||
current = current->metadata.prev;
|
current = current->metadata.prev;
|
||||||
delete ptr;
|
std::free(ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,15 +209,15 @@ namespace blt::gp
|
||||||
return static_cast<blt::ptrdiff_t>(metadata.size - sizeof(typename block::block_metadata_t));
|
return static_cast<blt::ptrdiff_t>(metadata.size - sizeof(typename block::block_metadata_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const
|
|
||||||
{
|
|
||||||
return storage_size() - static_cast<blt::ptrdiff_t>(metadata.offset - buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const
|
[[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const
|
||||||
{
|
{
|
||||||
return static_cast<blt::ptrdiff_t>(metadata.offset - buffer);
|
return static_cast<blt::ptrdiff_t>(metadata.offset - buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const
|
||||||
|
{
|
||||||
|
return storage_size() - used_bytes_in_block();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
Loading…
Reference in New Issue