Commit 21476d05 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Use unique_ptrs in MessagePumpLibevent::FileDescriptorWatcher.

Change-Id: I14e783b5adb905cf27731c55e224f7b0311e2b8e
Reviewed-on: https://chromium-review.googlesource.com/936010Reviewed-by: default avatarBen Chan <benchan@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#546022}
parent 5a9bcc28
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <memory> #include <utility>
#include "base/auto_reset.h" #include "base/auto_reset.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
...@@ -45,11 +45,7 @@ namespace base { ...@@ -45,11 +45,7 @@ namespace base {
MessagePumpLibevent::FileDescriptorWatcher::FileDescriptorWatcher( MessagePumpLibevent::FileDescriptorWatcher::FileDescriptorWatcher(
const Location& from_here) const Location& from_here)
: event_(nullptr), : created_from_location_(from_here) {}
pump_(nullptr),
watcher_(nullptr),
was_destroyed_(nullptr),
created_from_location_(from_here) {}
MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() { MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() {
if (event_) { if (event_) {
...@@ -62,29 +58,28 @@ MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() { ...@@ -62,29 +58,28 @@ MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() {
} }
bool MessagePumpLibevent::FileDescriptorWatcher::StopWatchingFileDescriptor() { bool MessagePumpLibevent::FileDescriptorWatcher::StopWatchingFileDescriptor() {
event* e = ReleaseEvent(); std::unique_ptr<event> e = ReleaseEvent();
if (e == nullptr) if (!e)
return true; return true;
// event_del() is a no-op if the event isn't active. // event_del() is a no-op if the event isn't active.
int rv = event_del(e); int rv = event_del(e.get());
delete e;
pump_ = nullptr; pump_ = nullptr;
watcher_ = nullptr; watcher_ = nullptr;
return (rv == 0); return (rv == 0);
} }
void MessagePumpLibevent::FileDescriptorWatcher::Init(event* e) { void MessagePumpLibevent::FileDescriptorWatcher::Init(
std::unique_ptr<event> e) {
DCHECK(e); DCHECK(e);
DCHECK(!event_); DCHECK(!event_);
event_ = e; event_ = std::move(e);
} }
event* MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() { std::unique_ptr<event>
struct event* e = event_; MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() {
event_ = nullptr; return std::move(event_);
return e;
} }
void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking(
...@@ -189,12 +184,9 @@ bool MessagePumpLibevent::WatchFileDescriptor(int fd, ...@@ -189,12 +184,9 @@ bool MessagePumpLibevent::WatchFileDescriptor(int fd,
return false; return false;
} }
// Transfer ownership of evt to controller. controller->Init(std::move(evt));
controller->Init(evt.release());
controller->set_watcher(delegate); controller->set_watcher(delegate);
controller->set_pump(this); controller->set_pump(this);
return true; return true;
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_ #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_
#define BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_ #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_
#include <memory>
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/location.h" #include "base/location.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -54,12 +56,11 @@ class BASE_EXPORT MessagePumpLibevent : public MessagePump { ...@@ -54,12 +56,11 @@ class BASE_EXPORT MessagePumpLibevent : public MessagePump {
friend class MessagePumpLibevent; friend class MessagePumpLibevent;
friend class MessagePumpLibeventTest; friend class MessagePumpLibeventTest;
// Called by MessagePumpLibevent, ownership of |e| is transferred to this // Called by MessagePumpLibevent.
// object. void Init(std::unique_ptr<event> e);
void Init(event* e);
// Used by MessagePumpLibevent to take ownership of event_. // Used by MessagePumpLibevent to take ownership of |event_|.
event* ReleaseEvent(); std::unique_ptr<event> ReleaseEvent();
void set_pump(MessagePumpLibevent* pump) { pump_ = pump; } void set_pump(MessagePumpLibevent* pump) { pump_ = pump; }
MessagePumpLibevent* pump() const { return pump_; } MessagePumpLibevent* pump() const { return pump_; }
...@@ -69,12 +70,12 @@ class BASE_EXPORT MessagePumpLibevent : public MessagePump { ...@@ -69,12 +70,12 @@ class BASE_EXPORT MessagePumpLibevent : public MessagePump {
void OnFileCanReadWithoutBlocking(int fd, MessagePumpLibevent* pump); void OnFileCanReadWithoutBlocking(int fd, MessagePumpLibevent* pump);
void OnFileCanWriteWithoutBlocking(int fd, MessagePumpLibevent* pump); void OnFileCanWriteWithoutBlocking(int fd, MessagePumpLibevent* pump);
event* event_; std::unique_ptr<event> event_;
MessagePumpLibevent* pump_; MessagePumpLibevent* pump_ = nullptr;
Watcher* watcher_; Watcher* watcher_ = nullptr;
// If this pointer is non-NULL, the pointee is set to true in the // If this pointer is non-NULL, the pointee is set to true in the
// destructor. // destructor.
bool* was_destroyed_; bool* was_destroyed_ = nullptr;
const Location created_from_location_; const Location created_from_location_;
......
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