 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Oliver S. Guest
|
Posted: Fri Oct 21, 2005 10:19 am Post subject: Eigener Allocator - was mach ich falsch? |
|
|
Da mir häufig bei std::string-Operationen auf die Nerven gegangen ist,
daß die durch die std::allocator<T>-Operationen ziemlich lahmarschig ist,
habe ich mir eine eigene Speicherverwaltungs-Library gezimmert und dieser
auch einen STL-konformen Allocator mitgegeben. Da ich zwei dieser Dinger
für verschiedene Speicherpools benötige (einmal globaler Heap und einmal
Thread-lokaler Heap, je nachdem wieviel Performnace man will bzw Thread
-lokalen Verschnitt man hinnehmen will), habe ich beide Allocator auf
einer gemeinsmen Basisklasse aufgebaut; die sieht so aus:
template<typename T>
class mallocator_base
{
public:
typedef T const &const_pointer;
typedef T const &const_reference;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef T &reference;
typedef std::size_t size_type;
typedef T value_type;
template<class Other>
struct rebind
{
typedef mallocator_base<Other> other;
};
public:
mallocator_base();
static pointer address( reference t );
static const_pointer address( const_reference t );
template<typename ValType>
static void construct( pointer ptr, ValType &val );
static void destroy( pointer ptr );
static size_type max_size();
template<typename Other>
mallocator_base &operator =( Other const &other );
private:
static pointer allocate( size_type count, void * = NULL );
static void deallocate( pointer ptr, size_type count );
};
template<typename T>
inline
mallocator_base<T>::mallocator_base()
{
}
template<typename T>
inline
typename mallocator_base<T>::pointer mallocator_base<T>::address(
reference t )
{
return &t;
}
template<typename T>
inline
typename mallocator_base<T>::const_pointer mallocator_base<T>::address(
const_reference t )
{
return &t;
}
template<typename T>
template<typename ValType>
inline
void mallocator_base<T>::construct( pointer ptr, ValType &val )
{
new( ptr )T( val );
}
template<typename T>
inline
void mallocator_base<T>::destroy( pointer ptr )
{
ptr->T::~T();
}
template<typename T>
inline
typename mallocator_base<T>::size_type mallocator_base<T>::max_size()
{
return (size_type)(int_t)-1;
}
template<typename T>
template<typename Other>
mallocator_base<T> &mallocator_base<T>::operator =( Other const &other )
{
}
Dann gibt es z.B eine weiter darauf aufbauende Klasse global_mallocator<T>.
Die hat zusätztlich noch die allocate()- und deallocate()-Methoden:
template<typename T>
class global_mallocator : public mallocator_base<T>
{
public:
global_mallocator();
static pointer allocate( size_type count, void *hint = NULL );
static void deallocate( pointer ptr, size_type count );
};
template<typename T>
inline
global_mallocator<T>::global_mallocator() :
mallocator_base<T>()
{
}
template<typename T>
inline
typename global_mallocator<T>::pointer global_mallocator<T>::allocate(
size_type count, void * )
{
return (pointer)Mallocator::Allocate( count );
}
template<typename T>
inline
void global_mallocator<T>::deallocate( pointer ptr, size_type count )
{
Mallocator::Release( ptr );
}
Wenn ich nun aber versuche meinen Allocator zu benutzen:
...
typedef std::basic_string<char, std::char_traits
global_mallocator<char> > faststring;
faststring str;
...
Dann bekomme ich folgenden Compiler-Fehler, der darauf hinweist, daß die
allocate()-Methode meines Allocators nicht benutzt werden kann:
xstring(1517):
error C2039: 'deallocate': Ist kein Element von
'ns_OClasses::mallocator_base<T>'
Meine STL-Implementation ist also somit nicht in der Lage auf
global_mallocator<char>::allocate zuzugrifen. Und nicht nur das,
mein Compiler menit auch noch diese Funktion in der übergeordeten
mallocator_base<char> suchen zu müssen; warum auch immer.
Kann mir einer da weiterhelfen?
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Fri Oct 21, 2005 3:12 pm Post subject: Re: Eigener Allocator - was mach ich falsch? |
|
|
Oliver S. wrote:
| Quote: | Da mir häufig bei std::string-Operationen auf die Nerven gegangen ist,
daß die durch die std::allocator<T>-Operationen ziemlich lahmarschig ist,
habe ich mir eine eigene Speicherverwaltungs-Library gezimmert und dieser
auch einen STL-konformen Allocator mitgegeben. Da ich zwei dieser Dinger
für verschiedene Speicherpools benötige (einmal globaler Heap und einmal
Thread-lokaler Heap, je nachdem wieviel Performnace man will bzw Thread
-lokalen Verschnitt man hinnehmen will), habe ich beide Allocator auf
einer gemeinsmen Basisklasse aufgebaut; die sieht so aus:
template<typename T
class mallocator_base
{
|
| Quote: | template<class Other
struct rebind
{
typedef mallocator_base
};
restlichen Implementierungsdetails entsorgt |
| Quote: | Dann bekomme ich folgenden Compiler-Fehler, der darauf hinweist, daß die
allocate()-Methode meines Allocators nicht benutzt werden kann:
xstring(1517):
error C2039: 'deallocate': Ist kein Element von
'ns_OClasses::mallocator_base<T>'
Meine STL-Implementation ist also somit nicht in der Lage auf
global_mallocator<char>::allocate zuzugrifen.
|
Das steht da so nicht in der Fehlermeldung.
| Quote: | Und nicht nur das,
mein Compiler menit auch noch diese Funktion in der übergeordeten
mallocator_base<char> suchen zu müssen; warum auch immer.
|
Der string wird wohl irgend welche internen Datenstruktur dynamisch
anlegen wollen, die nicht vom Typen char sind. Genau dafür ist der Typ
rebind da und den gibt es nur in der Basisklasse und der gibt halt einen
mallocator_base<T> für global_mallocator<char>::rebind<T>::other zurück.
Bei den meisten Compilern wirst Du auch noch den Typen von T in der
Fehlermeldung finden.
mfg Torsten
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Fri Oct 21, 2005 7:44 pm Post subject: Re: Eigener Allocator - was mach ich falsch? |
|
|
"Oliver S." <Follow.Me (AT) gmx (DOT) net> writes:
| Quote: | template<typename T
class mallocator_base
{
public:
typedef T const &const_pointer;
|
Das sieht schon mal komisch aus. Wie wär's mit
typedef T const *const_pointer;
?
[snip]
| Quote: | template
class global_mallocator : public mallocator_base
{
public:
global_mallocator();
static pointer allocate( size_type count, void *hint = NULL );
static void deallocate( pointer ptr, size_type count );
|
Ich nehme an, Du meinst mit size_type und pointer die Typen, welche Du
in mallocator_base deklariert hast. So, wie Du diese Namen hier
verwendest, werden sie in mallocator_base
Hilft es, an den Anfang des public:-Teils die folgenden Deklarationen
zu stellen:
typedef typename mallocator_base<T>::size_type size_type;
typedef typename mallocator_base<T>::pointer pointer;
?
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Oliver S. Guest
|
Posted: Sat Oct 22, 2005 4:52 am Post subject: Re: Eigener Allocator - was mach ich falsch? |
|
|
| Quote: | Das sieht schon mal komisch aus. Wie wär's mit
typedef T const *const_pointer;
|
| Quote: | template<typename T
class global_mallocator : public mallocator_base
{
public:
global_mallocator();
static pointer allocate( size_type count, void *hint = NULL );
static void deallocate( pointer ptr, size_type count );
Ich nehme an, Du meinst mit size_type und pointer die Typen, welche
Du in mallocator_base deklariert hast. So, wie Du diese Namen hier
verwendest, werden sie in mallocator_base
|
Türlich, aber das ist ja nicht unüblich, das so zu machen.
| Quote: | typedef typename mallocator_base<T>::size_type size_type;
typedef typename mallocator_base<T>::pointer pointer;
|
Versuch ich garnicht, denn das Hilft mit Sicherheit nicht.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Oliver S. Guest
|
Posted: Sat Oct 22, 2005 4:56 am Post subject: Re: Eigener Allocator - was mach ich falsch? |
|
|
| Quote: | Dann bekomme ich folgenden Compiler-Fehler, der darauf hinweist, daß die
allocate()-Methode meines Allocators nicht benutzt werden kann:
xstring(1517):
error C2039: 'deallocate': Ist kein Element von
'ns_OClasses::mallocator_base<T>'
Meine STL-Implementation ist also somit nicht in der Lage auf
global_mallocator<char>::allocate zuzugrifen.
Das steht da so nicht in der Fehlermeldung.
|
Nicht direkt, aber indirekt steht es da.
| Quote: | mein Compiler menit auch noch diese Funktion in der übergeordeten
mallocator_base<char> suchen zu müssen; warum auch immer.
Der string wird wohl irgend welche internen Datenstruktur
dynamisch anlegen wollen, die nicht vom Typen char sind.
|
Ausgeschlossen ist das nicht, aber hälst Du das für wahrscheinlich?
Ich halte das für eher unwahrscheinlich.
| Quote: | Genau dafür ist der Typ rebind da und den gibt es nur in der Basisklasse
und der gibt halt einen mallocator_base<T> für global_mallocator<char
::rebind
den Typen von T in der Fehlermeldung finden.
|
Ich werd ihn mal auch auf höherer Ebene implementieren bzw dahin komplett
verfrachten. Glaube aber nicht, daß das was bringt.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Sat Oct 22, 2005 10:32 am Post subject: Re: Eigener Allocator - was mach ich falsch? |
|
|
Oliver S. wrote:
| Quote: | Das steht da so nicht in der Fehlermeldung.
Nicht direkt, aber indirekt steht es da.
|
Ich habe mir angewöhnt, dem Compiler seine meist sehr direkt Art nicht
krum zu nehmen. ;-)
| Quote: | mein Compiler menit auch noch diese Funktion in der übergeordeten
mallocator_base<char> suchen zu müssen; warum auch immer.
Der string wird wohl irgend welche internen Datenstruktur
dynamisch anlegen wollen, die nicht vom Typen char sind.
Ausgeschlossen ist das nicht, aber hälst Du das für wahrscheinlich?
Ich halte das für eher unwahrscheinlich.
|
Ich halte das für sehr wahrscheinlich, wirf halt einen Blick in die in
der Fehlermeldung angegebene Datei.
| Quote: | Genau dafür ist der Typ rebind da und den gibt es nur in der Basisklasse
und der gibt halt einen mallocator_base<T> für global_mallocator<char
::rebind
den Typen von T in der Fehlermeldung finden.
Ich werd ihn mal auch auf höherer Ebene implementieren bzw dahin komplett
verfrachten. Glaube aber nicht, daß das was bringt.
|
Dann können wir ja ne Wette abschließen. Wie wäre es mit einer Tüte
Gummibärchen?
mfg Torsten
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|