-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gh-581 Extracted non-templated code out of RefCounted
- Loading branch information
1 parent
8af74c6
commit b1a6180
Showing
3 changed files
with
126 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2023 Roc Streaming authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#include "roc_core/ref_counted_impl.h" | ||
#include "roc_core/panic.h" | ||
|
||
namespace roc { | ||
namespace core { | ||
|
||
RefCountedImpl::RefCountedImpl() | ||
: counter_(0) { | ||
} | ||
|
||
RefCountedImpl::~RefCountedImpl() { | ||
if (!counter_.compare_exchange(0, -1)) { | ||
roc_panic("ref counter:" | ||
" attempt to destroy object that is in use, destroyed, or corrupted:" | ||
" counter=%d", | ||
(int)counter_); | ||
} | ||
} | ||
|
||
int RefCountedImpl::getref() const { | ||
const int current_counter = counter_; | ||
|
||
if (current_counter < 0 || current_counter > MaxCounter) { | ||
roc_panic("ref counter:" | ||
" attempt to access destroyed or corrupted object:" | ||
" counter=%d", | ||
(int)current_counter); | ||
} | ||
|
||
return current_counter; | ||
} | ||
|
||
int RefCountedImpl::incref() const { | ||
const int current_counter = ++counter_; | ||
|
||
if (current_counter < 0 || current_counter > MaxCounter) { | ||
roc_panic("ref counter:" | ||
" attempt to access destroyed or corrupted object" | ||
" counter=%d", | ||
(int)current_counter); | ||
} | ||
|
||
return current_counter; | ||
} | ||
|
||
int RefCountedImpl::decref() const { | ||
const int current_counter = --counter_; | ||
|
||
if (current_counter < 0 || current_counter > MaxCounter) { | ||
roc_panic("ref counter:" | ||
" attempt to access destroyed or corrupted object" | ||
" counter=%d", | ||
(int)current_counter); | ||
} | ||
return current_counter; | ||
} | ||
|
||
} // namespace core | ||
} // namespace roc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2023 Roc Streaming authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
//! @file roc_core/ref_counted_impl.h | ||
//! @brief Implementation class for reference counter. | ||
|
||
#ifndef ROC_CORE_REF_COUNTED_IMPL_H_ | ||
#define ROC_CORE_REF_COUNTED_IMPL_H_ | ||
|
||
#include "roc_core/atomic.h" | ||
|
||
namespace roc { | ||
namespace core { | ||
|
||
//! Implementation class for reference counter. | ||
//! | ||
//! Allows to increment and decrement reference counter. | ||
class RefCountedImpl { | ||
public: | ||
//! Initialize. | ||
RefCountedImpl(); | ||
|
||
~RefCountedImpl(); | ||
|
||
//! Get reference counter. | ||
int getref() const; | ||
|
||
//! Increment reference counter. | ||
//! @returns reference counter value after incrementing. | ||
int incref() const; | ||
|
||
//! Decrement reference counter. | ||
//! @returns reference counter value after decrementing. | ||
int decref() const; | ||
|
||
private: | ||
enum { MaxCounter = 100000 }; | ||
|
||
mutable Atomic<int> counter_; | ||
}; | ||
|
||
} // namespace core | ||
} // namespace roc | ||
|
||
#endif // ROC_CORE_REF_COUNTED_IMPL_H_ |