Search code examples
androidc++buildandroid-source

What does the PACKED keyword mean in Android aosp?


When I read some C++ code in the Android aosp, I see some classes are declared with a PACKED keyword. For example, in <android_source_root>/art/runtime/image.h, a class ImageSection is declared this way:

class PACKED(4) ImageSection {
 public:
  ImageSection() : offset_(0), size_(0) { }
  ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
  ImageSection(const ImageSection& section) = default;
  ImageSection& operator=(const ImageSection& section) = default;

What does the PACKED(4) mean?


Solution

  • It is defined in <android_source_root>/art/libartbase/base/macros.h

    #define PACKED(x) __attribute__ ((__aligned__(x), __packed__))