adb: remove fdevent_install, fdevent_remove.
Remove fdevent_install and fdevent_remove in favor of using
fdevent_create and fdevent_destroy, so that we can put RAII types (i.e.
unique_fd) into fdevent without worrying about -Wexit-time-destructors
or structs that are freed instead of deleted.
Bug: http://b/79786774
Test: python test_device.py
Change-Id: I8471cc00574ed492fe1b196944976cdaae8b7cff
diff --git a/adb/fdevent.cpp b/adb/fdevent.cpp
index cf441cf..b3ff457 100644
--- a/adb/fdevent.cpp
+++ b/adb/fdevent.cpp
@@ -117,29 +117,17 @@
return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
}
-fdevent* fdevent_create(int fd, fd_func func, void* arg) {
- check_main_thread();
- fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
- if(fde == 0) return 0;
- fdevent_install(fde, fd, func, arg);
- fde->state |= FDE_CREATED;
- return fde;
-}
-
-void fdevent_destroy(fdevent* fde) {
- check_main_thread();
- if(fde == 0) return;
- if(!(fde->state & FDE_CREATED)) {
- LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
- }
- fdevent_remove(fde);
- free(fde);
-}
-
void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
check_main_thread();
CHECK_GE(fd, 0);
memset(fde, 0, sizeof(fdevent));
+}
+
+fdevent* fdevent_create(int fd, fd_func func, void* arg) {
+ check_main_thread();
+ CHECK_GE(fd, 0);
+
+ fdevent* fde = new fdevent();
fde->state = FDE_ACTIVE;
fde->fd = fd;
fde->func = func;
@@ -152,12 +140,18 @@
}
auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
CHECK(pair.second) << "install existing fd " << fd;
- D("fdevent_install %s", dump_fde(fde).c_str());
+
+ fde->state |= FDE_CREATED;
+ return fde;
}
-void fdevent_remove(fdevent* fde) {
+void fdevent_destroy(fdevent* fde) {
check_main_thread();
- D("fdevent_remove %s", dump_fde(fde).c_str());
+ if (fde == 0) return;
+ if (!(fde->state & FDE_CREATED)) {
+ LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
+ }
+
if (fde->state & FDE_ACTIVE) {
g_poll_node_map.erase(fde->fd);
if (fde->state & FDE_PENDING) {
@@ -170,6 +164,8 @@
fde->state = 0;
fde->events = 0;
}
+
+ delete fde;
}
static void fdevent_update(fdevent* fde, unsigned events) {