Skip to content

dlib.core.memory

Timur Gafarov edited this page Apr 26, 2017 · 2 revisions

dlib.core.memory

Global GC-free instantiator for classes, structs and arrays. It utilizes dlib.memory for actual memory allocation, so it is possible to switch allocator that is being used. By default, dlib.memory.mallocator.Mallocator is used.

Free functions

  • T New(T, A...)(A args) - creates an object of type T and calls its constructor if necessary. This is an equivalent for D's new opetator. args is an arguments tuple that is passed to constructor.
  • void Delete(T)(T obj) - destroys an object of type T previously created by New and calls its destructor if necessary.
  • ulong allocatedMemory() - returns current amount of allocated memory in bytes. This is 0 at program start.
  • Allocator globalAllocator() - returns current Allocator that is used by New and Delete.
  • void globalAllocator(Allocator a) - sets allocator that should be used by New and Delete.

Usage example

MyClass c = New!MyClass(10, 4, 5);
int[] arr = New!(int[])(100);
assert(arr.length == 100);
MyStruct* s = New!MyStruct;
Delete(c);
Delete(arr);
Delete(s);

Memory profiler

The module includes a simple memory profiler that can be turned on with MemoryDebug version switch. If active, it will store information about every allocation (type and size), and will mark those which are leaked (haven't been deleted).

  • printMemoryLog() - prints an allocation list. Does nothing if compiled without MemoryDebug option.
Clone this wiki locally