diff options
author | Seungha Son <seungha.son@samsung.com> | 2020-04-07 10:38:42 +0900 |
---|---|---|
committer | Seungha Son <seungha.son@samsung.com> | 2020-04-08 10:10:28 +0900 |
commit | 016367713f8719af4c1c4578c8aa7d6215f678e3 (patch) | |
tree | ebf1e0224f5a0a1f0abb83450ff5af05db770a02 | |
parent | bd60a24eff02544cf14d1ca37a85dcf5dbd5601a (diff) | |
download | ttrace-016367713f8719af4c1c4578c8aa7d6215f678e3.tar.gz ttrace-016367713f8719af4c1c4578c8aa7d6215f678e3.tar.bz2 ttrace-016367713f8719af4c1c4578c8aa7d6215f678e3.zip |
Refactor resource managements
Buffers, managed by malloc() and free(), are replaced with
std::unique_ptr type, so it will automatically deleted when going
out of scope.
Change-Id: I117bea7d0a980c94c0481f3e470da2a626ca4595
Signed-off-by: Seungha Son <seungha.son@samsung.com>
-rwxr-xr-x | src/atrace/atrace.cpp | 29 |
1 files changed, 11 insertions, 18 deletions
diff --git a/src/atrace/atrace.cpp b/src/atrace/atrace.cpp index 81ea9d8..f4a0638 100755 --- a/src/atrace/atrace.cpp +++ b/src/atrace/atrace.cpp @@ -35,6 +35,7 @@ #include <sys/types.h> #include <unistd.h> +#include <memory> #include <fstream> #include "ttrace.h" @@ -708,7 +709,6 @@ static void dumpTrace(bool startup) if (g_compress) { z_stream zs; - uint8_t *in, *out; int result, flush; bzero(&zs, sizeof(zs)); @@ -722,14 +722,10 @@ static void dumpTrace(bool startup) } const size_t bufSize = 64*1024; - in = (uint8_t*)malloc(bufSize); - out = (uint8_t*)malloc(bufSize); + std::unique_ptr<uint8_t> in(new uint8_t[bufSize]); + std::unique_ptr<uint8_t> out(new uint8_t[bufSize]); if ((in == nullptr) || (out == nullptr)) { fprintf(stderr, "Could not allocate memory"); - if (in != nullptr) - free(in); - if (out != nullptr) - free(out); close(traceFD); if (backup_fd > -1) close(backup_fd); @@ -737,14 +733,14 @@ static void dumpTrace(bool startup) } flush = Z_NO_FLUSH; - zs.next_out = out; + zs.next_out = reinterpret_cast<Bytef*>(out.get()); zs.avail_out = bufSize; do { if (zs.avail_in == 0) { // More input is needed. - result = read(traceFD, in, bufSize); + result = read(traceFD, in.get(), bufSize); if (result < 0) { fprintf(stderr, "error reading trace: %s (%d)\n", strerror_r(errno, str_error, sizeof(str_error)), errno); @@ -753,7 +749,7 @@ static void dumpTrace(bool startup) } else if (result == 0) { flush = Z_FINISH; } else { - zs.next_in = in; + zs.next_in = reinterpret_cast<Bytef*>(in.get()); zs.avail_in = result; } } @@ -761,9 +757,9 @@ static void dumpTrace(bool startup) if (zs.avail_out == 0) { // Need to write the output. if(startup) - result = write(backup_fd, out, bufSize); + result = write(backup_fd, out.get(), bufSize); else - result = write(STDOUT_FILENO, out, bufSize); + result = write(STDOUT_FILENO, out.get(), bufSize); if ((size_t)result < bufSize) { fprintf(stderr, "error writing deflated trace: %s (%d)\n", @@ -772,7 +768,7 @@ static void dumpTrace(bool startup) zs.avail_out = bufSize; // skip the final write break; } - zs.next_out = out; + zs.next_out = reinterpret_cast<Bytef*>(out.get()); zs.avail_out = bufSize; } @@ -785,9 +781,9 @@ static void dumpTrace(bool startup) if (zs.avail_out < bufSize) { size_t bytes = bufSize - zs.avail_out; if(startup) - result = write(backup_fd, out, bytes); + result = write(backup_fd, out.get(), bytes); else - result = write(STDOUT_FILENO, out, bytes); + result = write(STDOUT_FILENO, out.get(), bytes); if ((size_t)result < bytes) { fprintf(stderr, "error writing deflated trace: %s (%d)\n", @@ -799,9 +795,6 @@ static void dumpTrace(bool startup) if (result != Z_OK) { fprintf(stderr, "error cleaning up zlib: %d\n", result); } - - free(in); - free(out); } else { ssize_t sent = 0; if (startup) |