A biodegradable memory wrapper for C++20.
Variables that decay, corrupt, and die when neglected.
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ int health = 100; โ
โ โ
โ // You forgot about it... โ
โ // 3 seconds later... โ
โ โ
โ health == 73 // It's rotting ๐ฆ โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Traditional variables are immortal โ they hold their value forever until explicitly changed.
LibOrganic introduces mortal memory: values that degrade over time unless actively used. Think of it like:
| Real World | LibOrganic |
|---|---|
| ๐ฅ Milk spoils if you don't drink it | Variables decay if you don't access them |
| ๐ Batteries drain when idle | Values corrupt when neglected |
| ๐ง Memories fade without recall | Data rots without interaction |
- ๐งฌ Organic<T> โ Smart wrapper with built-in decay timer
- โก DecayManager โ Lock-free background entropy (std::jthread)
- โธ๏ธ Pause/Resume โ Temporarily freeze decay
- ๐ Callbacks โ Get notified when values change
- ๐จ Strategy Pattern โ Pluggable corruption algorithms:
| Strategy | Effect | Best For |
|---|---|---|
DriftStrategy |
Gradually shifts values (ยฑ10%) | Numeric drift simulation |
BitFlipStrategy |
Flips random bits | Chaos/corruption testing |
NullifyStrategy |
Resets to zero/default | Hard expiration |
RegenStrategy |
Increases value up to max | Stamina/mana regen |
#include "Organic.h"
#include "BitFlipStrategy.h"
int main() {
// create a 2 second lifetime variable
auto health = Organic<int>::create(100, 2000);
std::cout << health->get(); // 100 โ access resets decay timer
// wait 3 seconds (not accesing it)
std::this_thread::sleep_for(3s);
std::cout << health->peek(); // 89 โ it decayed!
// switch corruption strategy
health->setStrategy(std::make_unique<BitFlipStrategy<int>>());
return 0;
}Copy the include/ folder to your project:
#include "liborganic/Organic.h"git clone https://github.com/alexandrusu1/LibOrganic.git
cd LibOrganic
#compilation
g++ -std=c++20 src/main.cpp src/DecayManager.cpp -Iinclude -pthread -o example
#compile with cmake
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --parallel
./exampleโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Code โ
โ โ
โ auto x = Organic<int>::create(42, 1500); โ
โ x->get(); // Touch! Timer reset. โ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Organic<T> โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ T value โ โ atomic<ms> โ โ CorruptionStrategy<T>* โ โ
โ โ โ โ lastAccess โ โ (default: Drift) โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ registers via weak_ptr
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DecayManager (Singleton) โ
โ โ
โ std::jthread โโโโโโบ entropyLoop() { โ
โ for each object: โ
โ if (now - lastAccess > TTL) โ
โ object->decay(); โ
โ } โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Method | Description |
|---|---|
create(value, ttlMs) |
Factory method โ creates managed instance |
get() |
Access value & reset decay timer |
peek() |
Read value WITHOUT resetting timer |
touch() |
Reset timer without reading value |
isAlive() |
Check if not expired |
pause() |
Freeze decay |
resume() |
Unfreeze decay |
onDecay(callback) |
Called when value changes |
setStrategy(unique_ptr) |
Change corruption algorithm |
#include "DriftStrategy.h" // gradual numeric drift
#include "BitFlipStrategy.h" // random bit corruption
#include "NullifyStrategy.h" // reset to T{}
#include "RegenStrategy.h" // increase up to max| Domain | Application |
|---|---|
| ๐ฎ Game Dev | Health/stamina that drains when idle |
| ๐ Security | Auto-expiring encryption keys |
| ๐งช Testing | Memory corruption simulation |
| ๐พ Caching | Self-invalidating cache entries |
| ๐ค AI/ML | Decaying weights, forgetting mechanisms |
- C++20 compiler (GCC 10+, Clang 10+, MSVC 19.29+)
- pthread support (Linux/macOS) or Windows threads
Contributions welcome! See CONTRIBUTING.md for guidelines.
Ideas for contribution:
- New corruption strategies (Gaussian noise, byte shuffle)
- Configurable decay curves (linear, exponential)
- Thread-safe value access with mutex
- Serialization support
- Python bindings
MIT ยฉ 2025 alexandrusu1
Built with ๐งฌ and C++20
"In LibOrganic, all memory is mortal."