Alternative Memory Management in D

Reaching C++ level of abstraction

import dlib.core.memory;void main()
{
int[] array = New!(int[])(100);
Delete(array);

MyClass obj = New!MyClass(1, 2, 3);
Delete(obj);
}

Allocators

import dlib.core.memory;
import dlib.memory.gcallocator;
void main()
{
Allocator defaultAllocator = globalAllocator;
globalAllocator = GCallocator.instance;
// ‘New’ will use GCallocator

globalAllocator = defaultAllocator;
// ‘New’ will again use default allocator based on malloc
}

Ownership

import dlib.core.memory;
import dlib.core.ownership;
class MyClass: Owner
{
this(Owner owner = null)
{
super(owner);
}
}
void main()
{
MyClass c1 = New!MyClass();
MyClass c2 = New!MyClass(c1);
Delete(c1); // c2 is deleted automatically
}
class MyClass: Owner
{
//...
~this()
{
Delete(myArray);
free(myExternalData);
}
}

Conclusion

--

--

--

Computer graphics developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Patch Notes: January 2020

The rest of LINQ

Knowledge Sharing — Agile, Software Environment

Building a CSS & DOM Clock

Introduction to RxJava for Android Developers

Java Virtual Machine

IoT Raspberry Pi SSH Remote Access over 3G, 4G LTE, or 5G cellular network

The click that changed my life

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Timur Gafarov

Timur Gafarov

Computer graphics developer

More from Medium

Okta vs. Other Authentication/Authorization managers

Writing tests after release is a mistake

What is Integration Testing? Approaches and Challenges Explained!

Exploring Design Patterns — Decorator Pattern