如何找到所有可以通过更改成员顺序来缩小的结构体
背景:编译器可能会在结构中插入填充,以使其成员更好地对齐。这将导致结构体的大小大于其成员大小的总和。对结构体的成员重新排序,以便它们更好地打包,可以消除编译器以这种方式填充的需要,并使结构体更小,从而节省内存。我需要节省这些内存。
后备选项是手动检查每个结构。我正在寻找一种可以减少工作量的自动化方法。
即使它只是减少了需要手动检查的结构数量,这也会有所帮助。
因此,例如,列出所有大于其成员大小总和的结构的流程/工具/等,虽然不完美,但仍然会有所帮助,因为它会限制需要手动检查的结构。
有谁知道有什么工具可以做到这一点,或者有人可以建议任何可能有帮助的方法。
ps 我需要在包含超过 100 万行代码的嵌入式 C 代码库上执行此操作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
pahole 是为此特定而编写的实用程序目的。它将分析您编译的目标文件(在启用调试的情况下编译),并向您显示结构漏洞。
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.
gcc 的 -Wpadded 警告选项可用于告诉您何时填充结构。这不会告诉您何时可以缩小结构,但它可以帮助减少工作量。
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.
您可以编写一个程序,依次为结构体中字段的每个排列写出一个小型 C 程序,并且在编译并运行输出程序时打印出结构体大小。如果字段数量远大于 10 个左右,这将变得不切实际。
You can write a program that in turn writes out a small C program for every permutation of the fields in the struct, and when the output program is compiled and run its prints out the struct size. This will become impractical if the number of fields becomes much larger than 10 or so.
CIL 是一个用 OCaml 编写的强大的 C 解析器,可以理解结构的填充。它带有一个检测C程序。结构填充是特定于平台的,我不怀疑你知道它,但你可以在你的问题中说得更清楚。 CIL封装的检测程序检测类型的大小,CIL假设用于结构体填充的算法是通过向上舍入计算第n个字段的偏移量(第(n-1)个字段的偏移量+ 第 (n-1) 个字段的大小)到最接近的(第 n 个字段的对齐方式)的倍数。
从 CIL 开始,只需不到 200 行 OCaml 即可制作出您需要的工具。但可能还有更好的解决方案。
CIL is a robust C parser written in OCaml that understand the padding of structs. It comes with a detection C program. Struct padding is platform-specific, I do not doubt you know it, but you could have made it clearer in your question. The detection program packaged with CIL detects the size of types, and the algorithm that CIL assumes is used for the padding of structs is that the n-th field's offset is computed by rounding up (offset of the (n-1)-th field + size of the (n-1)-th field) to the nearest multiple of (alignment of the n-th field).
It would be less than 200 lines of OCaml to make the tool you need, starting from CIL. But there may be better solutions yet.