Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Last active November 8, 2017 02:50
Show Gist options
  • Save kaityo256/32bc425b630642f67e649854104f977e to your computer and use it in GitHub Desktop.
Save kaityo256/32bc425b630642f67e649854104f977e to your computer and use it in GitHub Desktop.
Visualize chunk list of malloc
#include <cstdio>
#include <cstdlib>
#include <omp.h>
const int N = 128;
int
main(int argc, char **argv) {
char *buf[N];
size_t size = 128;
#pragma omp parallel for
for (int i = 0; i < N; i++) {
buf[i] = (char*)malloc(size);
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < size; j++) {
buf[i][j] = 0;
}
}
for (int i = 0; i < N; i++) {
free(buf[i]);
}
printf("digraph test_%d\n {\n", size);
for (int i = 0; i < N; i++) {
size_t *p = (size_t*)(buf[i]);
size_t prev_size = *(p - 2);
if (prev_size != 0)continue;
size_t *ct = p - 2;
size_t fd = *p;
printf("\"%lx\"->\"%lx\"\n", ct, fd);
}
printf("}\n");
}
@kaityo256
Copy link
Author

$ g++ -fopenmp test.cpp
$ OMP_NUM_THREADS=24 ./a.out
digraph test_128
 {
"608160"->"2aaaabc23ed8"
"606c90"->"608160"
"606c00"->"2aaaabc23ed8"
"609240"->"608a60"
"609e10"->"609240"
"2aaab0000b70"->"2aaab0000078"
"608a60"->"608160"
"6097e0"->"608a60"
"60a710"->"609240"
"608f70"->"608a60"
"607620"->"608a60"
"608790"->"2aaaabc23ed8"
"2aaab00008a0"->"2aaab0000078"
"607e90"->"606810"
"606810"->"2aaaabc23ed8"
"6069c0"->"2aaaabc23ed8"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment