1 /* 2 Copyright (c) 2016-2021 Eugene Wissner 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Abstract allocator interface 31 * 32 * Copyright: Eugene Wissner 2016-2021. 33 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 * Authors: Eugene Wissner 35 */ 36 module dlib.memory.allocator; 37 38 version (unittest) 39 { 40 import dlib.memory : defaultAllocator; 41 } 42 43 /** 44 * Allocator interface. 45 */ 46 interface Allocator 47 { 48 /** 49 * Allocates $(D_PARAM size) bytes of memory. 50 * 51 * Params: 52 * size = Amount of memory to allocate. 53 * 54 * Returns: The pointer to the new allocated memory. 55 */ 56 void[] allocate(size_t size); 57 58 /** 59 * Deallocates a memory block. 60 * 61 * Params: 62 * p = A pointer to the memory block to be freed. 63 * 64 * Returns: Whether the deallocation was successful. 65 */ 66 bool deallocate(void[] p); 67 68 /** 69 * Increases or decreases the size of a memory block. 70 * 71 * Params: 72 * p = A pointer to the memory block. 73 * size = Size of the reallocated block. 74 * 75 * Returns: Whether the reallocation was successful. 76 */ 77 bool reallocate(ref void[] p, size_t size); 78 79 /** 80 * Returns: The alignment offered. 81 */ 82 @property immutable(uint) alignment() const @safe pure nothrow; 83 } 84 85 /** 86 * Params: 87 * T = Element type of the array being created. 88 * allocator = The allocator used for getting memory. 89 * array = A reference to the array being changed. 90 * length = New array length. 91 * 92 * Returns: $(D_KEYWORD true) upon success, $(D_KEYWORD false) if memory could 93 * not be reallocated. In the latter 94 */ 95 bool resizeArray(T)(Allocator allocator, 96 ref T[] array, 97 in size_t length) 98 { 99 void[] buf = array; 100 101 if (!allocator.reallocate(buf, length * T.sizeof)) 102 { 103 return false; 104 } 105 array = cast(T[]) buf; 106 107 return true; 108 } 109 110 /// 111 unittest 112 { 113 int[] p; 114 115 defaultAllocator.resizeArray(p, 20); 116 assert(p.length == 20); 117 118 defaultAllocator.resizeArray(p, 30); 119 assert(p.length == 30); 120 121 defaultAllocator.resizeArray(p, 10); 122 assert(p.length == 10); 123 124 defaultAllocator.resizeArray(p, 0); 125 assert(p is null); 126 } 127 128 enum bool isFinalizable(T) = 129 is(T == class) || 130 is(T == interface) || 131 hasElaborateDestructor!T || 132 isDynamicArray!T;