All Projects → atilaneves → Automem

atilaneves / Automem

Licence: bsd-3-clause
C++-style automatic memory management smart pointers for D

Programming Languages

d
599 projects
dlang
54 projects

Projects that are alternatives of or similar to Automem

Cone
Cone Programming Language
Stars: ✭ 257 (+261.97%)
Mutual labels:  memory-management
Micro Mitten
You might not need your garbage collector
Stars: ✭ 470 (+561.97%)
Mutual labels:  memory-management
Errand Boy
A memory-conscious alternative to os.fork() and subprocess.Popen().
Stars: ✭ 34 (-52.11%)
Mutual labels:  memory-management
Heap Layers
Heap Layers: An Extensible Memory Allocation Infrastructure
Stars: ✭ 260 (+266.2%)
Mutual labels:  memory-management
Mps
The Memory Pool System
Stars: ✭ 351 (+394.37%)
Mutual labels:  memory-management
Redis Memory Analyzer
Redis memory profiler to find the RAM bottlenecks throw scaning key space in real time and aggregate RAM usage statistic by patterns.
Stars: ✭ 591 (+732.39%)
Mutual labels:  memory-management
rs-process-memory
A rust library that allows you to read/write into the memory of other processes
Stars: ✭ 63 (-11.27%)
Mutual labels:  memory-management
Weakable Self
A Swift micro-framework to easily deal with weak references to self inside closures
Stars: ✭ 64 (-9.86%)
Mutual labels:  memory-management
Scalene
Scalene: a high-performance, high-precision CPU, GPU, and memory profiler for Python
Stars: ✭ 4,819 (+6687.32%)
Mutual labels:  memory-management
Gc
Simple, zero-dependency garbage collection for C
Stars: ✭ 851 (+1098.59%)
Mutual labels:  memory-management
Structlayout
Visual Studio Extension for C++ struct memory layout visualization
Stars: ✭ 270 (+280.28%)
Mutual labels:  memory-management
Sinsofmemoryleaks
Some common patterns of memory leaks in Android development and how to fix/avoid them
Stars: ✭ 343 (+383.1%)
Mutual labels:  memory-management
Articles Translator
📚Translate the distinct technical blogs. Please star or watch. Welcome to join me.
Stars: ✭ 606 (+753.52%)
Mutual labels:  memory-management
Stringsareevil
Reducing memory allocations from 7.5GB to 32KB
Stars: ✭ 260 (+266.2%)
Mutual labels:  memory-management
Go Pmem
Native persistent memory support for Go
Stars: ✭ 57 (-19.72%)
Mutual labels:  memory-management
o1heap
Constant-complexity deterministic memory allocator (heap) for hard real-time high-integrity embedded systems
Stars: ✭ 119 (+67.61%)
Mutual labels:  memory-management
The holy book of x86
A simple guide to x86 architecture, assembly, memory management, paging, segmentation, SMM, BIOS....
Stars: ✭ 577 (+712.68%)
Mutual labels:  memory-management
Vulkanmemoryallocator
Easy to integrate Vulkan memory allocation library
Stars: ✭ 1,136 (+1500%)
Mutual labels:  memory-management
Memreduct
Lightweight real-time memory management application to monitor and clean system memory on your computer.
Stars: ✭ 1,101 (+1450.7%)
Mutual labels:  memory-management
Sralloc
Memory allocators
Stars: ✭ 25 (-64.79%)
Mutual labels:  memory-management

automem - smart pointers for D

Build Status Build Status Coverage Open on run.dlang.io

C++-style automatic memory management smart pointers for D using std.experimental.allocator.

Unlike the C++ variants, the smart pointers themselves allocate the memory for the objects they contain. That ensures the right allocator is used to dispose of the memory as well.

Allocators are template arguments instead of using theAllocator so that these smart pointers can be used in @nogc code. However, they will default to typeof(theAllocator) for simplicity. The examples above will be explicit.

Another reason to have to pass in the type of allocator is to decide how it is to be stored. Stateless allocators can be "stored" by value and imply zero-cost Unique pointers. Singleton allocators such as Mallocator (that have an instance attribute/member function) don't need to be passed in to the constructor. This is detected at compile-time as an example of design by instrospection.

RefCounted leverages D's type system by doing atomic reference counting iff the type of the contained object is shared. Otherwise it's non-atomic.

Sample code:

@safe unittest {

    import std.algorithm: move;

    static struct Point {
        int x;
        int y;
    }

    // set theAllocator as desired beforehand, e.g.
    // theAllocator = allocatorObject(Mallocator.instance)

    {
        // must pass arguments to initialise the contained object
        auto u1 = Unique!Point(2, 3);
        assert(*u1 == Point(2, 3));
        assert(u1.y == 3);

        // auto u2 = u1; // won't compile, can only move
        typeof(u1) u2 = () @trusted { return u1.move; }();
        assert(cast(bool)u1 == false); // u1 is now empty
    }
    // memory freed for the Point structure created in the block

    {
        auto s1 = RefCounted!Point(4, 5);
        assert(*s1 == Point(4, 5));
        assert(s1.x == 4);
        {
            auto s2 = s1; // can be copied
        } // ref count goes to 1 here

    } // ref count goes to 0 here, memory released

    {
        import std.algorithm: map;
        import std.range: iota;

        // `vector` is also known as `array`
        auto vec = vector(Point(1, 2), Point(3, 4), Point(5, 6));
        assert(equal(vec.range, [Point(1, 2), Point(3, 4), Point(5, 6)]));

        vec.length = 1;
        assert(equal(vec.range, [Point(1, 2)]));

        vec ~= Point(7, 8);
        assert(equal(vec.range, [Point(1, 2), Point(7, 8)]));

        vec ~= 2.iota.map!(i => Point(i + 10, i + 11));
        assert(equal(vec.range, [Point(1, 2), Point(7, 8), Point(10, 11), Point(11, 12)]));
    } // memory for the array released here
}


// @nogc test - must explicitly use the allocator for compile-time guarantees
@system @nogc unittest {
    import stdx.allocator.mallocator: Mallocator;

    static struct Point {
        int x;
        int y;
    }

    {
        // must pass arguments to initialise the contained object
        auto u1 = Unique!(Point, Mallocator)(2, 3);
        assert(*u1 == Point(2, 3));
        assert(u1.y == 3);
    }
    // memory freed for the Point structure created in the block

    // similarly for the other types
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].