diff options
author | Trond Kjernåsen <trond@trolltech.com> | 2009-12-10 16:38:30 (GMT) |
---|---|---|
committer | Trond Kjernåsen <trond@trolltech.com> | 2009-12-10 16:49:29 (GMT) |
commit | bf9456c5a2d8dfe9a35a2175186630cb426858ad (patch) | |
tree | 2430371741ecea3f6c0b27d61887ada433559af0 /src/opengl/qgl_p.h | |
parent | 6db96dcd4acccbc13161f85adf3164907b7b5cae (diff) | |
download | Qt-bf9456c5a2d8dfe9a35a2175186630cb426858ad.zip Qt-bf9456c5a2d8dfe9a35a2175186630cb426858ad.tar.gz Qt-bf9456c5a2d8dfe9a35a2175186630cb426858ad.tar.bz2 |
Optimize our GL extension checks to avoid mallocs.
We want to avoid any unnecessary mallocs when checking GL/GLX
extensions. The GL extension string can be quite long and contain
several hundred extensions. The old code forced one malloc for each
extension + 1 extra malloc for the extension string itself when
it was copied into the QByteArray.
Reviewed-by: Kim
Diffstat (limited to 'src/opengl/qgl_p.h')
-rw-r--r-- | src/opengl/qgl_p.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/opengl/qgl_p.h b/src/opengl/qgl_p.h index 8a0b31f..11770d3 100644 --- a/src/opengl/qgl_p.h +++ b/src/opengl/qgl_p.h @@ -610,6 +610,49 @@ private: friend class QGLContextGroup; }; + +// This class can be used to match GL extensions with doing any mallocs. The +// class assumes that the GL extension string ends with a space character, +// which it should do on all conformant platforms. Create the object and pass +// in a pointer to the extension string, then call match() on each extension +// that should be matched. The match() function takes the extension name +// *without* the terminating space character as input. + +class QGLExtensionMatcher +{ +public: + QGLExtensionMatcher(const char *str) + : gl_extensions(str), gl_extensions_length(qstrlen(str)) + {} + + bool match(const char *str) { + int str_length = qstrlen(str); + const char *extensions = gl_extensions; + int extensions_length = gl_extensions_length; + + while (1) { + // the total length that needs to be matched is the str_length + + // the space character that terminates the extension name + if (extensions_length < str_length + 1) + return false; + if (qstrncmp(extensions, str, str_length) == 0 && extensions[str_length] == ' ') + return true; + + int split_pos = 0; + while (split_pos < extensions_length && extensions[split_pos] != ' ') + ++split_pos; + ++split_pos; // added for the terminating space character + extensions += split_pos; + extensions_length -= split_pos; + } + return false; + } + +private: + const char *gl_extensions; + int gl_extensions_length; +}; + QT_END_NAMESPACE #endif // QGL_P_H |