Commit aee5ab34 authored by Maged Michael's avatar Maged Michael Committed by Facebook Github Bot

Remove obsolete version of hazard pointers under folly/experimental

Summary:
Remove obsolete version of hazard pointers under folly/experimental.
Current version is under folly/synchronization.

Reviewed By: djwatson

Differential Revision: D10441558

fbshipit-source-id: 36ef42f83a857ce4bdfff8b64cf9dcc179e0c804
parent 3bd7d93d
...@@ -104,8 +104,6 @@ REMOVE_MATCHES_FROM_LISTS(files hfiles ...@@ -104,8 +104,6 @@ REMOVE_MATCHES_FROM_LISTS(files hfiles
MATCHES MATCHES
"^${FOLLY_DIR}/build/" "^${FOLLY_DIR}/build/"
"^${FOLLY_DIR}/experimental/exception_tracer/" "^${FOLLY_DIR}/experimental/exception_tracer/"
"^${FOLLY_DIR}/experimental/hazptr/bench/"
"^${FOLLY_DIR}/experimental/hazptr/example/"
"^${FOLLY_DIR}/experimental/pushmi/" "^${FOLLY_DIR}/experimental/pushmi/"
"^${FOLLY_DIR}/futures/exercises/" "^${FOLLY_DIR}/futures/exercises/"
"^${FOLLY_DIR}/logging/example/" "^${FOLLY_DIR}/logging/example/"
......
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define HAZPTR_AMB true
#define HAZPTR_TC false
#define HAZPTR_PRIV false
#define HAZPTR_ENABLE_TLS true
#include <folly/experimental/hazptr/bench/HazptrBench.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
using namespace folly::hazptr;
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
benches(" amb - no tc");
}
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define HAZPTR_AMB true
#define HAZPTR_TC true
#define HAZPTR_PRIV true
#define HAZPTR_ENABLE_TLS true
#include <folly/experimental/hazptr/bench/HazptrBench.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
using namespace folly::hazptr;
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
benches(" amb - tc");
}
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define HAZPTR_AMB false
#define HAZPTR_TC false
#define HAZPTR_PRIV false
#define HAZPTR_ENABLE_TLS true
#include <folly/experimental/hazptr/bench/HazptrBench.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
using namespace folly::hazptr;
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
benches("no amb - no tc");
}
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define HAZPTR_AMB false
#define HAZPTR_TC true
#define HAZPTR_PRIV true
#define HAZPTR_ENABLE_TLS true
#include <folly/experimental/hazptr/bench/HazptrBench.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
using namespace folly::hazptr;
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
benches("no amb - tc");
}
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define HAZPTR_AMB true
#define HAZPTR_TC true
#define HAZPTR_PRIV true
#define HAZPTR_ONE_DOMAIN true
#define HAZPTR_ENABLE_TLS true
#include <folly/experimental/hazptr/bench/HazptrBench.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
using namespace folly::hazptr;
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
benches(" one domain");
}
This diff is collapsed.
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <glog/logging.h>
#ifdef HAZPTR_DEBUG
#define HAZPTR_DEBUG_ HAZPTR_DEBUG
#else
#define HAZPTR_DEBUG_ false
#endif
#define HAZPTR_DEBUG_PRINT(...) \
do { \
if (HAZPTR_DEBUG_) { \
VLOG(2) << __func__ << " --- " << __VA_ARGS__; \
} \
} while (false)
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/experimental/hazptr/debug.h>
#include <folly/experimental/hazptr/hazptr.h>
namespace folly {
namespace hazptr {
template <typename T>
class LockFreeLIFO {
class Node : public hazptr_obj_base<Node> {
friend LockFreeLIFO;
public:
~Node() {
HAZPTR_DEBUG_PRINT(this);
}
private:
Node(T v, Node* n) : value_(v), next_(n) {
HAZPTR_DEBUG_PRINT(this);
}
T value_;
Node* next_;
};
public:
LockFreeLIFO() {
HAZPTR_DEBUG_PRINT(this);
}
~LockFreeLIFO() {
HAZPTR_DEBUG_PRINT(this);
}
void push(T val) {
HAZPTR_DEBUG_PRINT(this);
auto pnode = new Node(val, head_.load());
while (!head_.compare_exchange_weak(pnode->next_, pnode)) {
;
}
}
bool pop(T& val) {
HAZPTR_DEBUG_PRINT(this);
hazptr_holder hptr;
Node* pnode = head_.load();
do {
if (pnode == nullptr) {
return false;
}
if (!hptr.try_protect(pnode, head_)) {
continue;
}
auto next = pnode->next_;
if (head_.compare_exchange_weak(pnode, next)) {
break;
}
} while (true);
hptr.reset();
val = pnode->value_;
pnode->retire();
return true;
}
private:
std::atomic<Node*> head_ = {nullptr};
};
} // namespace hazptr
} // namespace folly
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/experimental/hazptr/debug.h>
#include <folly/experimental/hazptr/hazptr.h>
namespace folly {
namespace hazptr {
/** Set implemented as an ordered singly-linked list.
*
* Multiple writers may add or remove elements. Multiple reader
* threads may search the set concurrently with each other and with
* the writers' operations.
*/
template <typename T>
class MWMRListSet {
class Node : public hazptr_obj_base<Node> {
friend MWMRListSet;
T elem_;
std::atomic<uint64_t> refcount_{1};
std::atomic<Node*> next_{nullptr};
// Node must be refcounted for wait-free access: A deleted node
// may have hazptrs pointing at it, so the rest of the list (or at
// least, what existed at the time of the hazptr load) must still
// be accessible.
void release() {
if (refcount_.fetch_sub(1) == 1) {
this->retire();
}
}
// Optimization in the case that we know there are no hazptrs pointing
// at the list.
void releaseFast() {
if (refcount_.load(std::memory_order_relaxed) == 1) {
auto next = getPtr(next_.load(std::memory_order_relaxed));
if (next) {
next->releaseFast();
next_.store(nullptr, std::memory_order_relaxed);
}
delete this;
}
}
void acquire() {
DCHECK(refcount_.load() != 0);
refcount_.fetch_add(1);
}
public:
explicit Node(T e) : elem_(e) {
HAZPTR_DEBUG_PRINT(this << " " << e);
}
~Node() {
HAZPTR_DEBUG_PRINT(this);
auto next = getPtr(next_.load(std::memory_order_relaxed));
if (next) {
next->release();
}
}
};
static bool getDeleted(Node* ptr) {
return uintptr_t(ptr) & 1;
}
static Node* getPtr(Node* ptr) {
return (Node*)(uintptr_t(ptr) & ~1UL);
}
mutable std::atomic<Node*> head_ = {nullptr};
// Remove a single deleted item.
// Although it doesn't have to be our item.
//
// Note that standard lock-free Michael linked lists put this in the
// contains() path, while this implementation leaves it only in
// remove(), such that contains() is wait-free.
void fixlist(
hazptr_holder& hptr_prev,
hazptr_holder& hptr_curr,
std::atomic<Node*>*& prev,
Node*& curr) const {
while (true) {
prev = &head_;
curr = hptr_curr.get_protected(*prev, getPtr);
while (getPtr(curr)) {
auto next = getPtr(curr)->next_.load(std::memory_order_acquire);
if (getDeleted(next)) {
auto nextp = getPtr(next);
if (nextp) {
nextp->acquire();
}
// Try to fix
auto curr_no_mark = getPtr(curr);
if (prev->compare_exchange_weak(curr_no_mark, nextp)) {
// Physically delete
curr_no_mark->release();
return;
} else {
if (nextp) {
nextp->release();
}
break;
}
}
prev = &(getPtr(curr)->next_);
curr = hptr_prev.get_protected(getPtr(curr)->next_, getPtr);
swap(hptr_curr, hptr_prev);
}
DCHECK(getPtr(curr));
}
}
/* wait-free set search */
bool find(
const T& val,
hazptr_holder& hptr_prev,
hazptr_holder& hptr_curr,
std::atomic<Node*>*& prev,
Node*& curr) const {
prev = &head_;
curr = hptr_curr.get_protected(*prev, getPtr);
while (getPtr(curr)) {
auto next = getPtr(curr)->next_.load(std::memory_order_acquire);
if (!getDeleted(next)) {
if (getPtr(curr)->elem_ == val) {
return true;
} else if (!(getPtr(curr)->elem_ < val)) {
break; // Because the list is sorted.
}
}
prev = &(getPtr(curr)->next_);
curr = hptr_prev.get_protected(getPtr(curr)->next_, getPtr);
/* Swap does not change the values of the owned hazard
* pointers themselves. After the swap, The hazard pointer
* owned by hptr_prev continues to protect the node that
* contains the pointer *prev. The hazard pointer owned by
* hptr_curr will continue to protect the node that contains
* the old *prev (unless the old prev was &head), which no
* longer needs protection, so hptr_curr's hazard pointer is
* now free to protect *curr in the next iteration (if curr !=
* null).
*/
swap(hptr_curr, hptr_prev);
}
return false;
}
public:
explicit MWMRListSet() {}
~MWMRListSet() {
Node* next = head_.load();
if (next) {
next->releaseFast();
}
}
bool add(T v) {
hazptr_holder hptr_prev;
hazptr_holder hptr_curr;
std::atomic<Node*>* prev;
Node* cur;
auto newnode = folly::make_unique<Node>(v);
while (true) {
if (find(v, hptr_prev, hptr_curr, prev, cur)) {
return false;
}
newnode->next_.store(cur, std::memory_order_relaxed);
auto cur_no_mark = getPtr(cur);
if (prev->compare_exchange_weak(cur_no_mark, newnode.get())) {
newnode.release();
return true;
}
// Ensure ~Node() destructor doesn't destroy next_
newnode->next_.store(nullptr, std::memory_order_relaxed);
}
}
bool remove(const T& v) {
hazptr_holder hptr_prev;
hazptr_holder hptr_curr;
std::atomic<Node*>* prev;
Node* curr;
while (true) {
if (!find(v, hptr_prev, hptr_curr, prev, curr)) {
return false;
}
auto next = getPtr(curr)->next_.load(std::memory_order_acquire);
auto next_no_mark = getPtr(next); // Ensure only one deleter wins
// Logically delete
if (!getPtr(curr)->next_.compare_exchange_weak(
next_no_mark, (Node*)(uintptr_t(next_no_mark) | 1))) {
continue;
}
if (next) {
next->acquire();
}
// Swing prev around
auto curr_no_mark = getPtr(curr); /* ensure not deleted */
if (prev->compare_exchange_weak(curr_no_mark, next)) {
// Physically delete
curr->release();
return true;
}
if (next) {
next->release();
}
// Someone else modified prev. Call fixlist
// to unlink deleted element by re-walking list.
fixlist(hptr_prev, hptr_curr, prev, curr);
}
}
bool contains(const T& v) const {
hazptr_holder hptr_prev;
hazptr_holder hptr_curr;
std::atomic<Node*>* prev;
Node* curr;
return find(v, hptr_prev, hptr_curr, prev, curr);
}
};
} // namespace hazptr
} // namespace folly
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/experimental/hazptr/debug.h>
#include <folly/experimental/hazptr/hazptr.h>
namespace folly {
namespace hazptr {
/** Set implemented as an ordered singly-linked list.
*
* A single writer thread may add or remove elements. Multiple reader
* threads may search the set concurrently with each other and with
* the writer's operations.
*/
template <typename T>
class SWMRListSet {
template <typename Node>
struct Reclaimer {
void operator()(Node* p) {
HAZPTR_DEBUG_PRINT(p << " " << sizeof(Node));
delete p;
}
};
class Node : public hazptr_obj_base<Node, Reclaimer<Node>> {
friend SWMRListSet;
T elem_;
std::atomic<Node*> next_;
Node(T e, Node* n) : elem_(e), next_(n) {
HAZPTR_DEBUG_PRINT(this << " " << e << " " << n);
}
public:
~Node() {
HAZPTR_DEBUG_PRINT(this);
}
};
std::atomic<Node*> head_ = {nullptr};
/* Used by the single writer */
void locate_lower_bound(const T& v, std::atomic<Node*>*& prev) const {
auto curr = prev->load(std::memory_order_relaxed);
while (curr) {
if (curr->elem_ >= v) {
break;
}
prev = &(curr->next_);
curr = curr->next_.load(std::memory_order_relaxed);
}
return;
}
public:
~SWMRListSet() {
Node* next;
for (auto p = head_.load(); p; p = next) {
next = p->next_.load();
delete p;
}
}
bool add(T v) {
auto prev = &head_;
locate_lower_bound(v, prev);
auto curr = prev->load(std::memory_order_relaxed);
if (curr && curr->elem_ == v) {
return false;
}
prev->store(new Node(std::move(v), curr));
return true;
}
bool remove(const T& v) {
auto prev = &head_;
locate_lower_bound(v, prev);
auto curr = prev->load(std::memory_order_relaxed);
if (!curr || curr->elem_ != v) {
return false;
}
Node* curr_next = curr->next_.load();
// Patch up the actual list...
prev->store(curr_next, std::memory_order_release);
// ...and only then null out the removed node.
curr->next_.store(nullptr, std::memory_order_release);
curr->retire();
return true;
}
/* Used by readers */
bool contains(const T& val) const {
/* Two hazard pointers for hand-over-hand traversal. */
hazptr_local<2> hptr;
hazptr_holder* hptr_prev = &hptr[0];
hazptr_holder* hptr_curr = &hptr[1];
while (true) {
auto prev = &head_;
auto curr = prev->load(std::memory_order_acquire);
while (true) {
if (!curr) {
return false;
}
if (!hptr_curr->try_protect(curr, *prev)) {
break;
}
auto next = curr->next_.load(std::memory_order_acquire);
if (prev->load(std::memory_order_acquire) != curr) {
break;
}
if (curr->elem_ == val) {
return true;
} else if (!(curr->elem_ < val)) {
return false; // because the list is sorted
}
prev = &(curr->next_);
curr = next;
std::swap(hptr_curr, hptr_prev);
}
}
}
};
} // namespace hazptr
} // namespace folly
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/experimental/hazptr/debug.h>
#include <folly/experimental/hazptr/hazptr.h>
#include <string>
namespace folly {
namespace hazptr {
/** Wide CAS.
*/
class WideCAS {
using T = std::string;
class Node : public hazptr_obj_base<Node> {
friend WideCAS;
T val_;
Node() : val_(T()) {
HAZPTR_DEBUG_PRINT(this << " " << val_);
}
explicit Node(T v) : val_(v) {
HAZPTR_DEBUG_PRINT(this << " " << v);
}
public:
~Node() {
HAZPTR_DEBUG_PRINT(this);
}
};
std::atomic<Node*> p_ = {new Node()};
public:
WideCAS() = default;
~WideCAS() {
HAZPTR_DEBUG_PRINT(this << " " << p_.load());
delete p_.load();
}
bool cas(T& u, T& v) {
HAZPTR_DEBUG_PRINT(this << " " << u << " " << v);
Node* n = new Node(v);
hazptr_holder hptr;
Node* p;
do {
p = hptr.get_protected(p_);
if (p->val_ != u) {
delete n;
return false;
}
if (p_.compare_exchange_weak(p, n)) {
break;
}
} while (true);
hptr.reset();
p->retire();
HAZPTR_DEBUG_PRINT(this << " " << p << " " << u << " " << n << " " << v);
return true;
}
};
} // namespace hazptr
} // namespace folly
This diff is collapsed.
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/experimental/hazptr/hazptr.h>
namespace folly {
namespace hazptr {
FOLLY_STATIC_CTOR_PRIORITY_MAX hazptr_domain default_domain_;
hazptr_stats hazptr_stats_;
bool hazptr_tc_enabled() {
return HAZPTR_TC;
}
bool hazptr_priv_enabled() {
return HAZPTR_PRIV;
}
} // namespace hazptr
} // namespace folly
This diff is collapsed.
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/experimental/hazptr/memory_resource.h>
namespace folly {
namespace hazptr {
namespace {
memory_resource** default_mr_ptr() {
/* library-local */ static memory_resource* default_mr =
new_delete_resource();
HAZPTR_DEBUG_PRINT(&default_mr << " " << default_mr);
return &default_mr;
}
} // namespace
memory_resource* get_default_resource() {
HAZPTR_DEBUG_PRINT("");
return *default_mr_ptr();
}
void set_default_resource(memory_resource* mr) {
HAZPTR_DEBUG_PRINT("");
*default_mr_ptr() = mr;
}
memory_resource* new_delete_resource() {
class new_delete : public memory_resource {
public:
void* allocate(const size_t bytes, const size_t alignment = max_align_v)
override {
(void)alignment;
void* p = static_cast<void*>(new char[bytes]);
HAZPTR_DEBUG_PRINT(this << " " << p << " " << bytes);
return p;
}
void deallocate(
void* p,
const size_t bytes,
const size_t alignment = max_align_v) override {
(void)alignment;
(void)bytes;
HAZPTR_DEBUG_PRINT(p << " " << bytes);
delete[] static_cast<char*>(p);
}
};
static new_delete mr;
return &mr;
}
} // namespace hazptr
} // namespace folly
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/experimental/hazptr/debug.h>
////////////////////////////////////////////////////////////////////////////////
/// Disclaimer: This is intended only as a partial stand-in for
/// std::pmr::memory_resource (C++17) as needed for developing a
/// hazptr prototype.
////////////////////////////////////////////////////////////////////////////////
#include <memory>
#include <folly/Portability.h>
#include <folly/lang/Align.h>
namespace folly {
namespace hazptr {
class memory_resource {
public:
virtual ~memory_resource() = default;
virtual void* allocate(
const size_t bytes,
const size_t alignment = max_align_v) = 0;
virtual void deallocate(
void* p,
const size_t bytes,
const size_t alignment = max_align_v) = 0;
};
memory_resource* get_default_resource();
void set_default_resource(memory_resource*);
memory_resource* new_delete_resource();
} // namespace hazptr
} // namespace folly
This diff is collapsed.
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/experimental/hazptr/debug.h>
#include <folly/experimental/hazptr/hazptr.h>
namespace folly {
namespace hazptr {
class MyMemoryResource : public memory_resource {
public:
void* allocate(const size_t sz, const size_t /* align */) override {
void* p = malloc(sz);
HAZPTR_DEBUG_PRINT(p << " " << sz);
return p;
}
void deallocate(void* p, const size_t sz, const size_t /* align */) override {
HAZPTR_DEBUG_PRINT(p << " " << sz);
free(p);
}
};
template <typename Node1>
struct MyReclaimerFree {
inline void operator()(Node1* p) {
HAZPTR_DEBUG_PRINT(p << " " << sizeof(Node1));
free(p);
}
};
class Node1 : public hazptr_obj_base<Node1, MyReclaimerFree<Node1>> {
char a[100];
};
} // namespace hazptr
} // namespace folly
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/experimental/hazptr/debug.h>
#include <folly/experimental/hazptr/hazptr.h>
namespace folly {
namespace hazptr {
class MineMemoryResource : public memory_resource {
public:
void* allocate(const size_t sz, const size_t /* align */) override {
void* p = malloc(sz);
HAZPTR_DEBUG_PRINT(p << " " << sz);
return p;
}
void deallocate(void* p, const size_t sz, const size_t /* align */) override {
HAZPTR_DEBUG_PRINT(p << " " << sz);
free(p);
}
};
class Node2 : public hazptr_obj_base<Node2, void (*)(Node2*)> {
char a[200];
};
inline void mineReclaimFnFree(Node2* p) {
HAZPTR_DEBUG_PRINT(p << " " << sizeof(Node2));
free(p);
}
inline void mineReclaimFnDelete(Node2* p) {
HAZPTR_DEBUG_PRINT(p << " " << sizeof(Node2));
delete p;
}
} // namespace hazptr
} // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment