qmalloc.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/global/qmalloc.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
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 The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qplatformdefs.h"-
41-
42#include <stdlib.h>-
43-
44/*-
45 Define the container allocation functions in a separate file, so that our-
46 users can easily override them.-
47*/-
48-
49QT_BEGIN_NAMESPACE-
50-
51#if !QT_DEPRECATED_SINCE(5, 0)-
52// Make sure they're defined to be exported-
53Q_CORE_EXPORT void *qMalloc(size_t size) Q_ALLOC_SIZE(1);-
54Q_CORE_EXPORT void qFree(void *ptr);-
55Q_CORE_EXPORT void *qRealloc(void *ptr, size_t size) Q_ALLOC_SIZE(2);-
56#endif-
57-
58-
59void *qMalloc(size_t size)-
60{-
61 return ::malloc(size);
never executed: return ::malloc(size);
0
62}-
63-
64void qFree(void *ptr)-
65{-
66 ::free(ptr);-
67}
never executed: end of block
0
68-
69void *qRealloc(void *ptr, size_t size)-
70{-
71 return ::realloc(ptr, size);
never executed: return ::realloc(ptr, size);
0
72}-
73-
74void *qMallocAligned(size_t size, size_t alignment)-
75{-
76 return qReallocAligned(0, size, 0, alignment);
executed 1462 times by 4 tests: return qReallocAligned(0, size, 0, alignment);
Executed by:
  • tst_Collections
  • tst_QContiguousCache
  • tst_QGuiMetaType
  • tst_QMetaType
1462
77}-
78-
79void *qReallocAligned(void *oldptr, size_t newsize, size_t oldsize, size_t alignment)-
80{-
81 // fake an aligned allocation-
82 Q_UNUSED(oldsize);-
83-
84 void *actualptr = oldptr ? static_cast<void **>(oldptr)[-1] : 0;
oldptrDescription
TRUEnever evaluated
FALSEevaluated 1462 times by 4 tests
Evaluated by:
  • tst_Collections
  • tst_QContiguousCache
  • tst_QGuiMetaType
  • tst_QMetaType
0-1462
85 if (alignment <= sizeof(void*)) {
alignment <= sizeof(void*)Description
TRUEevaluated 261 times by 4 tests
Evaluated by:
  • tst_Collections
  • tst_QContiguousCache
  • tst_QGuiMetaType
  • tst_QMetaType
FALSEevaluated 1201 times by 1 test
Evaluated by:
  • tst_Collections
261-1201
86 // special, fast case-
87 void **newptr = static_cast<void **>(realloc(actualptr, newsize + sizeof(void*)));-
88 if (!newptr)
!newptrDescription
TRUEnever evaluated
FALSEevaluated 261 times by 4 tests
Evaluated by:
  • tst_Collections
  • tst_QContiguousCache
  • tst_QGuiMetaType
  • tst_QMetaType
0-261
89 return 0;
never executed: return 0;
0
90 if (newptr == actualptr) {
newptr == actualptrDescription
TRUEnever evaluated
FALSEevaluated 261 times by 4 tests
Evaluated by:
  • tst_Collections
  • tst_QContiguousCache
  • tst_QGuiMetaType
  • tst_QMetaType
0-261
91 // realloc succeeded without reallocating-
92 return oldptr;
never executed: return oldptr;
0
93 }-
94-
95 *newptr = newptr;-
96 return newptr + 1;
executed 261 times by 4 tests: return newptr + 1;
Executed by:
  • tst_Collections
  • tst_QContiguousCache
  • tst_QGuiMetaType
  • tst_QMetaType
261
97 }-
98-
99 // malloc returns pointers aligned at least at sizeof(size_t) boundaries-
100 // but usually more (8- or 16-byte boundaries).-
101 // So we overallocate by alignment-sizeof(size_t) bytes, so we're guaranteed to find a-
102 // somewhere within the first alignment-sizeof(size_t) that is properly aligned.-
103-
104 // However, we need to store the actual pointer, so we need to allocate actually size +-
105 // alignment anyway.-
106-
107 void *real = realloc(actualptr, newsize + alignment);-
108 if (!real)
!realDescription
TRUEnever evaluated
FALSEevaluated 1201 times by 1 test
Evaluated by:
  • tst_Collections
0-1201
109 return 0;
never executed: return 0;
0
110-
111 quintptr faked = reinterpret_cast<quintptr>(real) + alignment;-
112 faked &= ~(alignment - 1);-
113-
114 void **faked_ptr = reinterpret_cast<void **>(faked);-
115-
116 // now save the value of the real pointer at faked-sizeof(void*)-
117 // by construction, alignment > sizeof(void*) and is a power of 2, so-
118 // faked-sizeof(void*) is properly aligned for a pointer-
119 faked_ptr[-1] = real;-
120-
121 return faked_ptr;
executed 1201 times by 1 test: return faked_ptr;
Executed by:
  • tst_Collections
1201
122}-
123-
124void qFreeAligned(void *ptr)-
125{-
126 if (!ptr)
!ptrDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QMetaType
FALSEevaluated 1459 times by 4 tests
Evaluated by:
  • tst_Collections
  • tst_QContiguousCache
  • tst_QGuiMetaType
  • tst_QMetaType
2-1459
127 return;
executed 2 times by 1 test: return;
Executed by:
  • tst_QMetaType
2
128 void **ptr2 = static_cast<void **>(ptr);-
129 free(ptr2[-1]);-
130}
executed 1459 times by 4 tests: end of block
Executed by:
  • tst_Collections
  • tst_QContiguousCache
  • tst_QGuiMetaType
  • tst_QMetaType
1459
131-
132QT_END_NAMESPACE-
133-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9