ladivic
Loading...
Searching...
No Matches
ldvc_mem.hpp File Reference

Memory management utilities for dynamic allocation and deallocation. More...

#include <cstddef>
#include <mutex>
#include <ldvc_type.hpp>
Include dependency graph for ldvc_mem.hpp:

Go to the source code of this file.

Functions

template<typename T >
T * ldvc_malloc (usize size)
 Allocates memory for an array of elements.
 
template<typename T >
void ldvc_free (T *object)
 Deallocates memory previously allocated by ldvc_malloc or similar functions.
 
template<typename T >
Tldvc_realloc (T *ptr, usize size)
 Reallocates memory for an array of elements.
 
template<typename T >
Tldvc_calloc (usize num)
 Allocates and initializes memory for an array of elements.
 

Variables

std::mutex ldvc_mem_mutx
 Mutex for thread-safe memory management.
 

Detailed Description

Memory management utilities for dynamic allocation and deallocation.

This header file provides functions for dynamic memory allocation and deallocation, including allocation, reallocation, and deallocation of memory blocks. Thread safety is ensured during these operations using a mutex.

Author
Nathanne Isip

Definition in file ldvc_mem.hpp.

Function Documentation

◆ ldvc_calloc()

template<typename T >
T * ldvc_calloc ( usize num)

Allocates and initializes memory for an array of elements.

This function allocates memory for an array of elements of type T with the specified size and initializes all elements to zero.

Template Parameters
TThe type of elements to allocate memory for.
Parameters
numThe number of elements to allocate memory for.
Returns
A pointer to the allocated memory block, or nullptr if allocation fails.

Definition at line 133 of file ldvc_mem.hpp.

134{
136
137 if(ptr != nullptr)
138 for (size_t i = 0; i < num; ++i)
139 ptr[i] = T();
140 return ptr;
141}
T * ldvc_malloc(usize size)
Allocates memory for an array of elements.
Definition ldvc_mem.hpp:61

◆ ldvc_free()

template<typename T >
void ldvc_free ( T * object)

Deallocates memory previously allocated by ldvc_malloc or similar functions.

This function deallocates memory previously allocated by ldvc_malloc or similar functions.

Template Parameters
TThe type of elements in the memory block to deallocate.
Parameters
objectA pointer to the memory block to deallocate.

Definition at line 84 of file ldvc_mem.hpp.

85{
86 const std::lock_guard<std::mutex> lock(ldvc_mem_mutx);
87 ::operator delete(object);
88}
std::mutex ldvc_mem_mutx
Mutex for thread-safe memory management.
Definition ldvc_mem.hpp:44

Referenced by ldvc_realloc().

◆ ldvc_malloc()

template<typename T >
T * ldvc_malloc ( usize size)

Allocates memory for an array of elements.

This function allocates memory for an array of elements of type T with the specified size.

Template Parameters
TThe type of elements to allocate memory for.
Parameters
sizeThe number of elements to allocate memory for.
Returns
A pointer to the allocated memory block, or nullptr if allocation fails.

Definition at line 61 of file ldvc_mem.hpp.

62{
63 const std::lock_guard<std::mutex> lock(ldvc_mem_mutx);
64
65 T* ptr = static_cast<T*>(::operator new(size * sizeof(T)));
66 if(ptr == nullptr)
67 return (int*) -1;
68
69 return ptr;
70}

Referenced by ldvc_calloc(), and ldvc_realloc().

◆ ldvc_realloc()

template<typename T >
T * ldvc_realloc ( T * ptr,
usize size )

Reallocates memory for an array of elements.

This function reallocates memory for an array of elements of type T with the specified size.

Template Parameters
TThe type of elements to reallocate memory for.
Parameters
ptrA pointer to the memory block to reallocate.
sizeThe new number of elements to allocate memory for.
Returns
A pointer to the reallocated memory block, or nullptr if reallocation fails.

Definition at line 106 of file ldvc_mem.hpp.

107{
109 if(ptr != nullptr) {
110 for (size_t i = 0; i < size; ++i)
111 new_ptr[i] = ptr[i];
112 ldvc_free(ptr);
113 }
114
115 return new_ptr;
116}
void ldvc_free(T *object)
Deallocates memory previously allocated by ldvc_malloc or similar functions.
Definition ldvc_mem.hpp:84

Variable Documentation

◆ ldvc_mem_mutx

std::mutex ldvc_mem_mutx

Mutex for thread-safe memory management.

Definition at line 44 of file ldvc_mem.hpp.

Referenced by ldvc_free(), and ldvc_malloc().