Deprecate qMemCopy/qMemSet in favour of their stdlib equivilents.

Just like qMalloc/qRealloc/qFree, there is absolutely no reason to wrap these
functions just to avoid an include, except to pay for it with worse runtime
performance.

On OS X, on byte sizes from 50 up to 1000, calling memset directly is 28-15%
faster(!) than adding an additional call to qMemSet. The advantage on sizes
above that is unmeasurable.

For qMemCopy, the benefits are a little more modest: 16-7%.

Change-Id: I98aa92bb765aea0448e3f20af42a039b369af0b3
Reviewed-by: Giuseppe D'Angelo <dangelog@gmail.com>
Reviewed-by: John Brooks <john.brooks@dereferenced.net>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Robin Burchell 2012-04-06 16:34:19 +02:00 committed by Qt by Nokia
parent 5dc506ad84
commit 7be255156f
31 changed files with 48 additions and 61 deletions

View File

@ -79,7 +79,7 @@ template <typename T> inline void qbswap(const T src, uchar *dest)
// If you want to avoid the memcopy, you must write specializations for this function
template <typename T> inline void qToUnaligned(const T src, uchar *dest)
{
qMemCopy(dest, &src, sizeof(T));
memcpy(dest, &src, sizeof(T));
}
/* T qFromLittleEndian(const uchar *src)

View File

@ -1964,13 +1964,6 @@ Q_CORE_EXPORT unsigned int qt_int_sqrt(unsigned int n)
return p;
}
#if defined(qMemCopy)
# undef qMemCopy
#endif
#if defined(qMemSet)
# undef qMemSet
#endif
void *qMemCopy(void *dest, const void *src, size_t n) { return memcpy(dest, src, n); }
void *qMemSet(void *dest, int c, size_t n) { return memset(dest, c, n); }

View File

@ -1189,12 +1189,12 @@ inline void qSwap(T &value1, T &value2)
Q_CORE_EXPORT QT_DEPRECATED void *qMalloc(size_t size) Q_ALLOC_SIZE(1);
Q_CORE_EXPORT QT_DEPRECATED void qFree(void *ptr);
Q_CORE_EXPORT QT_DEPRECATED void *qRealloc(void *ptr, size_t size) Q_ALLOC_SIZE(2);
Q_CORE_EXPORT QT_DEPRECATED void *qMemCopy(void *dest, const void *src, size_t n);
Q_CORE_EXPORT QT_DEPRECATED void *qMemSet(void *dest, int c, size_t n);
#endif
Q_CORE_EXPORT void *qMallocAligned(size_t size, size_t alignment) Q_ALLOC_SIZE(1);
Q_CORE_EXPORT void *qReallocAligned(void *ptr, size_t size, size_t oldsize, size_t alignment) Q_ALLOC_SIZE(2);
Q_CORE_EXPORT void qFreeAligned(void *ptr);
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);
/*
@ -1388,14 +1388,6 @@ inline const QForeachContainer<T> *qForeachContainer(const QForeachContainerBase
# endif
#endif
#if 0
/* tell gcc to use its built-in methods for some common functions */
#if defined(QT_NO_DEBUG) && defined(Q_CC_GNU)
# define qMemCopy __builtin_memcpy
# define qMemSet __builtin_memset
#endif
#endif
template <typename T> static inline T *qGetPtrHelper(T *ptr) { return ptr; }
template <typename Wrapper> static inline typename Wrapper::pointer qGetPtrHelper(const Wrapper &p) { return p.data(); }

View File

@ -100,7 +100,7 @@ static inline double qt_inf()
: qt_le_inf_bytes);
union { unsigned char c[8]; double d; } returnValue;
qMemCopy(returnValue.c, bytes, sizeof(returnValue.c));
memcpy(returnValue.c, bytes, sizeof(returnValue.c));
return returnValue.d;
}
@ -115,7 +115,7 @@ static inline double qt_snan()
: qt_le_snan_bytes);
union { unsigned char c[8]; double d; } returnValue;
qMemCopy(returnValue.c, bytes, sizeof(returnValue.c));
memcpy(returnValue.c, bytes, sizeof(returnValue.c));
return returnValue.d;
}
@ -130,7 +130,7 @@ static inline double qt_qnan()
: qt_le_qnan_bytes);
union { unsigned char c[8]; double d; } returnValue;
qMemCopy(returnValue.c, bytes, sizeof(returnValue.c));
memcpy(returnValue.c, bytes, sizeof(returnValue.c));
return returnValue.d;
}

