16 #ifndef INCLUDE_NN_NLIB_HANDLEMAKER_H_ 17 #define INCLUDE_NN_NLIB_HANDLEMAKER_H_ 23 namespace handle_maker {
46 return body->IsHandleBodyEnabled();
51 static const size_t size = HBODY::N;
59 int32_t transaction_count;
67 mask =
static_cast<int32_t
>(
reinterpret_cast<uintptr_t
>(&mask));
70 mask |= (1L << (20 + 2));
85 #define NLIB_HANDLETABLE_INITIALIZER {0, 0, 0, {{0, 0, NULL}}} 102 handle_table_ = NULL;
108 : ptr_(ptr), handle_table_(handle_table) {}
111 NLIB_NOEXCEPT NLIB_EXCLUDES(*ptr->item) NLIB_ACQUIRE(*ptr->item)
112 NLIB_NO_THREAD_SAFETY_ANALYSIS {
115 handle_table_ = handle_table;
116 handle_maker::LockHandleBody<HBODY>(ptr->item);
119 detail::HandleData<HBODY>* ptr_;
126 template<
class HBODY>
128 int32_t old_handle = ptr_->handle;
129 if (old_handle != 0) {
138 template<
class HBODY>
140 #if (defined(__GLIBCXX__) && __GLIBCXX__ >= 20140911) || \ 141 defined(_LIBCPP_VERSION) 158 handle = (handle ^ ptr_->mask) >> 2;
159 return static_cast<size_t>(handle) & (N - 1);
162 if (++ptr_->base_count == 1024 * 1024) ptr_->base_count = 0;
163 int salt = ptr_->base_count;
164 salt |= ((salt + 1) << 10) | ((salt + 2) << 20);
165 int handle =
static_cast<int>(idx + (salt * N));
166 return (handle << 2) ^ ptr_->mask;
169 NLIB_NOEXCEPT NLIB_REQUIRES(*data->item) NLIB_RELEASE(*data->item)
170 NLIB_NO_THREAD_SAFETY_ANALYSIS {
171 HBODY* x = data->item;
172 handle_maker::UnlockHandleBody<HBODY>(data->item);
177 handle_maker::DestroyHandleBody<HBODY>(x);
186 template<
class HBODY>
190 template<
class HBODY>
195 int32_t expected = 0;
196 int32_t mask = detail::GenerateHandleMakerMask();
202 detail::HandleData<HBODY>* data;
205 idx = (ptr_->cur) & (N - 1);
207 data = &ptr_->table[idx];
208 int32_t expected = 0;
212 size_t from = idx + 1;
213 for (; idx < N; ++idx) {
214 data = &ptr_->table[idx];
218 goto CREATE_HANDLE_SUCCESS;
221 for (idx = 0; idx < from; ++idx) {
222 data = &ptr_->table[idx];
226 goto CREATE_HANDLE_SUCCESS;
229 for (; idx < N; ++idx) {
230 data = &ptr_->table[idx];
234 goto CREATE_HANDLE_SUCCESS;
237 for (idx = 0; idx < from; ++idx) {
238 data = &ptr_->table[idx];
242 goto CREATE_HANDLE_SUCCESS;
249 CREATE_HANDLE_SUCCESS:
251 data->handle = this->GetHandleFromIdx(idx);
253 *handle = data->handle;
257 template<
class HBODY>
263 detail::HandleData<HBODY>* data;
269 idx = this->GetIdxFromHandle(handle);
274 data = &ptr_->table[idx];
276 if (old_cnt <= 0)
return EBADF;
281 if (old_cnt <= 0)
return EBADF;
284 access->Init(data, ptr_);
293 template<
class HBODY>
303 #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.