mirror of
https://github.com/Zenithsiz/ftmemsim-valgrind.git
synced 2026-02-04 10:21:20 +00:00
If you do `malloc(100)` followed by `realloc(200)`, DHAT now adds 100 bytes to the read and write counts for the implicit `memcpy`. This gives more reasonable results. I have long been surprised by low writes-per-byte values of around 0.35 for vectors that are grown by doubling. Counting the implicit `memcpy` increases those numbers to well above 0.5, which is what you'd expect. The commit also adds a section to the DHAT docs about `realloc`, because there is some non-obvious behaviour, some of which confused me just a couple of days ago.
29 lines
722 B
C
29 lines
722 B
C
// Some basic allocations and accesses.
|
|
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int main(void)
|
|
{
|
|
int64_t* m = malloc(1000);
|
|
m[0] = 1; // write 8 bytes
|
|
m[10] = m[1]; // read and write 8 bytes
|
|
|
|
char* c = calloc(1, 2000);
|
|
for (int i = 0; i < 1000; i++) {
|
|
c[i + 1000] = c[i]; // read and write 1000 bytes
|
|
}
|
|
|
|
char* r = realloc(m, 3000); // read and write 1000 bytes (memcpy)
|
|
for (int i = 0; i < 500; i++) {
|
|
r[i + 2000] = 99; // write 500 bytes
|
|
}
|
|
|
|
c = realloc(c, 1000); // read and write 1000 bytes (memcpy)
|
|
|
|
free(c);
|
|
// totals: 3008 read, 3516 write
|
|
return 0;
|
|
}
|