View File

@ -1057,7 +1057,7 @@ QMetaProperty QMetaObject::property(int index) const
if (colon > enum_name) {
int len = colon-enum_name-1;
scope_buffer = (char *)malloc(len+1);
qMemCopy(scope_buffer, enum_name, len);
memcpy(scope_buffer, enum_name, len);
scope_buffer[len] = '\0';
scope_name = scope_buffer;
enum_name = colon+1;

View File

@ -121,7 +121,7 @@ QByteArrayMatcher::QByteArrayMatcher()
{
p.p = 0;
p.l = 0;
qMemSet(p.q_skiptable, 0, sizeof(p.q_skiptable));
memset(p.q_skiptable, 0, sizeof(p.q_skiptable));
}
/*!

View File

@ -814,7 +814,7 @@ inline QString QString::section(QChar asep, int astart, int aend, SectionFlags a
inline int QString::toWCharArray(wchar_t *array) const
{
if (sizeof(wchar_t) == sizeof(QChar)) {
qMemCopy(array, d->data(), sizeof(QChar) * size());
memcpy(array, d->data(), sizeof(QChar) * size());
return size();
}
return toUcs4_helper(d->data(), size(), reinterpret_cast<uint *>(array));

View File

@ -151,7 +151,7 @@ static inline int bm_find(const ushort *uc, uint l, int index, const ushort *puc
QStringMatcher::QStringMatcher()
: d_ptr(0), q_cs(Qt::CaseSensitive)
{
qMemSet(q_data, 0, sizeof(q_data));
memset(q_data, 0, sizeof(q_data));
}
/*!

View File

@ -230,7 +230,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, in
while (s < asize)
new (ptr+(s++)) T(*abuf++);
} else {
qMemCopy(&ptr[s], abuf, increment * sizeof(T));
memcpy(&ptr[s], abuf, increment * sizeof(T));
s = asize;
}
}
@ -268,7 +268,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int a
QT_RETHROW;
}
} else {
qMemCopy(ptr, oldPtr, copySize * sizeof(T));
memcpy(ptr, oldPtr, copySize * sizeof(T));
}
} else {
ptr = oldPtr;

View File

@ -418,7 +418,7 @@ QVector<T>::QVector(int asize)
while (i != b)
new (--i) T;
} else {
qMemSet(d->begin(), 0, asize * sizeof(T));
memset(d->begin(), 0, asize * sizeof(T));
}
}
@ -546,7 +546,7 @@ void QVector<T>::realloc(int asize, int aalloc)
} else if (asize > x->size) {
// initialize newly allocated memory to 0
qMemSet(x->end(), 0, (asize - x->size) * sizeof(T));
memset(x->end(), 0, (asize - x->size) * sizeof(T));
}
x->size = asize;

View File

@ -51,6 +51,8 @@
#include <QtGui/qcolor.h>
#include <QtGui/qevent.h>
#include <stdlib.h>
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
@ -216,7 +218,7 @@ public:
// quint64 alertHigh : 1;
State() {
qMemSet(this, 0, sizeof(State));
memset(this, 0, sizeof(State));
}
};

View File

@ -184,7 +184,7 @@ void CALLBACK_CALL_TYPE iod_read_fn(png_structp png_ptr, png_bytep data, png_siz
if (d->state == QPngHandlerPrivate::ReadingEnd && !in->isSequential() && (in->size() - in->pos()) < 4 && length == 4) {
// Workaround for certain malformed PNGs that lack the final crc bytes
uchar endcrc[4] = { 0xae, 0x42, 0x60, 0x82 };
qMemCopy(data, endcrc, 4);
memcpy(data, endcrc, 4);
in->seek(in->size());
return;
}
@ -664,7 +664,7 @@ static void set_text(const QImage &image, png_structp png_ptr, png_infop info_pt
return;
png_textp text_ptr = new png_text[text.size()];
qMemSet(text_ptr, 0, text.size() * sizeof(png_text));
memset(text_ptr, 0, text.size() * sizeof(png_text));
QMap<QString, QString>::ConstIterator it = text.constBegin();
int i = 0;

View File

@ -5594,9 +5594,9 @@ void QPainterPrivate::drawGlyphs(const quint32 *glyphArray, QFixedPoint *positio
QVarLengthArray<QFixed, 128> advances(glyphCount);
QVarLengthArray<QGlyphJustification, 128> glyphJustifications(glyphCount);
QVarLengthArray<HB_GlyphAttributes, 128> glyphAttributes(glyphCount);
qMemSet(glyphAttributes.data(), 0, glyphAttributes.size() * sizeof(HB_GlyphAttributes));
qMemSet(advances.data(), 0, advances.size() * sizeof(QFixed));
qMemSet(glyphJustifications.data(), 0, glyphJustifications.size() * sizeof(QGlyphJustification));
memset(glyphAttributes.data(), 0, glyphAttributes.size() * sizeof(HB_GlyphAttributes));
memset(advances.data(), 0, advances.size() * sizeof(QFixed));
memset(glyphJustifications.data(), 0, glyphJustifications.size() * sizeof(QGlyphJustification));
textItem.glyphs.numGlyphs = glyphCount;
textItem.glyphs.glyphs = reinterpret_cast<HB_Glyph *>(const_cast<quint32 *>(glyphArray));

View File

@ -612,7 +612,7 @@ void QPAGenerator::writeGMap()
const int numBytes = glyphCount * sizeof(quint32);
qint64 pos = buffer.size();
buffer.resize(pos + numBytes);
qMemSet(buffer.data() + pos, 0xff, numBytes);
memset(buffer.data() + pos, 0xff, numBytes);
dev->seek(pos + numBytes);
}

View File

@ -1120,7 +1120,7 @@ void QPFGenerator::writeGMap()
const int numBytes = glyphCount * sizeof(quint32);
qint64 pos = buffer.size();
buffer.resize(pos + numBytes);
qMemSet(buffer.data() + pos, 0xff, numBytes);
memset(buffer.data() + pos, 0xff, numBytes);
dev->seek(pos + numBytes);
}

View File

@ -247,7 +247,7 @@ bool QFontEngineDirectWrite::getSfntTableData(uint tag, uchar *buffer, uint *len
return false;
}
qMemCopy(buffer, tableData, tableSize);
memcpy(buffer, tableData, tableSize);
m_directWriteFontFace->ReleaseFontTable(tableContext);
return true;
@ -597,7 +597,7 @@ QImage QFontEngineDirectWrite::imageForGlyph(glyph_t t,
int size = width * height * 3;
if (size > 0) {
BYTE *alphaValues = new BYTE[size];
qMemSet(alphaValues, size, 0);
memset(alphaValues, size, 0);
hr = glyphAnalysis->CreateAlphaTexture(DWRITE_TEXTURE_CLEARTYPE_3x1,
&rect,

View File

@ -221,7 +221,7 @@ QVector<quint32> QGlyphRun::glyphIndexes() const
return d->glyphIndexes;
} else {
QVector<quint32> indexes(d->glyphIndexDataSize);
qMemCopy(indexes.data(), d->glyphIndexData, d->glyphIndexDataSize * sizeof(quint32));
memcpy(indexes.data(), d->glyphIndexData, d->glyphIndexDataSize * sizeof(quint32));
return indexes;
}
}
@ -247,7 +247,7 @@ QVector<QPointF> QGlyphRun::positions() const
return d->glyphPositions;
} else {
QVector<QPointF> glyphPositions(d->glyphPositionDataSize);
qMemCopy(glyphPositions.data(), d->glyphPositionData,
memcpy(glyphPositions.data(), d->glyphPositionData,
d->glyphPositionDataSize * sizeof(QPointF));
return glyphPositions;
}

View File

@ -504,7 +504,7 @@ QVector<QPointF> QRawFont::advancesForGlyphIndexes(const QVector<quint32> &glyph
int numGlyphs = glyphIndexes.size();
QVarLengthGlyphLayoutArray glyphs(numGlyphs);
qMemCopy(glyphs.glyphs, glyphIndexes.data(), numGlyphs * sizeof(quint32));
memcpy(glyphs.glyphs, glyphIndexes.data(), numGlyphs * sizeof(quint32));
d->fontEngine->recalcAdvances(&glyphs, 0);

View File

@ -1000,7 +1000,7 @@ void QTextEngine::shapeTextWithHarfbuzz(int item) const
kerningEnabled = this->font(si).d->kerning;
HB_ShaperItem entire_shaper_item;
qMemSet(&entire_shaper_item, 0, sizeof(entire_shaper_item));
memset(&entire_shaper_item, 0, sizeof(entire_shaper_item));
entire_shaper_item.string = reinterpret_cast<const HB_UChar16 *>(layoutData->string.constData());
entire_shaper_item.stringLength = layoutData->string.length();
entire_shaper_item.item.script = (HB_Script)si.analysis.script;

View File

@ -973,7 +973,7 @@ qint64 QNetworkReplyImpl::readData(char *data, qint64 maxlen)
if (maxAvail == 0)
return d->state == QNetworkReplyImplPrivate::Finished ? -1 : 0;
// FIXME what about "Aborted" state?
qMemCopy(data, d->downloadBuffer + d->downloadBufferReadPosition, maxAvail);
memcpy(data, d->downloadBuffer + d->downloadBufferReadPosition, maxAvail);
d->downloadBufferReadPosition += maxAvail;
return maxAvail;
}

View File

@ -850,7 +850,7 @@ int QWindowsNativeFileDialogBase::itemPaths(IShellItemArray *items,
static inline void toBuffer(const QString &what, WCHAR **ptr)
{
const int length = 1 + what.size();
qMemCopy(*ptr, what.utf16(), length * sizeof(WCHAR));
memcpy(*ptr, what.utf16(), length * sizeof(WCHAR));
*ptr += length;
}

View File

@ -275,7 +275,7 @@ bool QWindowsFontEngineDirectWrite::getSfntTableData(uint tag, uchar *buffer, ui
return false;
}
qMemCopy(buffer, tableData, tableSize);
memcpy(buffer, tableData, tableSize);
m_directWriteFontFace->ReleaseFontTable(tableContext);
return true;
@ -570,7 +570,7 @@ QImage QWindowsFontEngineDirectWrite::imageForGlyph(glyph_t t,
int size = width * height * 3;
BYTE *alphaValues = new BYTE[size];
qMemSet(alphaValues, size, 0);
memset(alphaValues, size, 0);
hr = glyphAnalysis->CreateAlphaTexture(DWRITE_TEXTURE_CLEARTYPE_3x1,
&rect,

View File

@ -143,7 +143,7 @@ QByteArray composePreprocessorOutput(const Symbols &symbols) {
const int padding = sym.lineNum - lineNum;
if (padding > 0) {
output.resize(output.size() + padding);
qMemSet(output.data() + output.size() - padding, '\n', padding);
memset(output.data() + output.size() - padding, '\n', padding);
lineNum = sym.lineNum;
}

View File

@ -779,7 +779,7 @@ void QGridLayoutPrivate::setupLayoutData(int hSpacing, int vSpacing)
adjacent to which and compute the spacings correctly.
*/
QVarLengthArray<QGridBox *> grid(rr * cc);
qMemSet(grid.data(), 0, rr * cc * sizeof(QGridBox *));
memset(grid.data(), 0, rr * cc * sizeof(QGridBox *));
/*
Initialize 'sizes' and 'grid' data structures, and insert

View File

@ -402,7 +402,7 @@ public:
if (readSize < 0)
return -1;
qMemCopy(data, openFile_->content.constData() + position_, readSize);
memcpy(data, openFile_->content.constData() + position_, readSize);
position_ += readSize;
return readSize;

View File

@ -622,7 +622,7 @@ void tst_QByteArray::fromBase64()
void tst_QByteArray::qvsnprintf()
{
char buf[20];
qMemSet(buf, 42, sizeof(buf));
memset(buf, 42, sizeof(buf));
QCOMPARE(::qsnprintf(buf, 10, "%s", "bubu"), 4);
QCOMPARE(static_cast<const char *>(buf), "bubu");
@ -631,12 +631,12 @@ void tst_QByteArray::qvsnprintf()
QCOMPARE(buf[5], char(42));
#endif
qMemSet(buf, 42, sizeof(buf));
memset(buf, 42, sizeof(buf));
QCOMPARE(::qsnprintf(buf, 5, "%s", "bubu"), 4);
QCOMPARE(static_cast<const char *>(buf), "bubu");
QCOMPARE(buf[5], char(42));
qMemSet(buf, 42, sizeof(buf));
memset(buf, 42, sizeof(buf));
#ifdef Q_OS_WIN
// VS 2005 uses the Qt implementation of vsnprintf.
# if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(Q_OS_WINCE)
@ -661,7 +661,7 @@ void tst_QByteArray::qvsnprintf()
QCOMPARE(buf[4], char(42));
#ifndef Q_OS_WIN
qMemSet(buf, 42, sizeof(buf));
memset(buf, 42, sizeof(buf));
QCOMPARE(::qsnprintf(buf, 10, ""), 0);
#endif
}

View File

@ -112,7 +112,7 @@ static void initializePadding(QImage *image)
if (paddingBytes == 0)
return;
for (int y = 0; y < image->height(); ++y) {
qMemSet(image->scanLine(y) + effectiveBytesPerLine, 0, paddingBytes);
memset(image->scanLine(y) + effectiveBytesPerLine, 0, paddingBytes);
}
}
@ -454,7 +454,7 @@ void tst_QImageWriter::saveWithNoFormat()
SKIP_IF_UNSUPPORTED(format);
QImage niceImage(64, 64, QImage::Format_ARGB32);
qMemSet(niceImage.bits(), 0, niceImage.byteCount());
memset(niceImage.bits(), 0, niceImage.byteCount());
QImageWriter writer(fileName /* , 0 - no format! */);
if (error != 0) {

View File

@ -400,7 +400,7 @@ bool tst_QFileSystemModel::createFiles(const QString &test_path, const QStringLi
if (initial_files.at(i)[0] == '.') {
QString hiddenFile = QDir::toNativeSeparators(file.fileName());
wchar_t nativeHiddenFile[MAX_PATH];
qMemSet(nativeHiddenFile, 0, sizeof(nativeHiddenFile));
memset(nativeHiddenFile, 0, sizeof(nativeHiddenFile));
hiddenFile.toWCharArray(nativeHiddenFile);
DWORD currentAttributes = ::GetFileAttributes(nativeHiddenFile);
if (currentAttributes == 0xFFFFFFFF) {

View File

@ -885,7 +885,7 @@ void tst_QItemDelegate::decoration()
}
case QVariant::Image: {
QImage img(size, QImage::Format_Mono);
qMemSet(img.bits(), 0, img.byteCount());
memset(img.bits(), 0, img.byteCount());
value = img;
break;
}

View File

@ -232,7 +232,7 @@ quint64 ImageItem::computeChecksum(const QImage &image)
uchar *p = img.bits() + bpl - padBytes;
const int h = img.height();
for (int y = 0; y < h; ++y) {
qMemSet(p, 0, padBytes);
memset(p, 0, padBytes);
p += bpl;
}
}

View File

@ -379,7 +379,7 @@ QRawVector<T>::QRawVector(int asize)
while (i != b)
new (--i) T;
} else {
qMemSet(m_begin, 0, asize * sizeof(T));
memset(m_begin, 0, asize * sizeof(T));
}
}
@ -474,7 +474,7 @@ void QRawVector<T>::realloc(int asize, int aalloc, bool ref)
} else if (asize > xsize) {
// initialize newly allocated memory to 0
qMemSet(xbegin + xsize, 0, (asize - xsize) * sizeof(T));
memset(xbegin + xsize, 0, (asize - xsize) * sizeof(T));
}
xsize = asize;