[v3] extern/fixed-memory/archive allocator

Gabriel Dos Reis gdr@cs.tamu.edu
Wed Sep 29 15:53:00 GMT 2004


Benjamin Kosnik <bkoz@redhat.com> writes:

| >I really like the general idea.  And I'd like to see our basic_string
| >use that technique too. 
| 
| Thanks. This allows the use of basic_string/STL containers instead of
| fixed char/value_type arrays in initialization routines where
| dynamically allocated memory is problematic. Which, let's face it, would
| be nice!

Yep.

The other reason I would really like to use this is PR 8670.  See
comment #6 in the audit trail.  That was some time ago,  I haven't
checked whether the align attribute got fixed with the new parser.  My
guess is no.

| So, it can already be used with basic_string... however, I see what you
| are saying. It is an interesting solution for known small containers.
| 
| > However I think we would need first to fix the
| >front-end so that this works properly
| >
| >+       typedef char layout_type[sizeof(_Tp)] 
| >+       __attribute__ ((aligned(__alignof__(_Tp))));
| 
| This works now .... see src/globals_*.cc.

src/globals_*.cc are the results of splitting old src/globals.cc,
which was the compromise Mark and I came to agree on a long time ago :-)

But I do not believe it works properly for typedefs as written above.
On hardwares that properly enforce alignment, you should see a SIGBUS
error. Here is a simple testcase that shows that it does not work (no,
it does not crash :-)

     merlin[10:41]% cat t.C && g++ t.C && ./a.out
     #include <iostream>

     template<typename T>
     struct X {
        typedef char layout_type[sizeof(T)]
        __attribute__((aligned(__alignof__(T))));
        layout_type data;
     };

     struct A {
        typedef char layout_type[sizeof(double)]
        __attribute__((aligned(__alignof__(double))));
        layout_type data;
     };

     struct B {
        typedef char layout_type[sizeof(double)];
        layout_type data   __attribute__((aligned(__alignof__(double))));
     };

     int main()
     {
        std::cout << __alignof__(X<double>) << std::endl
                  << __alignof__(A) << std::endl
                  << __alignof__(B) << std::endl;
     }
     1
     1
     8

This is on an i686-px-linux-gnu.  If it worked, it should have printed
8 all the time.


As you can see, the template should work if written as

     template<typename T>
     struct X {
        typedef char layout_type[sizeof(T)];
        layout_type data __attribute__((aligned(__alignof__(T))));
     };

(following the pattern of class B definition).  However the front-end
would complain with

   error: requested alignment is not a constant

which is pure nonsense.  This is the bug I was referring to.
It -seems- to work in the version you initially wrote because the
compiler just ignored the alignment attribute you requested...

-- Gaby



More information about the Libstdc++ mailing list