Thanks to visit codestin.com
Credit goes to github.com

Skip to content

alexandrusu1/LibOrganic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿงฌ LibOrganic

Memory that dies if you don't feed it.

C++20 License: MIT Platform PRs Welcome


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 ๐Ÿฆ         โ”‚
    โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

๐ŸŽฏ Why LibOrganic?

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

โœจ Features

  • ๐Ÿงฌ 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

๐Ÿš€ Quick Start

#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;
}

๐Ÿ”ง Installation

Header-Only (Recommended)

Copy the include/ folder to your project:

#include "liborganic/Organic.h"

Build from Source

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

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         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();                    โ”‚
โ”‚                       }                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“š API Reference

Organic<T>

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

Built-in Strategies

#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

๐ŸŽฎ Use Cases

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

๐Ÿ› ๏ธ Requirements

  • C++20 compiler (GCC 10+, Clang 10+, MSVC 19.29+)
  • pthread support (Linux/macOS) or Windows threads

๐Ÿค Contributing

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

๐Ÿ“„ License

MIT ยฉ 2025 alexandrusu1


Built with ๐Ÿงฌ and C++20

"In LibOrganic, all memory is mortal."

About

C++20 library for self-decaying variables. Memory that rots if you don't access it.

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published