[v3] extern/fixed-memory/archive allocator
Gabriel Dos Reis
gdr@cs.tamu.edu
Wed Sep 29 16:22:00 GMT 2004
Benjamin Kosnik <bkoz@redhat.com> writes:
| I'm still looking for a good name for the allocator part. The choices
| I've come up with leave me kind of cold: "extern_allocator" isn't
| general enough, "global_allocator" is too vague, I suppose
| "fixed_size_allocator" would work. Thoughts? Probably should just adopt
| TR1 naming conventions if applicable.
This stuff, when it works could be used just about everywhere -- it is
really useful -- for local and non-local objects.
Having to think about it (and the other mail I got from you), it is
essentially the aligned_storage<> proposal.
template<class T, int N = 1>
struct aligned_storage;
provides a suitably aligned contiguous storage to store N objects of
type T.
The obvious implementation with non-broken g++ would be
template<class T, int N>
struct aligned_storage {
typedef unsigned char byte;
typedef byte store[N * sizeof(T)];
store storage;
byte& operator[](int i) { return storage[i]; }
const byte& operator[](int i) const { return storage[i]; }
operator void*() { return storage; }
operator const void*() const { return storage; }
};
You can use it at local scope
void foo()
{
aligned_storage<complex<double>, 10000> data; // no initialization
// ...
}
or with unconstructed streams
aligned_storage<ostream> cout;
aligned_storage<istream> cin;
It can be used to implement a fixed-sized pool allocator.
-- Gaby
More information about the Libstdc++
mailing list