Skip to content

Instantly share code, notes, and snippets.

@run-dlang
Created September 28, 2023 10:24
Show Gist options
  • Save run-dlang/bee406576c1f371fa8a8d69582e40612 to your computer and use it in GitHub Desktop.
Save run-dlang/bee406576c1f371fa8a8d69582e40612 to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io.
// Library for creating, resizing and managing memory blocks.
import core.stdc.stdio;
import core.stdc.stdlib;
alias heap = malloc; // memory allocation function
alias heapInit = calloc; // memory allocation with initialization.
alias resizeHeap = realloc; // change allocated memory.
alias release = free; // Release memory block
alias print = printf;
// Memory block information
static union MemoryBlock{
void * pointer;
size_t elements;
size_t elementSizeInBytes;
size_t MemoryBlockSize;
}
static MemoryBlock[] allocatedMemoryBlocks;
@nogc
@system
auto onlyAllocate(size_t bytes){
auto memoryPointer = heap(size: bytes);
if (memoryPointer is null){
print("Fatal: failed to allocate %zu Bytes of RAM memory.\n", bytes);
return null;
}
return memoryPointer;
}
nothrow
@nogc
@system
auto allocate(size_t elements, size_t elementSizeInBytes){
auto memoryPointer = heapInit(nmemb: elements, size: elementSizeInBytes);
if (memoryPointer is null){
print("Fatal: failed to allocate %zu Bytes per %zu elements of RAM memory.\n", elementSizeInBytes, elements); //parts of ram
print("Fatal: failed to allocate %zu Bytes of RAM memory.\n", elements * elementSizeInBytes);
return null; //overall
}
MemoryBlock memoryBlock;
memoryBlock.pointer = memoryPointer;
memoryBlock.elements = elements;
memoryBlock.elementSizeInBytes = elementSizeInBytes;
memoryBlock.MemoryBlockSize = elements * elementSizeInBytes;
print("%p", memoryPointer);
return memoryPointer;
}
// TODO: make structure to check if memory allocated.
// Store: memory address and its size with datatype info?
// Idea: For tracking and calculating memory usage
// Will be useful for realloc and free to randomly deallocate memory and fill the program with dangling pointers?
// Rust borrowing would be better solution.
// Idea: functions with no ability to store allocated memory pointers for later use, like checking if pointer deallocated.
//NoTrackingHeap NoTrackingHeapInit
// custom free function for releasing pointers that checks if pointer exists in the structure of pointers.
// Also remember to save the datatype of memory block used in the memory block. (int, unit, size_t or other)
void listAllocatedHeap();
void allocationHistory();
void allocationMap();
void allocatedMemory();
@nogc
@system
auto reallocate(void * MemoryPointer, size_t size){
auto memoryPointer = resizeHeap(ptr: MemoryPointer, size: size);
if (memoryPointer is null){
print("Fatal: failed to reallocate %zu Bytes of RAM memory.\n", size);
return null; //overall
}
print("Resized memory pointer PointerAddress to SIZE Bytes");
return memoryPointer;
}
@nogc
@system
int main() {
//int * memory = cast(int*)onlyAllocate(2147483648); //can allocate more than 2GB, 32bit interger myth
// realloc works like malloc, therefore additional step of initialization needs to be added, a new reallocInit function?
int * memory = cast(int*)allocate(5, 50);
print("test");
print("%p", memory);
print("Hello D");
// Don't forget to free the allocated memory when you're done
//free(memory);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment