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 #include <string.h>
44 
45 /*
46     Define the container allocation functions in a separate file, so that our
47     users can easily override them.
48 */
49 
50 QT_BEGIN_NAMESPACE
51 
52 #if !QT_DEPRECATED_SINCE(5, 0)
53 // Make sure they're defined to be exported
54 Q_CORE_EXPORT void *qMalloc(size_t size) Q_ALLOC_SIZE(1);
55 Q_CORE_EXPORT void qFree(void *ptr);
56 Q_CORE_EXPORT void *qRealloc(void *ptr, size_t size) Q_ALLOC_SIZE(2);
57 #endif
58 
59 
qMalloc(size_t size)60 void *qMalloc(size_t size)
61 {
62     return ::malloc(size);
63 }
64 
qFree(void * ptr)65 void qFree(void *ptr)
66 {
67     ::free(ptr);
68 }
69 
qRealloc(void * ptr,size_t size)70 void *qRealloc(void *ptr, size_t size)
71 {
72     return ::realloc(ptr, size);
73 }
74 
qMallocAligned(size_t size,size_t alignment)75 void *qMallocAligned(size_t size, size_t alignment)
76 {
77     return qReallocAligned(nullptr, size, 0, alignment);
78 }
79 
qReallocAligned(void * oldptr,size_t newsize,size_t oldsize,size_t alignment)80 void *qReallocAligned(void *oldptr, size_t newsize, size_t oldsize, size_t alignment)
81 {
82     // fake an aligned allocation
83     void *actualptr = oldptr ? static_cast<void **>(oldptr)[-1] : nullptr;
84     if (alignment <= sizeof(void*)) {
85         // special, fast case
86         void **newptr = static_cast<void **>(realloc(actualptr, newsize + sizeof(void*)));
87         if (!newptr)
88             return nullptr;
89         if (newptr == actualptr) {
90             // realloc succeeded without reallocating
91             return oldptr;
92         }
93 
94         *newptr = newptr;
95         return newptr + 1;
96     }
97 
98     // malloc returns pointers aligned at least at sizeof(size_t) boundaries
99     // but usually more (8- or 16-byte boundaries).
100     // So we overallocate by alignment-sizeof(size_t) bytes, so we're guaranteed to find a
101     // somewhere within the first alignment-sizeof(size_t) that is properly aligned.
102 
103     // However, we need to store the actual pointer, so we need to allocate actually size +
104     // alignment anyway.
105 
106     void *real = realloc(actualptr, newsize + alignment);
107     if (!real)
108         return nullptr;
109 
110     quintptr faked = reinterpret_cast<quintptr>(real) + alignment;
111     faked &= ~(alignment - 1);
112     void **faked_ptr = reinterpret_cast<void **>(faked);
113 
114     if (oldptr) {
115         qptrdiff oldoffset = static_cast<char *>(oldptr) - static_cast<char *>(actualptr);
116         qptrdiff newoffset = reinterpret_cast<char *>(faked_ptr) - static_cast<char *>(real);
117         if (oldoffset != newoffset)
118             memmove(faked_ptr, static_cast<char *>(real) + oldoffset, qMin(oldsize, newsize));
119     }
120 
121     // now save the value of the real pointer at faked-sizeof(void*)
122     // by construction, alignment > sizeof(void*) and is a power of 2, so
123     // faked-sizeof(void*) is properly aligned for a pointer
124     faked_ptr[-1] = real;
125 
126     return faked_ptr;
127 }
128 
qFreeAligned(void * ptr)129 void qFreeAligned(void *ptr)
130 {
131     if (!ptr)
132         return;
133     void **ptr2 = static_cast<void **>(ptr);
134     free(ptr2[-1]);
135 }
136 
137 QT_END_NAMESPACE
138 
139