[libcxx-commits] [PATCH] D128285: [libc++][AIX] Make basic_string layout compatible with earlier version

Xing Xue via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jun 24 11:09:09 PDT 2022


xingxue updated this revision to Diff 439823.
xingxue marked an inline comment as done.
xingxue added a comment.

Addressed the comment to consider potential padding in the `assert` added for `struct __long`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128285/new/

https://reviews.llvm.org/D128285

Files:
  libcxx/include/__config
  libcxx/include/string


Index: libcxx/include/string
===================================================================
--- libcxx/include/string
+++ libcxx/include/string
@@ -723,8 +723,15 @@
 
     struct __long
     {
-        size_type __is_long_ : 1;
-        size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
+        // Attribute 'packed' is used to keep the layout compatible with the
+        // previous definition that did not use bit fields. This is because
+        // on some platforms bit fields have a default size rather than
+        // the actual size used, e.g., it is 4 bytes on AIX. See D128285
+        // for details.
+        struct _LIBCPP_PACKED {
+            size_type __is_long_ : 1;
+            size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
+        };
         size_type __size_;
         pointer   __data_;
     };
@@ -734,14 +741,24 @@
 
     struct __short
     {
-        unsigned char __is_long_ : 1;
-        unsigned char __size_ : 7;
+        // Attribute 'packed' is used to keep the layout compatible with the
+        // previous definition that did not use bit fields. This is because
+        // on some platforms bit fields have a default size rather than
+        // the actual size used, e.g., it is 4 bytes on AIX. See D128285
+        // for details.
+        struct _LIBCPP_PACKED {
+            unsigned char __is_long_ : 1;
+            unsigned char __size_ : 7;
+        };
         char __padding_[sizeof(value_type) - 1];
         value_type __data_[__min_cap];
     };
 
 #endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
-
+    static_assert(sizeof(__long) == (sizeof(pointer) + 2 * sizeof(size_type) +
+                                     (sizeof(pointer) * ((2 * sizeof(size_type) - 1) / sizeof(pointer) + 1)) -
+                                     2 * sizeof(size_type)),
+                  "__long has an unexpected size.");
     static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
 
     union __ulx{__long __lx; __short __lxx;};
Index: libcxx/include/__config
===================================================================
--- libcxx/include/__config
+++ libcxx/include/__config
@@ -1176,6 +1176,12 @@
 #    define _LIBCPP_PACKED_BYTE_FOR_AIX_END /* empty */
 #  endif
 
+#  if __has_attribute(__packed__)
+#    define _LIBCPP_PACKED __attribute__((__packed__))
+#  else
+#    define _LIBCPP_PACKED
+#  endif
+
 #endif // __cplusplus
 
 #endif // _LIBCPP___CONFIG


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128285.439823.patch
Type: text/x-patch
Size: 2477 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220624/722e231d/attachment.bin>


More information about the libcxx-commits mailing list