From -3946379657406234215
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fc772,10e355266b6fd117
X-Google-Attributes: gidfc772,public
X-Google-Thread: f78e5,10e355266b6fd117
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2002-05-30 09:22:11 PST
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!washdc3-snh1.gtei.net!denver-snf1.gtei.net!news.gtei.net!namche.sun.com!sunnews1.Eng.Sun.COM!engnews1.eng.sun.com!taumet!clamage
From: Christopher Eltschka <celtschk@web.de>
Newsgroups: comp.std.c++,comp.lang.c++.moderated
Subject: Re: A humble attempt to suggest alignment support for C++
Date: 30 May 2002 16:17:20 GMT
Organization: Vienna University of Technology, Austria
Lines: 129
Approved: stephen.clamage@sun.com (comp.std.c++)
Message-ID: <ad35eb$mpa$1@news.tuwien.ac.at>
References: <3CEA1A08.C6D95CC7@lmf.ericsson.se>
NNTP-Posting-Host: taumet.eng.sun.com
X-NNTP-Posting-Host: netlab.cs.rpi.edu
X-Original-Date: 29 May 2002 20:08:10 +0200
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated
	iQBVAwUAPPZD7UHMCo9UcraBAQGSLAH9FLveA3pHbtxYkva0pzeQYWKHgjCvK6aP
	XG1eRL4E4JyF2Wp9P58F+GXoHfalAo1rrm3v+VTwnX8aomzzJKjehA==
	=/rFm
X-Approved-For-Group: hsutter@acm.org comp.lang.c++.moderated
X-Scanned-By: MIMEDefang 2.3 (www dot roaringpenguin dot com slash mimedefang)
Content-Length: 3990
Originator: clamage@taumet
Xref: archiver1.google.com comp.std.c++:11707 comp.lang.c++.moderated:44251


Attila Feher <Attila.Feher@lmf.ericsson.se> writes:

[...]

> Solution #1:
> ------------
> 
> This solution is more work for the users of the language, the way to 
> solve the problem is not so elegant, but it is probably easy to 
> implement.
> 
> Two additions to the language are required here:
> 
> 1.) max_align(T) compile time operator(?)
> 
> 2.) T *std::align<T>( void *ptr[, std::size_t size]) runtime function
> 
> The max_align<T> works like sizeof.  It gives the maximum
> "alignment loss" in bytes one can get with the type T.  So if type T
> needs to be aligned on 8 bytes boundary, max_align(T) would give 7,
> because we can loose max. 7 bytes, if we align this type.
> 
> The align<T>(ptr[, size]) template like function would give return an 
> address _after_ ptr, which is properly aligned for type T.  The caller

> can then create a type T at that address using replacement new.
> 
> The second overload of align would check if it aligns ptr to a T is 
> there any space left in those size bytes to create a T.  If not, it 
> throws some std::whatever_we_call_it.
> 
> The max_align would help in allocation:
> 
> template <typename T, std::size_t S>
> class fsdc_array {
> 
>    T *arr_;
>    char buff_[sizeof(T)+max_align(T)];
> };
> 
> The align would help in figuring out the address of the first element:
> 
> template blabla
> fsdc_array::fsdc_array() {
>   arr_ = align<T>( buff_);
> }
> 
> The code of the array template could then address its elements via 
> arr_.
> 
> 
> Solution #2:
> ------------
> 
> This solution requires a new, special keyword in the declarations, 
> hides all the know-how, and therefore a bit more complicated for the 
> implementors but much easier for the users.  And it is less flexible 
> than #1, since with #1 I can even create a T *tp with align, then say 
> Y *yp=align<Y>( tp+1); and position a Y after the T in the memory.
> 
> This solution would enable the coder to change alignment of any type 
> or just char arrays in declarations.  As it seems it is enough for 
> just char arrays, but you may see a good reason for open it up to 
> arrays or any other type.
> 
> It could look something like this:
> 
> align_as<T> char T_arr[sizeof(T)*S];
> 
> So align_as would be a sort of "storage specifier like thing", 
> possibly behaving the same as static from syntax point of view.  
> Except that it would not "collide" with any storage specifiers, since 
> it is a different
> thing: alignment specifier.
> 
> 
> Solution #3:
> ------------
> 
> Provide both #1 and #2, allowing the nice "tricks" described about #1 
> under #2 (opening up a great flexibility) as well as give an easy way 
> to do the easy thing.

Solution #4:

Provide a built-in "class template" align<T> with the following
properties:

 a) align<T> has the samme alignment restrictions as T
 b) align<T> has the smallest size compatrible with those alignment
    restrictions (i.e. sizeof(align<T>) gives the alignment of T)
 c) align<T> is POD

As a special case, make align<void> be suitable allignd for

Now you can get an aligned memory area f.ex. with

union
{
  align<int> int_alignment;              // forces alignment
  unsigned char intarr[100*sizeof(int)]; // aligned for int
};

union
{
  align<void> void_alignment;
  unsigned char arr[10000]; // aligned for arbitrary type
}

char* p = arr + sizeof(align<double>);
  // p is the second position in arr correctly aligned for double

char* q = arr + sizeof(align<void>);
  // q is the second position in arr correctly aligned for any type

[...]



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]




