Merge "adb: use delete on objects with destructors."

This commit is contained in:
Josh Gao 2018-02-13 01:37:46 +00:00 committed by Gerrit Code Review
commit e504360f3d
4 changed files with 26 additions and 34 deletions

View file

@ -453,7 +453,7 @@ static void jdwp_control_event(int s, unsigned events, void* _control) {
**/ **/
struct JdwpSocket : public asocket { struct JdwpSocket : public asocket {
bool pass; bool pass = false;
}; };
static void jdwp_socket_close(asocket* s) { static void jdwp_socket_close(asocket* s) {
@ -467,7 +467,7 @@ static void jdwp_socket_close(asocket* s) {
} }
remove_socket(s); remove_socket(s);
free(s); delete s;
} }
static int jdwp_socket_enqueue(asocket* s, std::string) { static int jdwp_socket_enqueue(asocket* s, std::string) {
@ -497,7 +497,7 @@ static void jdwp_socket_ready(asocket* s) {
} }
asocket* create_jdwp_service_socket(void) { asocket* create_jdwp_service_socket(void) {
JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1)); JdwpSocket* s = new JdwpSocket();
if (!s) { if (!s) {
fatal("failed to allocate JdwpSocket"); fatal("failed to allocate JdwpSocket");

View file

@ -36,31 +36,31 @@ class atransport;
struct asocket { struct asocket {
/* the unique identifier for this asocket /* the unique identifier for this asocket
*/ */
unsigned id; unsigned id = 0;
/* flag: set when the socket's peer has closed /* flag: set when the socket's peer has closed
* but packets are still queued for delivery * but packets are still queued for delivery
*/ */
int closing; int closing = 0;
// flag: set when the socket failed to write, so the socket will not wait to // flag: set when the socket failed to write, so the socket will not wait to
// write packets and close directly. // write packets and close directly.
bool has_write_error; bool has_write_error = 0;
/* flag: quit adbd when both ends close the /* flag: quit adbd when both ends close the
* local service socket * local service socket
*/ */
int exit_on_close; int exit_on_close = 0;
// the asocket we are connected to // the asocket we are connected to
asocket* peer; asocket* peer = nullptr;
/* For local asockets, the fde is used to bind /* For local asockets, the fde is used to bind
* us to our fd event system. For remote asockets * us to our fd event system. For remote asockets
* these fields are not used. * these fields are not used.
*/ */
fdevent fde; fdevent fde = {0};
int fd; int fd = 0;
// queue of data waiting to be written // queue of data waiting to be written
std::deque<Range> packet_queue; std::deque<Range> packet_queue;
@ -73,27 +73,27 @@ struct asocket {
* peer->ready() when we once again are ready to * peer->ready() when we once again are ready to
* receive data. * receive data.
*/ */
int (*enqueue)(asocket* s, std::string data); int (*enqueue)(asocket* s, std::string data) = nullptr;
/* ready is called by the peer when it is ready for /* ready is called by the peer when it is ready for
* us to send data via enqueue again * us to send data via enqueue again
*/ */
void (*ready)(asocket* s); void (*ready)(asocket* s) = nullptr;
/* shutdown is called by the peer before it goes away. /* shutdown is called by the peer before it goes away.
* the socket should not do any further calls on its peer. * the socket should not do any further calls on its peer.
* Always followed by a call to close. Optional, i.e. can be NULL. * Always followed by a call to close. Optional, i.e. can be NULL.
*/ */
void (*shutdown)(asocket* s); void (*shutdown)(asocket* s) = nullptr;
/* close is called by the peer when it has gone away. /* close is called by the peer when it has gone away.
* we are not allowed to make any further calls on the * we are not allowed to make any further calls on the
* peer once our close method is called. * peer once our close method is called.
*/ */
void (*close)(asocket* s); void (*close)(asocket* s) = nullptr;
/* A socket is bound to atransport */ /* A socket is bound to atransport */
atransport* transport; atransport* transport = nullptr;
size_t get_max_payload() const; size_t get_max_payload() const;
}; };

View file

@ -170,7 +170,7 @@ static void local_socket_destroy(asocket* s) {
fdevent_remove(&s->fde); fdevent_remove(&s->fde);
remove_socket(s); remove_socket(s);
free(s); delete s;
if (exit_on_close) { if (exit_on_close) {
D("local_socket_destroy: exiting"); D("local_socket_destroy: exiting");
@ -347,10 +347,7 @@ static void local_socket_event_func(int fd, unsigned ev, void* _s) {
} }
asocket* create_local_socket(int fd) { asocket* create_local_socket(int fd) {
asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); asocket* s = new asocket();
if (s == NULL) {
fatal("cannot allocate socket");
}
s->fd = fd; s->fd = fd;
s->enqueue = local_socket_enqueue; s->enqueue = local_socket_enqueue;
s->ready = local_socket_ready; s->ready = local_socket_ready;
@ -459,7 +456,7 @@ static void remote_socket_close(asocket* s) {
D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd, D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
s->peer ? s->peer->fd : -1); s->peer ? s->peer->fd : -1);
D("RS(%d): closed", s->id); D("RS(%d): closed", s->id);
free(s); delete s;
} }
// Create a remote socket to exchange packets with a remote service through transport // Create a remote socket to exchange packets with a remote service through transport
@ -470,11 +467,7 @@ asocket* create_remote_socket(unsigned id, atransport* t) {
if (id == 0) { if (id == 0) {
fatal("invalid remote socket id (0)"); fatal("invalid remote socket id (0)");
} }
asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); asocket* s = new asocket();
if (s == NULL) {
fatal("cannot allocate socket");
}
s->id = id; s->id = id;
s->enqueue = remote_socket_enqueue; s->enqueue = remote_socket_enqueue;
s->ready = remote_socket_ready; s->ready = remote_socket_ready;
@ -811,13 +804,12 @@ static void smart_socket_close(asocket* s) {
s->peer->close(s->peer); s->peer->close(s->peer);
s->peer = 0; s->peer = 0;
} }
free(s); delete s;
} }
static asocket* create_smart_socket(void) { static asocket* create_smart_socket(void) {
D("Creating smart socket"); D("Creating smart socket");
asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); asocket* s = new asocket();
if (s == NULL) fatal("cannot allocate socket");
s->enqueue = smart_socket_enqueue; s->enqueue = smart_socket_enqueue;
s->ready = smart_socket_ready; s->ready = smart_socket_ready;
s->shutdown = NULL; s->shutdown = NULL;

View file

@ -378,9 +378,9 @@ static fdevent transport_registration_fde;
*/ */
struct device_tracker { struct device_tracker {
asocket socket; asocket socket;
bool update_needed; bool update_needed = false;
bool long_output; bool long_output = false;
device_tracker* next; device_tracker* next = nullptr;
}; };
/* linked list of all device trackers */ /* linked list of all device trackers */
@ -411,7 +411,7 @@ static void device_tracker_close(asocket* socket) {
peer->close(peer); peer->close(peer);
} }
device_tracker_remove(tracker); device_tracker_remove(tracker);
free(tracker); delete tracker;
} }
static int device_tracker_enqueue(asocket* socket, std::string) { static int device_tracker_enqueue(asocket* socket, std::string) {
@ -446,7 +446,7 @@ static void device_tracker_ready(asocket* socket) {
} }
asocket* create_device_tracker(bool long_output) { asocket* create_device_tracker(bool long_output) {
device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker))); device_tracker* tracker = new device_tracker();
if (tracker == nullptr) fatal("cannot allocate device tracker"); if (tracker == nullptr) fatal("cannot allocate device tracker");
D("device tracker %p created", tracker); D("device tracker %p created", tracker);