adb: Remove most C-style allocations
This change gets rid of most malloc/calloc/free calls. The future is
now!
Bug: None
Test: test_device.py
Change-Id: Iccfe3bd4fe45a0319bd9f23b8cbff4c7070c9f4d
diff --git a/adb/client/commandline.cpp b/adb/client/commandline.cpp
index 80d0dd3..7791895 100644
--- a/adb/client/commandline.cpp
+++ b/adb/client/commandline.cpp
@@ -362,9 +362,8 @@
}
static void copy_to_file(int inFd, int outFd) {
- const size_t BUFSIZE = 32 * 1024;
- char* buf = (char*) malloc(BUFSIZE);
- if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file");
+ constexpr size_t BUFSIZE = 32 * 1024;
+ std::vector<char> buf(BUFSIZE);
int len;
long total = 0;
int old_stdin_mode = -1;
@@ -376,9 +375,9 @@
while (true) {
if (inFd == STDIN_FILENO) {
- len = unix_read(inFd, buf, BUFSIZE);
+ len = unix_read(inFd, buf.data(), BUFSIZE);
} else {
- len = adb_read(inFd, buf, BUFSIZE);
+ len = adb_read(inFd, buf.data(), BUFSIZE);
}
if (len == 0) {
D("copy_to_file() : read 0 bytes; exiting");
@@ -389,10 +388,10 @@
break;
}
if (outFd == STDOUT_FILENO) {
- fwrite(buf, 1, len, stdout);
+ fwrite(buf.data(), 1, len, stdout);
fflush(stdout);
} else {
- adb_write(outFd, buf, len);
+ adb_write(outFd, buf.data(), len);
}
total += len;
}
@@ -400,7 +399,6 @@
stdinout_raw_epilogue(inFd, outFd, old_stdin_mode, old_stdout_mode);
D("copy_to_file() finished after %lu bytes", total);
- free(buf);
}
static void send_window_size_change(int fd, std::unique_ptr<ShellProtocol>& shell) {
@@ -1142,24 +1140,22 @@
static void write_zeros(int bytes, int fd) {
int old_stdin_mode = -1;
int old_stdout_mode = -1;
- char* buf = (char*) calloc(1, bytes);
- if (buf == nullptr) fatal("couldn't allocate buffer for write_zeros");
+ std::vector<char> buf(bytes);
D("write_zeros(%d) -> %d", bytes, fd);
stdinout_raw_prologue(-1, fd, old_stdin_mode, old_stdout_mode);
if (fd == STDOUT_FILENO) {
- fwrite(buf, 1, bytes, stdout);
+ fwrite(buf.data(), 1, bytes, stdout);
fflush(stdout);
} else {
- adb_write(fd, buf, bytes);
+ adb_write(fd, buf.data(), bytes);
}
stdinout_raw_prologue(-1, fd, old_stdin_mode, old_stdout_mode);
D("write_zeros() finished");
- free(buf);
}
static int backup(int argc, const char** argv) {