Skip to content

Commit

Permalink
Replaces goto with deferer object
Browse files Browse the repository at this point in the history
  • Loading branch information
vimes committed Dec 12, 2024
1 parent 7259efa commit cd20fd0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions common/deferer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace utils
{

template<typename F>
class Deferer
{
F action_;

public:
Deferer(F&& action) :
action_{std::move(action)}
{}
Deferer(const Deferer& other) = delete;
Deferer(Deferer&& other) = delete;
Deferer& operator=(const Deferer& other) = delete;
Deferer& operator=(Deferer&& other) = delete;
~Deferer()
{
action_();
}
};

} // namespace utils
6 changes: 3 additions & 3 deletions dataplane/globalbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "worker_gc.h"

#include "common/counters.h"
#include "common/deferer.h"
#include "common/define.h"

#include "debug_latch.h"
Expand Down Expand Up @@ -1612,6 +1613,7 @@ balancer_real_id_t* generation::rebuild_service_ring_one_wrr(
const balancer_real_id_t* const do_not_exceed,
const balancer_service_t* service)
{
utils::Deferer defer([]() { YADECAP_MEMORY_BARRIER_COMPILE; });
for (uint32_t real_idx = service->real_start;
real_idx < service->real_start + service->real_size;
++real_idx)
Expand All @@ -1631,15 +1633,13 @@ balancer_real_id_t* generation::rebuild_service_ring_one_wrr(
if (start == do_not_exceed)
{
YANET_LOG_ERROR("Balancer service exceeded ring chunk bounds\n");
goto finish;
return start;
}
*start = real_id;
++start;
}
}

finish:
YADECAP_MEMORY_BARRIER_COMPILE;
return start;
}

Expand Down

0 comments on commit cd20fd0

Please sign in to comment.