ladivic
Loading...
Searching...
No Matches
ldvc_mem.hpp
Go to the documentation of this file.
1/*
2 * This file is part of the ladivic library.
3 * Copyright (c) 2024 Nathanne Isip
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24/**
25 *
26 * @file ldvc_mem.hpp
27 * @brief Memory management utilities for dynamic allocation and deallocation.
28 *
29 * This header file provides functions for dynamic memory allocation and deallocation,
30 * including allocation, reallocation, and deallocation of memory blocks. Thread safety
31 * is ensured during these operations using a mutex.
32 *
33 * @author Nathanne Isip
34 *
35 */
36#ifndef LDVC_MEM_HPP
37#define LDVC_MEM_HPP
38
39#include <cstddef>
40#include <mutex>
41#include <ldvc_type.hpp>
42
43/// Mutex for thread-safe memory management
44std::mutex ldvc_mem_mutx;
45
46/**
47 *
48 * @brief Allocates memory for an array of elements.
49 *
50 * This function allocates memory for an array of elements
51 * of type T with the specified size.
52 *
53 * @tparam T The type of elements to allocate memory for.
54 *
55 * @param size The number of elements to allocate memory for.
56 *
57 * @return A pointer to the allocated memory block, or nullptr if allocation fails.
58 *
59 */
60template <typename T>
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}
71
72/**
73 *
74 * @brief Deallocates memory previously allocated by ldvc_malloc or similar functions.
75 *
76 * This function deallocates memory previously allocated by ldvc_malloc or similar functions.
77 *
78 * @tparam T The type of elements in the memory block to deallocate.
79 *
80 * @param object A pointer to the memory block to deallocate.
81 *
82 */
83template <typename T>
84void ldvc_free(T* object)
85{
86 const std::lock_guard<std::mutex> lock(ldvc_mem_mutx);
87 ::operator delete(object);
88}
89
90/**
91 *
92 * @brief Reallocates memory for an array of elements.
93 *
94 * This function reallocates memory for an array of elements of
95 * type T with the specified size.
96 *
97 * @tparam T The type of elements to reallocate memory for.
98 *
99 * @param ptr A pointer to the memory block to reallocate.
100 * @param size The new number of elements to allocate memory for.
101 *
102 * @return A pointer to the reallocated memory block, or nullptr if reallocation fails.
103 *
104 */
105template <typename T>
106T* ldvc_realloc(T* ptr, usize size)
107{
108 T* new_ptr = ldvc_malloc<T>(size);
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}
117
118/**
119 *
120 * @brief Allocates and initializes memory for an array of elements.
121 *
122 * This function allocates memory for an array of elements of type
123 * T with the specified size and initializes all elements to zero.
124 *
125 * @tparam T The type of elements to allocate memory for.
126 *
127 * @param num The number of elements to allocate memory for.
128 *
129 * @return A pointer to the allocated memory block, or nullptr if allocation fails.
130 *
131 */
132template <typename T>
134{
136
137 if(ptr != nullptr)
138 for (size_t i = 0; i < num; ++i)
139 ptr[i] = T();
140 return ptr;
141}
142
143#endif
void ldvc_free(T *object)
Deallocates memory previously allocated by ldvc_malloc or similar functions.
Definition ldvc_mem.hpp:84
T * ldvc_realloc(T *ptr, usize size)
Reallocates memory for an array of elements.
Definition ldvc_mem.hpp:106
T * ldvc_calloc(usize num)
Allocates and initializes memory for an array of elements.
Definition ldvc_mem.hpp:133
T * ldvc_malloc(usize size)
Allocates memory for an array of elements.
Definition ldvc_mem.hpp:61
std::mutex ldvc_mem_mutx
Mutex for thread-safe memory management.
Definition ldvc_mem.hpp:44
Defines common types used throughout the library.
size_t usize
Unsigned size type.
Definition ldvc_type.hpp:63