Reputation: 19948
In this article, http://cacm.acm.org/magazines/2010/7/95061-youre-doing-it-wrong/fulltext
The author talks about the memory layouts of 2 data structures - The Binary Heap and the B-Heap and compares how one has better memory layout than the other (figures 5 and 6).
I want to get hands on experience on this. I have an implementation of a N-Ary Tree and I want to find out the memory layout of my data structure. What is the best way to come up with a memory layout like the one in the article?
Secondly, I think it is easier to identify the memory layout if it is an array based implementation. If the implementation of a Tree uses pointers then what Tools do we have or what kind of approach is required to map it's memory layout?
Upvotes: 10
Views: 781
Reputation: 3917
The first thing you need to do is figure out the data you need to represent in graphical format. The memory layout in Poul-Henning Kamp's figures are both the pointer structure, and contiguous virtual memory pages. The former can easily be displayed using a debugging tool like ddd. The latter takes a bit more effort, and there are more ways to accomplish it.
A few ideas...
Another possibility nobody mentioned yet, would be reading through the specification for the language you're writing the code in. This should generally let you determine the memory layout of the structures in the actual compiled code (C/C++, etc...), neglecting compiler optimization. This can be altered by telling the compiler to lay out the data structures in non-default ways though (alignas
, __attribute__(aligned)
, etc...). You would still need to consider how the memory is allocated from the heap and the operating system.
However, once you have the relevant values, you should be able to use any software you like to convert the data into a graphical format (graphviz, etc...).
Upvotes: 1
Reputation: 222
Perhaps just traversing the data structure to print element addresses (and sizes if they vary) would give you enough information to feed to for instance graphviz
? I'm not sure why did you include the linux-kernel
tag. Basic virtual memory mapping happens at page granularity (ignoring huge pages here) so physical vs virtual address don't matter. You can easily do your tests in user space.
I would proceed as follows:
graphviz
description filegraphviz
to enjoy the vizualisationUpvotes: 3
Reputation: 1
Design a code for a data-structure
to test
Pre-fill the data-structure
under test with significant-values ( 0x00000000
, 0x01111111
, ... ) highlighting the layout borders & data belonging to data-structure
elements
Use debugging tools to view actual live-memory content & layout that the coded data-structure
element-under-test uses in-vivo
( be systematic & patient )
Upvotes: 3