From 099ce2d91d61a1073a6b1b297c9b0e7a2b407a0c Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Thu, 26 Feb 2026 08:44:40 -0600 Subject: [PATCH] Add more asserts --- src/assert.hpp | 24 ++++++++++++++++++++++++ src/dirinfo.hpp | 9 ++++++++- src/fileinfo.hpp | 15 ++++++++++----- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/assert.hpp b/src/assert.hpp index 1d1488bf..123edf37 100644 --- a/src/assert.hpp +++ b/src/assert.hpp @@ -30,3 +30,27 @@ namespace assert struct StaticAssert {}; } + +#ifdef NDEBUG + +#define ASSERT_NOT_NULL(ptr) + +#else + +#include +#include + +#define ASSERT_NOT_NULL(ptr) \ + do { \ + auto *_tmp = (ptr); \ + if((_tmp) == nullptr) { \ + std::cerr << "ASSERT_NOT_NULL failed\n" \ + << " expression: \"" #ptr "\"\n" \ + << " file: " << __FILE__ << "\n" \ + << " line: " << __LINE__ << "\n" \ + << " function: " << __func__ << "\n"; \ + assert((_tmp) != nullptr); \ + } \ + } while (0) + +#endif diff --git a/src/dirinfo.hpp b/src/dirinfo.hpp index 49b126c6..8ae2315c 100644 --- a/src/dirinfo.hpp +++ b/src/dirinfo.hpp @@ -42,6 +42,7 @@ inline uint64_t DirInfo::to_fh() const { + ASSERT_NOT_NULL(this); return reinterpret_cast(this); } @@ -49,5 +50,11 @@ inline DirInfo* DirInfo::from_fh(const u64 fh_) { - return reinterpret_cast(fh_); + DirInfo *rv; + + rv = reinterpret_cast(fh_); + + ASSERT_NOT_NULL(rv); + + return rv; } diff --git a/src/fileinfo.hpp b/src/fileinfo.hpp index 236ac73d..f30013b5 100644 --- a/src/fileinfo.hpp +++ b/src/fileinfo.hpp @@ -18,6 +18,7 @@ #pragma once +#include "assert.hpp" #include "branch.hpp" #include "fh.hpp" #include "fs_path.hpp" @@ -42,7 +43,6 @@ public: branch(*branch_), direct_io(direct_io_) { - mutex_init(mutex); } FileInfo(const int fd_, @@ -54,7 +54,6 @@ public: branch(branch_), direct_io(direct_io_) { - mutex_init(mutex); } FileInfo(const FileInfo *fi_) @@ -63,7 +62,6 @@ public: branch(fi_->branch), direct_io(fi_->direct_io) { - mutex_init(mutex); } public: @@ -73,13 +71,14 @@ public: int fd; Branch branch; u32 direct_io:1; - mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + Mutex mutex; }; inline u64 FileInfo::to_fh() const { + ASSERT_NOT_NULL(this); return reinterpret_cast(this); } @@ -87,5 +86,11 @@ inline FileInfo* FileInfo::from_fh(const u64 fh_) { - return reinterpret_cast(fh_); + FileInfo *rv; + + rv = reinterpret_cast(fh_); + + ASSERT_NOT_NULL(rv); + + return rv; }