Qt 4.8
qvector.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qvector.h"
43 #include "qtools_p.h"
44 #include <string.h>
45 
47 
48 static inline int alignmentThreshold()
49 {
50  // malloc on 32-bit platforms should return pointers that are 8-byte aligned or more
51  // while on 64-bit platforms they should be 16-byte aligned or more
52  return 2 * sizeof(void*);
53 }
54 
56 
57 QVectorData *QVectorData::malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init)
58 {
59  QVectorData* p = (QVectorData *)qMalloc(sizeofTypedData + (size - 1) * sizeofT);
60  Q_CHECK_PTR(p);
61  ::memcpy(p, init, sizeofTypedData + (qMin(size, init->alloc) - 1) * sizeofT);
62  return p;
63 }
64 
66 {
67  return static_cast<QVectorData *>(alignment > alignmentThreshold() ? qMallocAligned(size, alignment) : qMalloc(size));
68 }
69 
70 QVectorData *QVectorData::reallocate(QVectorData *x, int newsize, int oldsize, int alignment)
71 {
72  if (alignment > alignmentThreshold())
73  return static_cast<QVectorData *>(qReallocAligned(x, newsize, oldsize, alignment));
74  return static_cast<QVectorData *>(qRealloc(x, newsize));
75 }
76 
77 void QVectorData::free(QVectorData *x, int alignment)
78 {
79  if (alignment > alignmentThreshold())
80  qFreeAligned(x);
81  else
82  qFree(x);
83 }
84 
85 int QVectorData::grow(int sizeofTypedData, int size, int sizeofT, bool excessive)
86 {
87  if (excessive)
88  return size + size / 2;
89  return qAllocMore(size * sizeofT, sizeofTypedData - sizeofT) / sizeofT;
90 }
91 
static QVectorData * reallocate(QVectorData *old, int newsize, int oldsize, int alignment)
Definition: qvector.cpp:70
static QVectorData * allocate(int size, int alignment)
Definition: qvector.cpp:65
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
Q_CORE_EXPORT void * qMallocAligned(size_t size, size_t alignment)
Definition: qmalloc.cpp:68
static void free(QVectorData *data, int alignment)
Definition: qvector.cpp:77
Q_CORE_EXPORT void qFree(void *ptr)
Definition: qmalloc.cpp:58
int alloc
Definition: qvector.h:69
Q_CORE_EXPORT void * qMalloc(size_t size)
Definition: qmalloc.cpp:53
Q_CORE_EXPORT void * qRealloc(void *ptr, size_t size)
Definition: qmalloc.cpp:63
#define Q_BASIC_ATOMIC_INITIALIZER(a)
Definition: qbasicatomic.h:218
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int qAllocMore(int alloc, int extra)
Definition: qbytearray.cpp:70
Q_CORE_EXPORT void * qReallocAligned(void *ptr, size_t size, size_t oldsize, size_t alignment)
Definition: qmalloc.cpp:73
static int alignmentThreshold()
Definition: qvector.cpp:48
static QVectorData * malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init)
Definition: qvector.cpp:57
static bool init
static int grow(int sizeofTypedData, int size, int sizeofT, bool excessive)
Definition: qvector.cpp:85
int size
Definition: qvector.h:70
#define Q_CHECK_PTR(p)
Definition: qglobal.h:1853
static QVectorData shared_null
Definition: qvector.h:82
Q_CORE_EXPORT void qFreeAligned(void *ptr)
Definition: qmalloc.cpp:118