Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all the structs that could be made smaller by changing the order of their members

Background: The compiler may insert padding into a struct to make it's members align better. This will result in the sizeof the struct being larger than the sum of the sizes of it's members. Reordering the members of the structure so they pack better can remove the need for the compiler to pad in this manner and make the struct smaller saving memory. I need to get those memory savings.

The fallback option is to check every struct by hand. I'm looking for an automated approach that can cut down the effort.

Even if it only reduces the number of structs to be checked by hand that would help.

So for example a process/tool/etc that lists all the structs that are bigger than the sum of the sizes of their members, while not perfect would still be helpful as it would limit the ones that need to be manually checked.

Does anyone know of any tools that can do this or can anyone suggest any approaches that might help.

p.s. I need to do this on an embedded C codebase containing over 1 million lines of code.

like image 250
Gordon Wrigley Avatar asked Jan 18 '10 03:01

Gordon Wrigley


2 Answers

pahole is a utility written for this specific purpose. It'll analyse your compiled object files (compiled with debugging enabled), and show you the structure holes.

like image 97
caf Avatar answered Oct 18 '22 07:10

caf


gcc's -Wpadded warning option can be used to tell you when a structure is being padded. This won't tell you when the structure can be made smaller, but it can help reduce the work.

like image 36
ergosys Avatar answered Oct 18 '22 05:10

ergosys