Skip to content

Commit dd681db

Browse files
chih-hungGerrit Code Review
authored and
Gerrit Code Review
committed
Merge "Fix/suppress potential memory leaks warnings."
2 parents ab8ec9c + ae4366e commit dd681db

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

micro_bench/micro_bench.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,19 @@ uint64_t nanoTime() {
9191
return static_cast<uint64_t>(t.tv_sec) * NS_PER_SEC + t.tv_nsec;
9292
}
9393

94+
// Static analyzer warns about potential memory leak of orig_ptr
95+
// in getAlignedMemory. That is true and the callers in this program
96+
// do not free orig_ptr. But, we don't care about that in this
97+
// going-obsolete test program. So, here is a hack to trick the
98+
// static analyzer.
99+
static void *saved_orig_ptr;
100+
94101
// Allocate memory with a specific alignment and return that pointer.
95102
// This function assumes an alignment value that is a power of 2.
96103
// If the alignment is 0, then use the pointer returned by malloc.
97104
uint8_t *getAlignedMemory(uint8_t *orig_ptr, int alignment, int or_mask) {
98105
uint64_t ptr = reinterpret_cast<uint64_t>(orig_ptr);
106+
saved_orig_ptr = orig_ptr;
99107
if (alignment > 0) {
100108
// When setting the alignment, set it to exactly the alignment chosen.
101109
// The pointer returned will be guaranteed not to be aligned to anything
@@ -447,6 +455,7 @@ int benchmarkMemread(const char *name, const command_data_t &cmd_data, void_func
447455
size_t k;
448456
MAINLOOP_DATA(name, cmd_data, size,
449457
for (k = 0; k < size/sizeof(uint32_t); k++) foo = src[k]);
458+
free(src);
450459

451460
return 0;
452461
}

0 commit comments

Comments
 (0)