aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDichenZhang1 <140119224+DichenZhang1@users.noreply.github.com>2023-11-15 20:49:22 -0800
committerGitHub <noreply@github.com>2023-11-15 20:49:22 -0800
commit3a3a752a5da0b2304b1b6de0ef383bbe41256a67 (patch)
tree1930161ecd2ecd5a57e5232f87c2b99c00ad8091
parentd81736f4f71ce1fd014e860145b8a84cb270d0f9 (diff)
parent153afb22b77eddb23b025acbde1e7e5b75185ae4 (diff)
downloadlibultrahdr-3a3a752a5da0b2304b1b6de0ef383bbe41256a67.tar.gz
Merge pull request #44 from ittiam-systems/benchmark
fix memory leak in benchmark application
-rw-r--r--CMakeLists.txt9
-rw-r--r--benchmark/Android.bp2
-rw-r--r--benchmark/benchmark_test.cpp30
3 files changed, 25 insertions, 16 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e5619e4..84091f4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -83,7 +83,7 @@ option_if_not_defined(UHDR_BUILD_BENCHMARK "Build benchmark " FALSE)
option_if_not_defined(UHDR_BUILD_FUZZERS "Build fuzzers " FALSE)
option_if_not_defined(UHDR_ENABLE_LOGS "Build with verbose logging " FALSE)
-if(UHDR_BUILD_BENCHMARK AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "Linux"))
+if(UHDR_BUILD_BENCHMARK AND WIN32)
message(FATAL_ERROR "Building benchmarks on current platform not supported")
endif()
@@ -213,7 +213,7 @@ ExternalProject_Add(libjpeg-turbo
INSTALL_COMMAND ""
)
-if(UHDR_BUILD_TESTS OR UHDR_BUILD_BENCHMARK)
+if(UHDR_BUILD_TESTS)
# gtest and gmock
set(GTEST_INCLUDE_DIRS
${THIRD_PARTY_DIR}/googletest/googletest/include
@@ -359,16 +359,15 @@ endif()
if(UHDR_BUILD_BENCHMARK)
add_executable(ultrahdr_bm ${UHDR_BM_LIST})
- add_dependencies(ultrahdr_bm benchmark googletest ultrahdr)
+ add_dependencies(ultrahdr_bm benchmark ultrahdr)
target_include_directories(ultrahdr_bm PRIVATE
${COMMON_INCLUDE_LIST}
${BENCHMARK_INCLUDE_DIR}
- ${GTEST_INCLUDE_DIRS}
)
if(UHDR_BUILD_FUZZERS)
target_link_options(ultrahdr_bm PRIVATE -fsanitize=fuzzer-no-link)
endif()
- target_link_libraries(ultrahdr_bm ultrahdr ${BENCHMARK_LIBRARIES} ${GTEST_BOTH_LIBRARIES})
+ target_link_libraries(ultrahdr_bm ultrahdr ${BENCHMARK_LIBRARIES})
endif()
if(UHDR_BUILD_FUZZERS)
diff --git a/benchmark/Android.bp b/benchmark/Android.bp
index 89a001a..1dc6ccf 100644
--- a/benchmark/Android.bp
+++ b/benchmark/Android.bp
@@ -26,8 +26,6 @@ cc_benchmark {
host_supported: true,
srcs: ["benchmark_test.cpp"],
static_libs: [
- "libgmock",
- "libgtest",
"libjpegdecoder",
"libjpegencoder",
"libultrahdr",
diff --git a/benchmark/benchmark_test.cpp b/benchmark/benchmark_test.cpp
index 32ade20..e9f9533 100644
--- a/benchmark/benchmark_test.cpp
+++ b/benchmark/benchmark_test.cpp
@@ -18,7 +18,6 @@
#include <iostream>
#include <benchmark/benchmark.h>
-#include <gtest/gtest.h>
#include "jpegr.h"
@@ -57,7 +56,7 @@ static bool loadFile(const char* filename, void*& result, int length) {
return false;
}
ifd.seekg(0, std::ios::beg);
- result = malloc(length);
+ result = new uint8_t[length];
if (result == nullptr) {
std::cerr << "failed to allocate memory to store contents of file : " << filename
<< std::endl;
@@ -75,7 +74,10 @@ static void BM_Decode(benchmark::State& s) {
ultrahdr_output_format of = static_cast<ultrahdr_output_format>(s.range(1));
std::ifstream ifd(srcFileName.c_str(), std::ios::binary | std::ios::ate);
- ASSERT_TRUE(ifd.good()) << "unable to open file " << srcFileName;
+ if (!ifd.good()) {
+ s.SkipWithError("unable to open file " + srcFileName);
+ return;
+ }
int size = ifd.tellg();
jpegr_compressed_struct jpegImgR{};
@@ -84,8 +86,10 @@ static void BM_Decode(benchmark::State& s) {
jpegImgR.data = nullptr;
jpegImgR.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
ifd.close();
- ASSERT_TRUE(loadFile(srcFileName.c_str(), jpegImgR.data, size))
- << "unable to load file " << srcFileName;
+ if (!loadFile(srcFileName.c_str(), jpegImgR.data, size)) {
+ s.SkipWithError("unable to load file " + srcFileName);
+ return;
+ }
std::unique_ptr<uint8_t[]> compData;
compData.reset(reinterpret_cast<uint8_t*>(jpegImgR.data));
@@ -94,17 +98,25 @@ static void BM_Decode(benchmark::State& s) {
std::vector<uint8_t> iccData(0);
std::vector<uint8_t> exifData(0);
jpegr_info_struct info{0, 0, &iccData, &exifData};
- ASSERT_EQ(JPEGR_NO_ERROR, jpegHdr.getJPEGRInfo(&jpegImgR, &info));
+ if (JPEGR_NO_ERROR != jpegHdr.getJPEGRInfo(&jpegImgR, &info)) {
+ s.SkipWithError("getJPEGRInfo returned with error ");
+ return;
+ }
size_t outSize = info.width * info.height * ((of == ULTRAHDR_OUTPUT_HDR_LINEAR) ? 8 : 4);
std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(outSize);
jpegr_uncompressed_struct destImage{};
destImage.data = data.get();
for (auto _ : s) {
- ASSERT_EQ(JPEGR_NO_ERROR, jpegHdr.decodeJPEGR(&jpegImgR, &destImage, FLT_MAX, nullptr, of));
+ if (JPEGR_NO_ERROR != jpegHdr.decodeJPEGR(&jpegImgR, &destImage, FLT_MAX, nullptr, of)) {
+ s.SkipWithError("decodeJPEGR returned with error ");
+ return;
+ }
+ }
+ if (info.width != destImage.width || info.height != destImage.height) {
+ s.SkipWithError("received unexpected width/height");
+ return;
}
- ASSERT_EQ(info.width, destImage.width);
- ASSERT_EQ(info.height, destImage.height);
s.SetLabel(srcFileName + ", " + ofToString(of) + ", " + std::to_string(info.width) + "x" +
std::to_string(info.height));