Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made allocator an inline function #532

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions include/matx/core/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,17 @@ struct matxPointerAttr_t {
};
}


inline detail::matxMemoryStats_t matxMemoryStats; ///< Statistics object
inline std::shared_mutex memory_mtx; ///< Mutex protecting updates from map
inline std::unordered_map<void *, detail::matxPointerAttr_t> allocationMap; ///< Map recording allocations



__attribute__ ((visibility ("default")))
__MATX_INLINE__ std::unordered_map<void *, detail::matxPointerAttr_t> &GetAllocMap() {
static std::unordered_map<void *, detail::matxPointerAttr_t> allocationMap;
return allocationMap;
}

/**
* @brief Determine if a pointer is printable by the host
Expand Down Expand Up @@ -137,9 +145,9 @@ inline bool IsAllocated(void *ptr) {
}

std::unique_lock lck(memory_mtx);
auto iter = allocationMap.find(ptr);
auto iter = GetAllocMap().find(ptr);

return iter != allocationMap.end();
return iter != GetAllocMap().end();
}

/**
Expand All @@ -161,9 +169,9 @@ inline matxMemorySpace_t GetPointerKind(void *ptr)
}

std::unique_lock lck(memory_mtx);
auto iter = allocationMap.find(ptr);
auto iter = GetAllocMap().find(ptr);

if (iter != allocationMap.end()) {
if (iter != GetAllocMap().end()) {
return iter->second.kind;
}

Expand All @@ -184,7 +192,7 @@ inline void matxPrintMemoryStatistics()
printf("Memory Statistics(GB): current: %.2f, total: %.2f, max: %.2f. Total "
"allocations: %lu\n",
static_cast<double>(current) / 1e9, static_cast<double>(total) / 1e9,
static_cast<double>(max) / 1e9, allocationMap.size());
static_cast<double>(max) / 1e9, GetAllocMap().size());
}

/**
Expand Down Expand Up @@ -237,14 +245,10 @@ inline void matxAlloc(void **ptr, size_t bytes,
matxMemoryStats.totalBytesAllocated += bytes;
matxMemoryStats.maxBytesAllocated = std::max(
matxMemoryStats.maxBytesAllocated, matxMemoryStats.currentBytesAllocated);
allocationMap[*ptr] = {bytes, space, stream};
GetAllocMap()[*ptr] = {bytes, space, stream};
}

/**
* @brief Free previously-allocated pointer
*
* @param ptr Pointer to free
*/

inline void matxFree(void *ptr)
{
MATX_NVTX_START("", matx::MATX_NVTX_LOG_INTERNAL)
Expand All @@ -254,10 +258,15 @@ inline void matxFree(void *ptr)
}

std::unique_lock lck(memory_mtx);
auto iter = allocationMap.find(ptr);
auto iter = GetAllocMap().find(ptr);

if (iter == allocationMap.end()) {
if (iter == GetAllocMap().end()) {
#ifdef MATX_DISABLE_MEM_TRACK_CHECK
// This error can occur in situations where the user includes MatX in multiple translation units
// and a deallocation occurs in a different one than it was allocated. Allow the user to ignore
// these cases if they know the issue.
MATX_THROW(matxInvalidParameter, "Couldn't find pointer in allocation cache");
#endif
return;
}

Expand All @@ -283,9 +292,11 @@ inline void matxFree(void *ptr)
MATX_THROW(matxInvalidType, "Invalid memory type");
}

allocationMap.erase(iter);
GetAllocMap().erase(iter);
}



/**
* @brief Allocator following the PMR interface using the internal MatX allocator/deallocator
*
Expand Down