Skip to content

Commit

Permalink
new sp
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed May 5, 2024
1 parent 25964e5 commit 6b7d9a5
Show file tree
Hide file tree
Showing 65 changed files with 473 additions and 197 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ if(CMAKE_BUILD_TYPE MATCHES Debug OR CMAKE_BUILD_TYPE MATCHES DEBUG)
message(STATUS "Enabling ASan")

target_link_libraries(Hyprland asan)
pkg_check_modules(ffidep REQUIRED IMPORTED_TARGET libffi)
target_link_libraries(Hyprland ${CMAKE_SOURCE_DIR}/libwayland-server.a PkgConfig::ffidep)
target_compile_options(Hyprland PUBLIC -fsanitize=address)
endif()

Expand Down
2 changes: 1 addition & 1 deletion src/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ void CCompositor::sanityCheckWorkspaces() {
const auto& WORKSPACE = *it;

// If ref == 1, only the compositor holds a ref, which means it's inactive and has no mapped windows.
if (!WORKSPACE->m_bPersistent && WORKSPACE.use_count() == 1) {
if (!WORKSPACE->m_bPersistent && WORKSPACE.strongRef() == 1) {
it = m_vWorkspaces.erase(it);
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions src/debug/HyprCtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1588,11 +1588,11 @@ CHyprCtl::CHyprCtl() {
startHyprCtlSocket();
}

std::shared_ptr<SHyprCtlCommand> CHyprCtl::registerCommand(SHyprCtlCommand cmd) {
return m_vCommands.emplace_back(std::make_shared<SHyprCtlCommand>(cmd));
SP<SHyprCtlCommand> CHyprCtl::registerCommand(SHyprCtlCommand cmd) {
return m_vCommands.emplace_back(makeShared<SHyprCtlCommand>(cmd));
}

void CHyprCtl::unregisterCommand(const std::shared_ptr<SHyprCtlCommand>& cmd) {
void CHyprCtl::unregisterCommand(const SP<SHyprCtlCommand>& cmd) {
std::erase(m_vCommands, cmd);
}

Expand Down
14 changes: 7 additions & 7 deletions src/debug/HyprCtl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ class CHyprCtl {
public:
CHyprCtl();

std::string makeDynamicCall(const std::string& input);
std::shared_ptr<SHyprCtlCommand> registerCommand(SHyprCtlCommand cmd);
void unregisterCommand(const std::shared_ptr<SHyprCtlCommand>& cmd);
std::string getReply(std::string);
std::string makeDynamicCall(const std::string& input);
SP<SHyprCtlCommand> registerCommand(SHyprCtlCommand cmd);
void unregisterCommand(const SP<SHyprCtlCommand>& cmd);
std::string getReply(std::string);

int m_iSocketFD = -1;
int m_iSocketFD = -1;

struct {
bool all = false;
} m_sCurrentRequestParams;

private:
void startHyprCtlSocket();
void startHyprCtlSocket();

std::vector<std::shared_ptr<SHyprCtlCommand>> m_vCommands;
std::vector<SP<SHyprCtlCommand>> m_vCommands;
};

inline std::unique_ptr<CHyprCtl> g_pHyprCtl;
6 changes: 2 additions & 4 deletions src/desktop/DesktopTypes.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#pragma once
#include <memory>

#include "../macros.hpp"
class CWorkspace;

typedef std::shared_ptr<CWorkspace> PHLWORKSPACE;
typedef SP<CWorkspace> PHLWORKSPACE;
2 changes: 1 addition & 1 deletion src/desktop/LayerSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static void onDestroy(void* owner, void* data) {
// IMPL

PHLLS CLayerSurface::create(wlr_layer_surface_v1* pWLRLS) {
PHLLS pLS = std::shared_ptr<CLayerSurface>(new CLayerSurface);
PHLLS pLS = SP<CLayerSurface>(new CLayerSurface);

auto PMONITOR = g_pCompositor->getMonitorFromOutput(pWLRLS->output);

Expand Down
4 changes: 2 additions & 2 deletions src/desktop/WLSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ std::optional<CBox> CWLSurface::getSurfaceBoxGlobal() {
return {};
}

void CWLSurface::appendConstraint(std::weak_ptr<CPointerConstraint> constraint) {
void CWLSurface::appendConstraint(WP<CPointerConstraint> constraint) {
m_pConstraint = constraint;
}

void CWLSurface::onCommit() {
;
}

std::shared_ptr<CPointerConstraint> CWLSurface::constraint() {
SP<CPointerConstraint> CWLSurface::constraint() {
return m_pConstraint.lock();
}
14 changes: 7 additions & 7 deletions src/desktop/WLSurface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class CWLSurface {
CSubsurface* getSubsurface();

// desktop components misc utils
std::optional<CBox> getSurfaceBoxGlobal();
void appendConstraint(std::weak_ptr<CPointerConstraint> constraint);
std::shared_ptr<CPointerConstraint> constraint();
std::optional<CBox> getSurfaceBoxGlobal();
void appendConstraint(WP<CPointerConstraint> constraint);
SP<CPointerConstraint> constraint();

// allow stretching. Useful for plugins.
bool m_bFillIgnoreSmall = false;
Expand Down Expand Up @@ -98,11 +98,11 @@ class CWLSurface {
CSubsurface* m_pSubsurfaceOwner = nullptr;

//
std::weak_ptr<CPointerConstraint> m_pConstraint;
WP<CPointerConstraint> m_pConstraint;

void destroy();
void init();
bool desktopComponent();
void destroy();
void init();
bool desktopComponent();

DYNLISTENER(destroy);
DYNLISTENER(commit);
Expand Down
2 changes: 1 addition & 1 deletion src/desktop/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "../managers/TokenManager.hpp"

PHLWINDOW CWindow::create() {
PHLWINDOW pWindow = std::shared_ptr<CWindow>(new CWindow);
PHLWINDOW pWindow = SP<CWindow>(new CWindow);

pWindow->m_pSelf = pWindow;

Expand Down
2 changes: 1 addition & 1 deletion src/desktop/Workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "../config/ConfigValue.hpp"

PHLWORKSPACE CWorkspace::create(int id, int monitorID, std::string name, bool special) {
PHLWORKSPACE workspace = std::make_shared<CWorkspace>(id, monitorID, name, special);
PHLWORKSPACE workspace = makeShared<CWorkspace>(id, monitorID, name, special);
workspace->init(workspace);
return workspace;
}
Expand Down
8 changes: 4 additions & 4 deletions src/desktop/Workspace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ class CWorkspace {
void markInert();

private:
void init(PHLWORKSPACE self);
void init(PHLWORKSPACE self);

std::shared_ptr<HOOK_CALLBACK_FN> m_pFocusedWindowHook;
bool m_bInert = true;
std::weak_ptr<CWorkspace> m_pSelf;
SP<HOOK_CALLBACK_FN> m_pFocusedWindowHook;
bool m_bInert = true;
WP<CWorkspace> m_pSelf;
};

inline bool valid(const PHLWORKSPACE& ref) {
Expand Down
8 changes: 4 additions & 4 deletions src/events/Monitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// //
// --------------------------------------------------------- //

static void checkDefaultCursorWarp(std::shared_ptr<CMonitor>* PNEWMONITORWRAP, std::string monitorName) {
static void checkDefaultCursorWarp(SP<CMonitor>* PNEWMONITORWRAP, std::string monitorName) {
const auto PNEWMONITOR = PNEWMONITORWRAP->get();

static auto PCURSORMONITOR = CConfigValue<std::string>("general:default_cursor_monitor");
Expand Down Expand Up @@ -61,9 +61,9 @@ void Events::listener_newOutput(wl_listener* listener, void* data) {
}

// add it to real
std::shared_ptr<CMonitor>* PNEWMONITORWRAP = nullptr;
SP<CMonitor>* PNEWMONITORWRAP = nullptr;

PNEWMONITORWRAP = &g_pCompositor->m_vRealMonitors.emplace_back(std::make_shared<CMonitor>());
PNEWMONITORWRAP = &g_pCompositor->m_vRealMonitors.emplace_back(makeShared<CMonitor>());
if (std::string("HEADLESS-1") == OUTPUT->name)
g_pCompositor->m_pUnsafeOutput = PNEWMONITORWRAP->get();

Expand Down Expand Up @@ -196,7 +196,7 @@ void Events::listener_monitorDestroy(void* owner, void* data) {

Debug::log(LOG, "Removing monitor {} from realMonitors", pMonitor->szName);

std::erase_if(g_pCompositor->m_vRealMonitors, [&](std::shared_ptr<CMonitor>& el) { return el.get() == pMonitor; });
std::erase_if(g_pCompositor->m_vRealMonitors, [&](SP<CMonitor>& el) { return el.get() == pMonitor; });
}

void Events::listener_monitorStateRequest(void* owner, void* data) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/AnimatedVariable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class CBaseAnimatedVariable {

protected:
PHLWINDOWREF m_pWindow;
std::weak_ptr<CWorkspace> m_pWorkspace;
WP<CWorkspace> m_pWorkspace;
PHLLSREF m_pLayer;

SAnimationPropertyConfig* m_pConfig = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/Monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void CMonitor::onConnect(bool noRule) {
m_bRenderingInitPassed = true;
}

std::shared_ptr<CMonitor>* thisWrapper = nullptr;
SP<CMonitor>* thisWrapper = nullptr;

// find the wrap
for (auto& m : g_pCompositor->m_vRealMonitors) {
Expand Down Expand Up @@ -337,7 +337,7 @@ void CMonitor::onDisconnect(bool destroy) {

g_pHyprRenderer->m_pMostHzMonitor = pMonitorMostHz;
}
std::erase_if(g_pCompositor->m_vMonitors, [&](std::shared_ptr<CMonitor>& el) { return el.get() == this; });
std::erase_if(g_pCompositor->m_vMonitors, [&](SP<CMonitor>& el) { return el.get() == this; });
}

void CMonitor::addDamage(const pixman_region32_t* rg) {
Expand Down Expand Up @@ -464,7 +464,7 @@ void CMonitor::setMirror(const std::string& mirrorOf) {

// push to mvmonitors

std::shared_ptr<CMonitor>* thisWrapper = nullptr;
SP<CMonitor>* thisWrapper = nullptr;

// find the wrap
for (auto& m : g_pCompositor->m_vRealMonitors) {
Expand Down
154 changes: 154 additions & 0 deletions src/helpers/memory/SharedPtr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#pragma once

#include <typeinfo>

namespace CSharedPointer_ {
struct impl {
/* strong refcount */
unsigned int ref = 0;
/* weak refcount */
unsigned int weak = 0;

/* the actual stored data */
void* data = nullptr;
/* if the destructor was called,
creating shared_ptrs is no longer valid */
bool destroying = false;
};
};

template <typename T>
class CSharedPointer {
public:
template <typename X>
using validHierarchy = typename std::enable_if<std::is_assignable<T*, X*>::value>;

/* creates a new shared pointer managing a resource
avoid calling. Could duplicate ownership. Prefer makeShared */
explicit CSharedPointer(T* object) {
impl_ = new CSharedPointer_::impl();
impl_->data = object;
increment();
}

/* creates a shared pointer from a reference */
template <typename U, typename = validHierarchy<U>>
explicit CSharedPointer(const CSharedPointer<U>& ref) {
impl_ = ref.impl_;
increment();
}

/* allows weakPointer to create from an impl */
explicit CSharedPointer(CSharedPointer_::impl* implementation) {
impl_ = implementation;
increment();
}

/* creates an empty shared pointer with no implementation */
CSharedPointer() {
; // empty
}

/* creates an empty shared pointer with no implementation */
CSharedPointer(std::nullptr_t) {
; // empty
}

~CSharedPointer() {
// we do not decrement here,
// because we want to preserve the pointer
// in case this is the last owner.
if (impl_ && impl_->ref == 1)
destroyImpl();
else
decrement();
}

template <typename U, typename = validHierarchy<U>>
CSharedPointer<T>& operator=(const CSharedPointer<U>& rhs) {
if (impl_ == rhs.impl_)
return *this;

decrement();
impl_ = rhs.impl_;
increment();
}

operator bool() const {
return impl_ && impl_->data;
}

bool operator==(const CSharedPointer& rhs) const {
return impl_ == rhs.impl_;
}

T* operator->() const {
return get();
}

T& operator*() const {
return *get();
}

void reset() {
decrement();
impl_ = nullptr;
}

T* get() const {
return (T*)(impl_ ? impl_->data : nullptr);
}

unsigned int strongRef() const {
return impl_ ? impl_->ref : 0;
}

CSharedPointer_::impl* impl_ = nullptr;

private:
/*
no-op if there is no impl_
may delete the stored object if ref == 0
may delete and reset impl_ if ref == 0 and weak == 0
*/
void decrement() {
if (!impl_)
return;

impl_->ref--;

// if ref == 0, we can destroy impl
if (impl_->ref == 0)
destroyImpl();
}
/* no-op if there is no impl_ */
void increment() {
if (!impl_)
return;

impl_->ref++;
}

/* destroy the pointed-to object
if able, will also destroy impl */
void destroyImpl() {
// first, we destroy the data, but keep the pointer.
// this way, weak pointers will still be able to
// reference and use, but no longer create shared ones.
impl_->destroying = true;
delete (T*)impl_->data;
// now, we can reset the data and call it a day.
impl_->data = nullptr;
impl_->destroying = false;

// check for weak refs, if zero, we can also delete impl_
if (impl_->weak == 0)
delete impl_;
}
};

template <typename U, typename... Args>
static CSharedPointer<U> makeShared(Args&&... args) {
auto p = CSharedPointer<U>(new U(std::forward<Args>(args)...));
return p;
}
Loading

0 comments on commit 6b7d9a5

Please sign in to comment.