[Qt-interest] On overriding qMalloc in QVector
Benoit Jacob
jacob.benoit.1 at gmail.com
Thu Oct 22 15:29:47 CEST 2009
2009/10/22 Thiago Macieira <thiago at kde.org>:
> Em Quinta-feira 22. Outubro 2009, às 01.12.09, Benoit Jacob escreveu:
>> I'm pleading that I shouldn't have had to examine Qt source code so
>> closely to do something that is a standard feature e.g. in the STL!
>
> Qt is not STL nor does purport to replace it completely. There are many
> features in STL that are not in Qt and also vice-versa: many features in Qt
> tool classes not present in STL.
Fair enough :)
But it seemed to be the code's intention to still allow redefining
qMalloc, qRealloc etc: indeed there is this (a bit cryptic) comment in
qglobal.h:
/*
These functions make it possible to use standard C++ functions with
a similar name from Qt header files (especially template classes).
*/
Q_CORE_EXPORT void *qMalloc(size_t size);
Q_CORE_EXPORT void qFree(void *ptr);
Q_CORE_EXPORT void *qRealloc(void *ptr, size_t size);
Q_CORE_EXPORT void *qMemCopy(void *dest, const void *src, size_t n);
Q_CORE_EXPORT void *qMemSet(void *dest, int c, size_t n);
Is it indeed the intention?
If no, you can stop reading here.
If yes, here goes:
So I just wanted to bring to your attention that if your intent is
that all these memory operations go through the above functions and
these be overridable, then that's not the case in practice.
So assuming that that is really the intention, here are 2 remarks:
1) in qvector.h line 466 you have a call to ::memcpy(). Shouldn't that
be qMemCopy?
2) As I mentioned, only calls made from templates can be overrid,
calls made from the binary lib can't be overrid, so in QVector the
allocation shouldn't be performed from QVectorData (which by the way
is independent from the type T, hence shouldn't allocate memory for
it).
> Qt tool classes do not support custom allocators. That feature cannot be added
> until Qt 5, so any discussion is tabled for now.
Still you have a very simple way of fixing this without breaking
compatibility, all what's needed is to make sure that the user can
override qMalloc : introduce
QVector<T>::growData()
containing the exact same code as QVectorData::grow(), and call that
instead of QVectorData::grow(). Same for QVectorData::malloc() if
it's actually used (i don't know).
> In any case, QVector<T>::Data contains one element of type T. I suggest using
> compiler extensions to change the alignment of said type, like GCC's
> __attribute__((aligned(N))).
Already done. But that doesn't affect what qMalloc does. In our case,
whenever qMalloc returns a pointer that's not 16-byte aligned, we
crash upon the subsequent access.
Note that even if you used c++'s default operator new, that still
wouldn't work. We still have to overload new manually for the custom
type, there's no way to have the __attribute__ do that for us.
Such good alignment support is coming in c++1x, see
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2341.pdf
Thanks,
Benoit
More information about the Qt-interest-old
mailing list