3 #ifndef INCLUDE_NN_NLIB_HANDLEMAKER_H_ 4 #define INCLUDE_NN_NLIB_HANDLEMAKER_H_ 10 namespace handle_maker {
33 return body->IsHandleBodyEnabled();
38 static const size_t size = HBODY::N;
46 int32_t transaction_count;
54 mask =
static_cast<int32_t
>(
reinterpret_cast<uintptr_t
>(&mask));
57 mask |= (1L << (20 + 2));
72 #define NLIB_HANDLETABLE_INITIALIZER {0, 0, 0, {{0, 0, NULL}}} 95 : ptr_(ptr), handle_table_(handle_table) {}
98 NLIB_NOEXCEPT NLIB_EXCLUDES(*ptr->item) NLIB_ACQUIRE(*ptr->item)
99 NLIB_NO_THREAD_SAFETY_ANALYSIS {
102 handle_table_ = handle_table;
103 handle_maker::LockHandleBody<HBODY>(ptr->item);
106 detail::HandleData<HBODY>* ptr_;
113 template<
class HBODY>
115 int32_t old_handle = ptr_->handle;
116 if (old_handle != 0) {
125 template<
class HBODY>
127 #if (defined(__GLIBCXX__) && __GLIBCXX__ >= 20140911) || \ 128 defined(_LIBCPP_VERSION) 145 handle = (handle ^ ptr_->mask) >> 2;
146 return static_cast<size_t>(handle) & (N - 1);
149 if (++ptr_->base_count == 1024 * 1024) ptr_->base_count = 0;
150 int salt = ptr_->base_count;
151 salt |= ((salt + 1) << 10) | ((salt + 2) << 20);
152 int handle =
static_cast<int>(idx + (salt * N));
153 return (handle << 2) ^ ptr_->mask;
156 NLIB_NOEXCEPT NLIB_REQUIRES(*data->item) NLIB_RELEASE(*data->item)
157 NLIB_NO_THREAD_SAFETY_ANALYSIS {
158 HBODY* x = data->item;
159 handle_maker::UnlockHandleBody<HBODY>(data->item);
164 handle_maker::DestroyHandleBody<HBODY>(x);
173 template<
class HBODY>
177 template<
class HBODY>
182 int32_t expected = 0;
183 int32_t mask = detail::GenerateHandleMakerMask();
189 detail::HandleData<HBODY>* data;
192 idx = (ptr_->cur) & (N - 1);
194 data = &ptr_->table[idx];
195 int32_t expected = 0;
199 size_t from = idx + 1;
200 for (; idx < N; ++idx) {
201 data = &ptr_->table[idx];
205 goto CREATE_HANDLE_SUCCESS;
208 for (idx = 0; idx < from; ++idx) {
209 data = &ptr_->table[idx];
213 goto CREATE_HANDLE_SUCCESS;
216 for (; idx < N; ++idx) {
217 data = &ptr_->table[idx];
221 goto CREATE_HANDLE_SUCCESS;
224 for (idx = 0; idx < from; ++idx) {
225 data = &ptr_->table[idx];
229 goto CREATE_HANDLE_SUCCESS;
236 CREATE_HANDLE_SUCCESS:
238 data->handle = this->GetHandleFromIdx(idx);
240 *handle = data->handle;
244 template<
class HBODY>
250 detail::HandleData<HBODY>* data;
256 idx = this->GetIdxFromHandle(handle);
261 data = &ptr_->table[idx];
263 if (old_cnt <= 0)
return EBADF;
268 if (old_cnt <= 0)
return EBADF;
271 access->Init(data, ptr_);
280 template<
class HBODY>
290 #endif // INCLUDE_NN_NLIB_HANDLEMAKER_H_ A total number of handles that access the HBODY type can be obtained through the data member size of ...
void LockHandleBody(HBODY *body) noexcept
Called when the HandleAccess<HBODY> object is given by HandleMaker<HBODY>::GetHandleAccess().
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
void UnlockHandleBody(HBODY *body) noexcept
Called when the HandleAccess object is destroyed.
A class supporting the implementation of handles with a 32-bit integer value.
void DestroyHandleBody(HBODY *body) noexcept
Called from the HandleMaker object to destroy a handle instance.
bool IsHandleBodyEnabled(HBODY *body) noexcept
Returns if the handle instance is valid or not.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
A table referencing handle instances used by the HandlerMaker class.
A file that contains the configuration information for each development environment.
TimeSpan operator*(int i, const TimeSpan &rhs) noexcept
Increases rhs by a factor of i.
An accessor class that can access the handle instance as if it is a pointer. It becomes available aft...
HandleAccess() noexcept
Instantiates the object with default parameters (default constructor).
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.