BrainMM is an alternate memory manager for Delphi. There are a number of benchmarks between FastMM and BrainMM provided. This is an open source project over on Github. With time the project will grow to the level of standard Delphi/C++ Builder supply, along with other great libraries. Supports of all operating systems envisaged by Delphi and C++ Builder. BrainMM memory manager features:
- Extremely high performance (not fully implemented);
- No locks for memory pieces up to 32Kb;
- DLL-shared memory (Delphi/FPC compatibility);
- Guaranteed aligned address for 16 bytes. It is useful for lock-free algorithms, SEE operations and in general at memory reading/writing/copying;
- RegetMem function. Functionally similar to ReallocMem, but does not guarantee data safety, that is why in some cases it can work faster;
- GetMemAligned function. Allows allocating memory with specific alignment. This memory is released in a standard way, via FreeMem. When the size is changed using ReallocMem/RegetMem, the alignment is preserved. The exception is when NewSize equals zero, in this case, FreeMem shall be induced and alignment information will be lost;
- API for memory block allocation (not fully implemented). BrainMM memory blocks are memory pieces of specific granularity, the size of which is unchangeable. Memory blocks are useful for highly specialized performance-demanding memory management. Service information can be stored at the beginning of the block, access to this information may be received by applying the logical multiplication (and) operation to the pointer. The management of small (up to 128 bytes) and medium (up to 32Kb) memory pieces in BrainMM is performed, for example, with the help of blocks of 64Kb;
- API for work with memory pages (not fully implemented);
- Memory leaks reporting (not fully implemented), standard ReportMemoryLeaksOnShutdow flag;
- Finalizations fixes (QC #103616).
Extended Memory API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
type TMemoryAlign = (ma16Bytes, ma32Bytes, ma64Bytes, ma128Bytes, ma256Bytes, ma512Bytes, ma1024Bytes, ma2048Bytes); PMemoryAlign = ^TMemoryAlign; MemoryBlock = type Pointer; PMemoryBlock = ^MemoryBlock; TMemoryBlockSize = (BLOCK_4K, BLOCK_16K, BLOCK_64K, BLOCK_256K, BLOCK_1MB, BLOCK_4MB, BLOCK_16MB, BLOCK_64MB, BLOCK_256MB); PMemoryBlockSize = ^TMemoryBlockSize; MemoryPages = type Pointer; PMemoryPages = ^MemoryPages; TMemoryAccessRight = (marRead, marWrite, marExecute); PMemoryAccessRight = ^TMemoryAccessRight; TMemoryAccessRights = set of TMemoryAccessRight; PMemoryAccessRights = ^TMemoryAccessRights; TMemoryKind = (mkSmall, mkMedium, mkBig, mkLarge, mkPages, mkBlock, mkJIT); PMemoryKind = ^TMemoryKind; TMemoryOptions = packed record Kind: TMemoryKind; Align: TMemoryAlign; BlockSize: TMemoryBlockSize; AccessRights: TMemoryAccessRights; ThreadId: NativeUInt; Size: NativeUInt; end; PMemoryOptions = ^TMemoryOptions; // additional memory functions procedure GetMemAligned(var P: Pointer; Align: TMemoryAlign; Size: NativeInt); procedure RegetMem(var P: Pointer; NewSize: NativeInt); // block routine procedure GetMemoryBlock(var Block: MemoryBlock; BlockSize: TMemoryBlockSize); procedure FreeMemoryBlock(Block: MemoryBlock); // 4kb-pages routine procedure GetMemoryPages(var Pages: MemoryPages; Count: NativeInt); procedure RegetMemoryPages(var Pages: MemoryPages; NewCount: NativeInt); procedure ReallocMemoryPages(var Pages: MemoryPages; NewCount: NativeInt); procedure FreeMemoryPages(Pages: MemoryPages); // any 4kb-aligned memory procedure ChangeMemoryAccessRights(Pages: MemoryPages; Count: NativeInt; Rights: TMemoryAccessRights); function GetMemoryAccessRights(Pages: MemoryPages): TMemoryAccessRights; // low level routine function GetMemoryOptions(const P: Pointer; var Options: TMemoryOptions): Boolean; function ThreadHeapMinimize: Boolean; // fast SSE-based 16-aligned memory move procedure MoveB16Aligned(const Source; var Dest; const B16Count: NativeInt); type { TJITHeap class } { Just-In-Time memory heap: READ | WRITE | EXECUTE } IJITHeap = interface procedure Clear; stdcall; function GetMemory(Size: NativeInt): Pointer; stdcall; procedure FreeMemory(P: Pointer); stdcall; function SyncGetMemory(Size: NativeInt): Pointer; stdcall; procedure SyncFreeMemory(P: Pointer); stdcall; end; TJITHeap = class(TInterfacedObject, IJITHeap) public constructor Create; destructor Destroy; override; procedure Clear; stdcall; // memory management function GetMemory(Size: NativeInt): Pointer; stdcall; procedure FreeMemory(P: Pointer); stdcall; // synchronization (spin lock) + memory management function SyncGetMemory(Size: NativeInt): Pointer; stdcall; procedure SyncFreeMemory(P: Pointer); stdcall; end; { TMalloc class } TMalloc = class(TInterfacedObject, IMalloc) function Alloc(cb: Longint): Pointer; stdcall; function Realloc(pv: Pointer; cb: Longint): Pointer; stdcall; procedure Free(pv: Pointer); stdcall; function GetSize(pv: Pointer): Longint; stdcall; function DidAlloc(pv: Pointer): Integer; stdcall; procedure HeapMinimize; stdcall; end; |
You can find and download BrainMM from the link below:
https://github.com/d-mozulyov/BrainMM
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition