Broom 1.0.0
A thread-local C++ Garbage Collector
Loading...
Searching...
No Matches
broom.h
Go to the documentation of this file.
1#pragma once
2
3#include <assert.h>
4#include <stddef.h>
5
6#include <memory>
7#include <type_traits>
8
31#if defined(_WIN32)
32#define BROOM_EXPORT __declspec(dllexport)
33#else
34#define BROOM_EXPORT __attribute__((visibility("default")))
35#endif
36
37#define __BROOM_IMPL_CONSTRUCT_T(v, T, args) \
38 new (v) T(std::forward<Args>(args)...)
39#define __BROOM_IMPL_DESTRUCTOR_T(T) \
40 [](const void* pointer) { static_cast<const T*>(pointer)->~T(); }
41#define __BROOM_IMPL_ALLOCATE_T(T, RegisterFunction, AllocateFunction, args) \
42 { \
43 T* t_to_return; \
44 void* to_return; \
45 if constexpr (!std::is_same_v<T, void>) { \
46 to_return = AllocateFunction(count * sizeof(T)); \
47 t_to_return = __BROOM_IMPL_CONSTRUCT_T(to_return, T, args); \
48 RegisterFunction(to_return, __BROOM_IMPL_DESTRUCTOR_T(T)); \
49 } else { \
50 to_return = AllocateFunction(count); \
51 t_to_return = to_return; \
52 RegisterFunction(to_return, nullptr); \
53 } \
54 return t_to_return; \
55 }
56
74
79
80namespace broom {
81class GarbageCollector;
82extern thread_local GarbageCollector* t_gc;
100
101#ifdef BROOM_FOR_TESTING
102// This is a workaround for Windows thread-local storage.
103BROOM_EXPORT GarbageCollector* thread_get_gc();
104#endif
105
128
161
191
227
244
245using Destructor = std::add_pointer<void(const void*)>::type;
246namespace __impl {
247BROOM_EXPORT void* AllocateRaw(size_t size);
248BROOM_EXPORT void RegisterWithGc(const void* pointer,
249 Destructor destructor = nullptr);
250BROOM_EXPORT void ShareRaw(const void* pointer, uint32_t how_many);
255} // namespace __impl
256
272 BroomDisableGcScope() { __impl::TurnOffGarbageCollection(); }
276 ~BroomDisableGcScope() { __impl::TurnOnGarbageCollection(); }
277
282};
283
288
301// clang-format off
302template <typename T, typename... Args>
307// clang-format on
308
334BROOM_EXPORT void register_external(const void* pointer, size_t size);
346
354 public:
358 Visitor() = default;
362 // XXX(gc): Decide if this really needs to be virtual.
363 virtual ~Visitor() = default;
368 virtual void Visit(const void* pointer) = 0;
369};
370
375
403 public:
404 virtual ~BroomValue();
413 virtual void Visit(Visitor* visitor) const = 0;
414
415 protected:
426 BroomValue();
441 BroomValue& operator=(const BroomValue& other);
449 BroomValue& operator=(BroomValue&& other) noexcept;
450
451 private:
455 GarbageCollector* gc_;
460 void* precise_root_ref_;
461};
462
467
468template <typename T>
470
506template <typename T>
508 public:
517 : gc_(other.gc_), ptr_(other.ptr_) {}
522
525 delete;
526
533 T* to_unsafe_ptr() const { return ptr_; }
541 T* operator->() { return ptr_; }
549 T& operator*() { return *ptr_; }
550
563 static constexpr BroomSharingPointer make(T* ptr, uint32_t how_many) {
564 assert(how_many >= 1);
565 __impl::ShareRaw(static_cast<const void*>(ptr), how_many);
566 return BroomSharingPointer<T>(ptr);
567 }
568
575 void dispose() const { __impl::UnpinPointer(gc_, ptr_); }
576
577 private:
584 explicit BroomSharingPointer(T* ptr)
585 : gc_(__impl::GetGarbageCollector()), ptr_(ptr) {}
586
590 GarbageCollector* gc_;
594 T* ptr_;
595 friend class BroomLifetimeAnchor<T>;
596};
597
630template <typename T>
632 public:
642 BroomLifetimeAnchor(BroomSharingPointer<T> ptr) : sharing_pointer_(ptr) {}
646 ~BroomLifetimeAnchor() { sharing_pointer_.dispose(); }
647
651 T* to_unsafe_ptr() const { return sharing_pointer_.ptr_; }
659 T* operator->() { return sharing_pointer_.ptr_; }
667 T& operator*() { return *sharing_pointer_.ptr_; }
668
669 private:
673 BroomSharingPointer<T> sharing_pointer_;
674};
675
679template <typename T>
681
685template <typename T>
687
698template <typename T>
699sharing_ptr<T> share(T* ptr, uint32_t count = 1) {
700 return sharing_ptr<T>::make(ptr, count);
701}
702} // namespace broom
703
704#undef BROOM_EXPORT
#define BROOM_EXPORT
Definition broom.h:34
#define __BROOM_IMPL_ALLOCATE_T(T, RegisterFunction, AllocateFunction, args)
Definition broom.h:41
T * to_unsafe_ptr() const
Definition broom.h:651
BroomLifetimeAnchor(BroomSharingPointer< T > ptr)
Definition broom.h:642
BroomSharingPointer & operator=(const BroomSharingPointer &&other) noexcept=delete
T * to_unsafe_ptr() const
Definition broom.h:533
BroomSharingPointer & operator=(const BroomSharingPointer &other)=delete
static constexpr BroomSharingPointer make(T *ptr, uint32_t how_many)
Definition broom.h:563
BroomSharingPointer(const BroomSharingPointer &other)
Definition broom.h:516
void dispose() const
Definition broom.h:575
virtual void Visit(Visitor *visitor) const =0
Visitor()=default
virtual void Visit(const void *pointer)=0
virtual ~Visitor()=default
void RegisterWithGc(const void *pointer, Destructor destructor)
Definition api.cc:25
void TurnOnGarbageCollection()
Definition api.cc:36
void * AllocateRaw(size_t size)
Definition api.cc:24
void TurnOffGarbageCollection()
Definition api.cc:35
void ShareRaw(const void *pointer, uint32_t how_many)
Definition api.cc:28
void UnpinPointer(GarbageCollector *gc, const void *pointer)
Definition api.cc:31
GarbageCollector * GetGarbageCollector()
Definition api.cc:34
std::add_pointer< void(const void *)>::type Destructor
Definition broom.h:245
thread_local GarbageCollector * t_gc
Definition api.cc:6
void unregister_external(const void *pointer)
Definition api.cc:44
void thread_init(broom_configuration config)
Definition api.cc:7
T * allocate(size_t count, Args &&... args)
Definition broom.h:303
void thread_teardown()
Definition api.cc:12
void force_collection()
Definition api.cc:39
std::queue< T, broom::deque< T > > queue
Definition broom-queue.h:12
void register_external(const void *pointer, size_t size)
Definition api.cc:41
BroomSharingPointer< T > sharing_ptr
Definition broom.h:680
void force_slow_collection()
Definition api.cc:40
sharing_ptr< T > share(T *ptr, uint32_t count=1)
Definition broom.h:699
BroomDisableGcScope & operator=(BroomDisableGcScope &&other) noexcept=delete
BroomDisableGcScope(const BroomDisableGcScope &other)=delete
BroomDisableGcScope & operator=(const BroomDisableGcScope &other)=delete
BroomDisableGcScope(BroomDisableGcScope &&other)=delete
BroomNoDestructorScope & operator=(BroomNoDestructorScope &&other)=delete
BroomNoDestructorScope(const BroomNoDestructorScope &other)=delete
BroomNoDestructorScope(broom_configuration config={})
Definition broom.h:146
BroomNoDestructorScope & operator=(const BroomNoDestructorScope &other)=delete
BroomNoDestructorScope(BroomNoDestructorScope &&other)=delete
BroomScope(BroomScope &&other)=delete
BroomScope & operator=(BroomScope &&other)=delete
BroomScope(const BroomScope &other)=delete
BroomScope & operator=(const BroomScope &other)=delete
BroomScope(broom_configuration config={})
Definition broom.h:117
BroomSharingNoDestructorScope(BroomSharingNoDestructorScope &&other)=delete
BroomSharingNoDestructorScope(broom_configuration config={})
Definition broom.h:209
BroomSharingNoDestructorScope(const BroomSharingNoDestructorScope &other)=delete
BroomSharingNoDestructorScope & operator=(const BroomSharingNoDestructorScope &other)=delete
BroomSharingNoDestructorScope & operator=(BroomSharingNoDestructorScope &&other)=delete
BroomSharingScope & operator=(BroomSharingScope &&other)=delete
BroomSharingScope(const BroomSharingScope &other)=delete
BroomSharingScope(BroomSharingScope &&other)=delete
BroomSharingScope(broom_configuration config={})
Definition broom.h:177
BroomSharingScope & operator=(const BroomSharingScope &other)=delete
bool run_destructors_on_shutdown
Definition broom.h:72