summaryrefslogtreecommitdiffstats
path: root/adb/client/commandline.cpp
diff options
context:
space:
mode:
authorLuis Hector Chavez <lhchavez@google.com>2018-07-18 21:18:27 -0700
committerLuis Hector Chavez <lhchavez@google.com>2018-07-19 09:41:40 -0700
commit6150a37dbe6c683285e98d7bd7ac1abf17ec84b4 (patch)
tree789b07fcded328049f2fe9e0f576aaa190e9d1cf /adb/client/commandline.cpp
parent095792c300d0563904224c5a81ad0709bcd234f6 (diff)
downloadsystem_core-6150a37dbe6c683285e98d7bd7ac1abf17ec84b4.tar.gz
system_core-6150a37dbe6c683285e98d7bd7ac1abf17ec84b4.tar.bz2
system_core-6150a37dbe6c683285e98d7bd7ac1abf17ec84b4.zip
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
Diffstat (limited to 'adb/client/commandline.cpp')
-rw-r--r--adb/client/commandline.cpp22
1 files changed, 9 insertions, 13 deletions
diff --git a/adb/client/commandline.cpp b/adb/client/commandline.cpp
index 80d0dd3d1..7791895f6 100644
--- a/adb/client/commandline.cpp
+++ b/adb/client/commandline.cpp
@@ -362,9 +362,8 @@ static void stdinout_raw_epilogue(int inFd, int outFd, int old_stdin_mode, int o
}
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 @@ static void copy_to_file(int inFd, int outFd) {
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 @@ static void copy_to_file(int inFd, int outFd) {
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 @@ static void copy_to_file(int inFd, int outFd) {
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 int logcat(int argc, const char** argv) {
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) {