2012-12-04 12:40:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-12-04 12:40:18 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
2013-06-24 11:50:51 +00:00
|
|
|
** This file is part of the QtQml module of the Qt Toolkit.
|
2012-12-04 12:40:18 +00:00
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
2012-12-04 12:40:18 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2014-08-22 06:13:59 +00:00
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
2012-12-04 12:40:18 +00:00
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-08-22 06:13:59 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-12-04 12:40:18 +00:00
|
|
|
**
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
2014-08-22 06:13:59 +00:00
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2012-12-04 12:40:18 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2013-03-29 03:00:03 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
2012-12-04 12:40:18 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-04-15 09:50:16 +00:00
|
|
|
#include "qv4engine_p.h"
|
|
|
|
#include "qv4object_p.h"
|
|
|
|
#include "qv4objectproto_p.h"
|
|
|
|
#include "qv4mm_p.h"
|
2013-06-07 09:21:18 +00:00
|
|
|
#include "qv4qobjectwrapper_p.h"
|
2013-05-24 11:19:15 +00:00
|
|
|
#include <qqmlengine.h>
|
2012-12-18 14:03:26 +00:00
|
|
|
#include "PageAllocation.h"
|
|
|
|
#include "StdLibExtras.h"
|
2012-12-04 12:40:18 +00:00
|
|
|
|
|
|
|
#include <QTime>
|
|
|
|
#include <QVector>
|
2013-01-02 15:43:47 +00:00
|
|
|
#include <QVector>
|
2012-12-13 22:46:51 +00:00
|
|
|
#include <QMap>
|
2012-12-04 12:40:18 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
2012-12-10 08:56:30 +00:00
|
|
|
#include <cstdlib>
|
2013-09-12 09:06:59 +00:00
|
|
|
#include <algorithm>
|
2013-02-08 08:30:40 +00:00
|
|
|
#include "qv4alloca_p.h"
|
2014-06-02 16:33:19 +00:00
|
|
|
#include "qv4profiling_p.h"
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2013-03-12 18:49:13 +00:00
|
|
|
#ifdef V4_USE_VALGRIND
|
|
|
|
#include <valgrind/valgrind.h>
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
#endif
|
|
|
|
|
2013-06-27 19:51:22 +00:00
|
|
|
#if OS(QNX)
|
|
|
|
#include <sys/storage.h> // __tls()
|
|
|
|
#endif
|
|
|
|
|
2014-01-21 09:55:18 +00:00
|
|
|
#if USE(PTHREADS) && HAVE(PTHREAD_NP_H)
|
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
|
|
|
|
2013-06-24 13:28:00 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2013-04-19 11:03:42 +00:00
|
|
|
using namespace QV4;
|
2012-12-18 14:03:26 +00:00
|
|
|
using namespace WTF;
|
2012-12-04 12:40:18 +00:00
|
|
|
|
|
|
|
struct MemoryManager::Data
|
|
|
|
{
|
|
|
|
bool gcBlocked;
|
|
|
|
bool aggressiveGC;
|
2014-03-25 08:46:43 +00:00
|
|
|
bool gcStats;
|
2012-12-04 12:40:18 +00:00
|
|
|
ExecutionEngine *engine;
|
|
|
|
|
2013-04-15 10:40:04 +00:00
|
|
|
enum { MaxItemSize = 512 };
|
2014-11-13 20:53:27 +00:00
|
|
|
Heap::Base *smallItems[MaxItemSize/16];
|
2013-01-30 07:58:27 +00:00
|
|
|
uint nChunks[MaxItemSize/16];
|
2013-03-01 12:34:52 +00:00
|
|
|
uint availableItems[MaxItemSize/16];
|
|
|
|
uint allocCount[MaxItemSize/16];
|
2013-11-02 17:39:55 +00:00
|
|
|
int totalItems;
|
|
|
|
int totalAlloc;
|
2014-02-20 16:43:09 +00:00
|
|
|
uint maxShift;
|
2014-03-25 15:34:52 +00:00
|
|
|
std::size_t maxChunkSize;
|
2013-01-02 15:43:47 +00:00
|
|
|
struct Chunk {
|
|
|
|
PageAllocation memory;
|
|
|
|
int chunkSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
QVector<Chunk> heapChunks;
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2013-11-14 11:05:42 +00:00
|
|
|
|
|
|
|
struct LargeItem {
|
|
|
|
LargeItem *next;
|
2014-06-02 16:33:19 +00:00
|
|
|
size_t size;
|
2013-11-14 11:05:42 +00:00
|
|
|
void *data;
|
|
|
|
|
2014-11-13 20:53:27 +00:00
|
|
|
Heap::Base *heapObject() {
|
|
|
|
return reinterpret_cast<Heap::Base *>(&data);
|
2013-11-14 11:05:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
LargeItem *largeItems;
|
2014-10-27 12:51:27 +00:00
|
|
|
std::size_t totalLargeItemsAllocated;
|
2013-11-14 11:05:42 +00:00
|
|
|
|
2014-03-26 14:11:48 +00:00
|
|
|
GCDeletable *deletable;
|
2013-11-14 11:05:42 +00:00
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
// statistics:
|
|
|
|
#ifdef DETAILED_MM_STATS
|
|
|
|
QVector<unsigned> allocSizeCounters;
|
|
|
|
#endif // DETAILED_MM_STATS
|
|
|
|
|
2014-03-25 08:44:52 +00:00
|
|
|
Data()
|
|
|
|
: gcBlocked(false)
|
2012-12-04 12:40:18 +00:00
|
|
|
, engine(0)
|
2013-11-06 15:04:10 +00:00
|
|
|
, totalItems(0)
|
|
|
|
, totalAlloc(0)
|
2014-03-25 09:08:01 +00:00
|
|
|
, maxShift(6)
|
2014-03-25 15:34:52 +00:00
|
|
|
, maxChunkSize(32*1024)
|
2013-11-14 11:05:42 +00:00
|
|
|
, largeItems(0)
|
2014-10-27 12:51:27 +00:00
|
|
|
, totalLargeItemsAllocated(0)
|
2014-03-26 14:11:48 +00:00
|
|
|
, deletable(0)
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
2012-12-13 22:46:51 +00:00
|
|
|
memset(smallItems, 0, sizeof(smallItems));
|
2013-01-30 07:58:27 +00:00
|
|
|
memset(nChunks, 0, sizeof(nChunks));
|
2013-03-01 12:34:52 +00:00
|
|
|
memset(availableItems, 0, sizeof(availableItems));
|
|
|
|
memset(allocCount, 0, sizeof(allocCount));
|
2013-10-07 12:22:02 +00:00
|
|
|
aggressiveGC = !qgetenv("QV4_MM_AGGRESSIVE_GC").isEmpty();
|
2014-03-25 08:46:43 +00:00
|
|
|
gcStats = !qgetenv("QV4_MM_STATS").isEmpty();
|
2014-02-20 16:43:09 +00:00
|
|
|
|
|
|
|
QByteArray overrideMaxShift = qgetenv("QV4_MM_MAXBLOCK_SHIFT");
|
|
|
|
bool ok;
|
|
|
|
uint override = overrideMaxShift.toUInt(&ok);
|
|
|
|
if (ok && override <= 11 && override > 0)
|
|
|
|
maxShift = override;
|
2014-03-25 15:34:52 +00:00
|
|
|
|
|
|
|
QByteArray maxChunkString = qgetenv("QV4_MM_MAX_CHUNK_SIZE");
|
|
|
|
std::size_t tmpMaxChunkSize = maxChunkString.toUInt(&ok);
|
|
|
|
if (ok)
|
|
|
|
maxChunkSize = tmpMaxChunkSize;
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~Data()
|
|
|
|
{
|
2014-06-02 16:33:19 +00:00
|
|
|
for (QVector<Chunk>::iterator i = heapChunks.begin(), ei = heapChunks.end(); i != ei; ++i) {
|
|
|
|
Q_V4_PROFILE_DEALLOC(engine, 0, i->memory.size(), Profiling::HeapPage);
|
2013-01-02 15:43:47 +00:00
|
|
|
i->memory.deallocate();
|
2014-06-02 16:33:19 +00:00
|
|
|
}
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-11 15:41:42 +00:00
|
|
|
|
2013-04-19 11:03:42 +00:00
|
|
|
namespace QV4 {
|
2013-01-03 13:00:59 +00:00
|
|
|
|
|
|
|
bool operator<(const MemoryManager::Data::Chunk &a, const MemoryManager::Data::Chunk &b)
|
2013-01-02 15:43:47 +00:00
|
|
|
{
|
|
|
|
return a.memory.base() < b.memory.base();
|
|
|
|
}
|
|
|
|
|
2013-04-19 11:03:42 +00:00
|
|
|
} // namespace QV4
|
2013-01-02 15:43:47 +00:00
|
|
|
|
2014-12-03 16:25:11 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct ChunkSweepData {
|
|
|
|
ChunkSweepData() : tail(&head), head(0), isEmpty(true) { }
|
|
|
|
Heap::Base **tail;
|
|
|
|
Heap::Base *head;
|
|
|
|
bool isEmpty;
|
|
|
|
};
|
|
|
|
|
2014-12-12 15:38:09 +00:00
|
|
|
void sweepChunk(const MemoryManager::Data::Chunk &chunk, ChunkSweepData *sweepData, uint *itemsInUse, ExecutionEngine *engine)
|
2014-12-03 16:25:11 +00:00
|
|
|
{
|
|
|
|
char *chunkStart = reinterpret_cast<char*>(chunk.memory.base());
|
|
|
|
std::size_t itemSize = chunk.chunkSize;
|
|
|
|
// qDebug("chunkStart @ %p, size=%x, pos=%x", chunkStart, chunk.chunkSize, chunk.chunkSize>>4);
|
|
|
|
|
|
|
|
#ifdef V4_USE_VALGRIND
|
|
|
|
VALGRIND_DISABLE_ERROR_REPORTING;
|
|
|
|
#endif
|
|
|
|
for (char *item = chunkStart, *chunkEnd = item + chunk.memory.size() - itemSize; item <= chunkEnd; item += itemSize) {
|
|
|
|
Heap::Base *m = reinterpret_cast<Heap::Base *>(item);
|
|
|
|
// qDebug("chunk @ %p, size = %lu, in use: %s, mark bit: %s",
|
|
|
|
// item, m->size, (m->inUse ? "yes" : "no"), (m->markBit ? "true" : "false"));
|
|
|
|
|
|
|
|
Q_ASSERT((qintptr) item % 16 == 0);
|
|
|
|
|
|
|
|
if (m->markBit) {
|
|
|
|
Q_ASSERT(m->inUse);
|
|
|
|
m->markBit = 0;
|
|
|
|
sweepData->isEmpty = false;
|
|
|
|
++(*itemsInUse);
|
|
|
|
} else {
|
|
|
|
if (m->inUse) {
|
|
|
|
// qDebug() << "-- collecting it." << m << sweepData->tail << m->nextFree();
|
|
|
|
#ifdef V4_USE_VALGRIND
|
|
|
|
VALGRIND_ENABLE_ERROR_REPORTING;
|
|
|
|
#endif
|
|
|
|
if (m->internalClass->vtable->destroy)
|
|
|
|
m->internalClass->vtable->destroy(m);
|
|
|
|
|
|
|
|
memset(m, 0, itemSize);
|
|
|
|
#ifdef V4_USE_VALGRIND
|
|
|
|
VALGRIND_DISABLE_ERROR_REPORTING;
|
2014-12-12 15:38:09 +00:00
|
|
|
VALGRIND_MEMPOOL_FREE(engine->memoryManager, m);
|
2014-12-03 16:25:11 +00:00
|
|
|
#endif
|
2014-12-12 15:38:09 +00:00
|
|
|
Q_V4_PROFILE_DEALLOC(engine, m, itemSize, Profiling::SmallItem);
|
2014-12-03 16:25:11 +00:00
|
|
|
++(*itemsInUse);
|
|
|
|
}
|
|
|
|
// Relink all free blocks to rewrite references to any released chunk.
|
|
|
|
*sweepData->tail = m;
|
|
|
|
sweepData->tail = m->nextFreeRef();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef V4_USE_VALGRIND
|
|
|
|
VALGRIND_ENABLE_ERROR_REPORTING;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
MemoryManager::MemoryManager()
|
2014-03-25 08:44:52 +00:00
|
|
|
: m_d(new Data)
|
2013-04-16 09:36:56 +00:00
|
|
|
, m_persistentValues(0)
|
2013-05-23 20:13:42 +00:00
|
|
|
, m_weakValues(0)
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
2013-03-12 18:49:13 +00:00
|
|
|
#ifdef V4_USE_VALGRIND
|
2014-12-12 15:38:09 +00:00
|
|
|
VALGRIND_CREATE_MEMPOOL(this, 0, true);
|
2013-03-12 18:49:13 +00:00
|
|
|
#endif
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 20:53:27 +00:00
|
|
|
Heap::Base *MemoryManager::allocData(std::size_t size)
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
|
|
|
if (m_d->aggressiveGC)
|
|
|
|
runGC();
|
|
|
|
#ifdef DETAILED_MM_STATS
|
|
|
|
willAllocate(size);
|
|
|
|
#endif // DETAILED_MM_STATS
|
|
|
|
|
2013-11-02 17:39:55 +00:00
|
|
|
Q_ASSERT(size >= 16);
|
|
|
|
Q_ASSERT(size % 16 == 0);
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2012-12-13 22:46:51 +00:00
|
|
|
size_t pos = size >> 4;
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2013-11-14 11:05:42 +00:00
|
|
|
// doesn't fit into a small bucket
|
|
|
|
if (size >= MemoryManager::Data::MaxItemSize) {
|
2014-10-27 12:51:27 +00:00
|
|
|
if (m_d->totalLargeItemsAllocated > 8 * 1024 * 1024)
|
|
|
|
runGC();
|
|
|
|
|
2013-11-14 11:05:42 +00:00
|
|
|
// we use malloc for this
|
2014-06-02 16:33:19 +00:00
|
|
|
MemoryManager::Data::LargeItem *item = static_cast<MemoryManager::Data::LargeItem *>(
|
2014-06-13 18:51:00 +00:00
|
|
|
malloc(Q_V4_PROFILE_ALLOC(m_d->engine, size + sizeof(MemoryManager::Data::LargeItem),
|
2014-06-02 16:33:19 +00:00
|
|
|
Profiling::LargeItem)));
|
2013-12-05 13:44:46 +00:00
|
|
|
memset(item, 0, size + sizeof(MemoryManager::Data::LargeItem));
|
2013-11-14 11:05:42 +00:00
|
|
|
item->next = m_d->largeItems;
|
2014-06-02 16:33:19 +00:00
|
|
|
item->size = size;
|
2013-11-14 11:05:42 +00:00
|
|
|
m_d->largeItems = item;
|
2014-10-27 12:51:27 +00:00
|
|
|
m_d->totalLargeItemsAllocated += size;
|
2014-11-13 20:53:27 +00:00
|
|
|
return item->heapObject();
|
2013-11-14 11:05:42 +00:00
|
|
|
}
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2014-11-13 20:53:27 +00:00
|
|
|
Heap::Base *m = m_d->smallItems[pos];
|
2013-01-02 15:43:47 +00:00
|
|
|
if (m)
|
|
|
|
goto found;
|
2012-12-13 22:46:51 +00:00
|
|
|
|
2013-01-02 15:43:47 +00:00
|
|
|
// try to free up space, otherwise allocate
|
2013-11-02 17:39:55 +00:00
|
|
|
if (m_d->allocCount[pos] > (m_d->availableItems[pos] >> 1) && m_d->totalAlloc > (m_d->totalItems >> 1) && !m_d->aggressiveGC) {
|
2013-01-02 21:09:31 +00:00
|
|
|
runGC();
|
2013-01-14 15:15:01 +00:00
|
|
|
m = m_d->smallItems[pos];
|
|
|
|
if (m)
|
|
|
|
goto found;
|
|
|
|
}
|
2012-12-13 22:46:51 +00:00
|
|
|
|
2013-01-02 15:43:47 +00:00
|
|
|
// no free item available, allocate a new chunk
|
|
|
|
{
|
2014-03-25 09:08:01 +00:00
|
|
|
// allocate larger chunks at a time to avoid excessive GC, but cap at maximum chunk size (2MB by default)
|
2013-01-30 07:58:27 +00:00
|
|
|
uint shift = ++m_d->nChunks[pos];
|
2014-02-20 16:43:09 +00:00
|
|
|
if (shift > m_d->maxShift)
|
|
|
|
shift = m_d->maxShift;
|
2014-03-25 15:34:52 +00:00
|
|
|
std::size_t allocSize = m_d->maxChunkSize*(size_t(1) << shift);
|
2013-01-02 15:43:47 +00:00
|
|
|
allocSize = roundUpToMultipleOf(WTF::pageSize(), allocSize);
|
|
|
|
Data::Chunk allocation;
|
2014-06-02 16:33:19 +00:00
|
|
|
allocation.memory = PageAllocation::allocate(
|
2014-06-13 18:51:00 +00:00
|
|
|
Q_V4_PROFILE_ALLOC(m_d->engine, allocSize, Profiling::HeapPage),
|
2014-06-02 16:33:19 +00:00
|
|
|
OSAllocator::JSGCHeapPages);
|
2013-11-25 13:08:19 +00:00
|
|
|
allocation.chunkSize = int(size);
|
2013-01-02 15:43:47 +00:00
|
|
|
m_d->heapChunks.append(allocation);
|
2013-09-12 09:06:59 +00:00
|
|
|
std::sort(m_d->heapChunks.begin(), m_d->heapChunks.end());
|
2013-01-02 15:43:47 +00:00
|
|
|
char *chunk = (char *)allocation.memory.base();
|
|
|
|
char *end = chunk + allocation.memory.size() - size;
|
2014-04-03 10:35:26 +00:00
|
|
|
|
2014-11-13 20:53:27 +00:00
|
|
|
Heap::Base **last = &m_d->smallItems[pos];
|
2013-01-02 15:43:47 +00:00
|
|
|
while (chunk <= end) {
|
2014-11-13 20:53:27 +00:00
|
|
|
Heap::Base *o = reinterpret_cast<Heap::Base *>(chunk);
|
2013-01-02 15:43:47 +00:00
|
|
|
*last = o;
|
2013-02-11 15:41:42 +00:00
|
|
|
last = o->nextFreeRef();
|
2013-01-02 15:43:47 +00:00
|
|
|
chunk += size;
|
|
|
|
}
|
2013-01-02 21:09:31 +00:00
|
|
|
*last = 0;
|
2013-01-02 15:43:47 +00:00
|
|
|
m = m_d->smallItems[pos];
|
2013-11-25 13:08:19 +00:00
|
|
|
const size_t increase = allocation.memory.size()/size - 1;
|
|
|
|
m_d->availableItems[pos] += uint(increase);
|
|
|
|
m_d->totalItems += int(increase);
|
2013-03-12 18:49:13 +00:00
|
|
|
#ifdef V4_USE_VALGRIND
|
2014-12-01 09:21:27 +00:00
|
|
|
VALGRIND_MAKE_MEM_NOACCESS(allocation.memory.base(), allocSize);
|
2013-03-12 18:49:13 +00:00
|
|
|
#endif
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 15:43:47 +00:00
|
|
|
found:
|
2013-03-12 18:49:13 +00:00
|
|
|
#ifdef V4_USE_VALGRIND
|
2014-12-12 15:38:09 +00:00
|
|
|
VALGRIND_MEMPOOL_ALLOC(this, m, size);
|
2013-03-12 18:49:13 +00:00
|
|
|
#endif
|
2014-06-13 18:51:00 +00:00
|
|
|
Q_V4_PROFILE_ALLOC(m_d->engine, size, Profiling::SmallItem);
|
2013-03-12 18:49:13 +00:00
|
|
|
|
2013-11-02 17:39:55 +00:00
|
|
|
++m_d->allocCount[pos];
|
|
|
|
++m_d->totalAlloc;
|
2013-02-11 15:41:42 +00:00
|
|
|
m_d->smallItems[pos] = m->nextFree();
|
2012-12-04 12:40:18 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2014-10-21 09:05:00 +00:00
|
|
|
static void drainMarkStack(QV4::ExecutionEngine *engine, Value *markBase)
|
|
|
|
{
|
|
|
|
while (engine->jsStackTop > markBase) {
|
2014-11-01 22:04:20 +00:00
|
|
|
Heap::Base *h = engine->popForGC();
|
2014-11-01 19:56:47 +00:00
|
|
|
Q_ASSERT (h->internalClass->vtable->markObjects);
|
|
|
|
h->internalClass->vtable->markObjects(h, engine);
|
2014-10-21 09:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-28 15:46:09 +00:00
|
|
|
void MemoryManager::mark()
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
2014-01-24 21:55:39 +00:00
|
|
|
Value *markBase = m_d->engine->jsStackTop;
|
2013-01-28 15:46:09 +00:00
|
|
|
|
2013-11-02 15:30:26 +00:00
|
|
|
m_d->engine->markObjects();
|
2013-01-28 15:46:09 +00:00
|
|
|
|
2013-04-16 09:36:56 +00:00
|
|
|
PersistentValuePrivate *persistent = m_persistentValues;
|
|
|
|
while (persistent) {
|
|
|
|
if (!persistent->refcount) {
|
|
|
|
PersistentValuePrivate *n = persistent->next;
|
2013-05-23 20:13:42 +00:00
|
|
|
persistent->removeFromList();
|
2013-04-16 09:36:56 +00:00
|
|
|
delete persistent;
|
|
|
|
persistent = n;
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-02 15:30:26 +00:00
|
|
|
persistent->value.mark(m_d->engine);
|
2013-04-16 09:36:56 +00:00
|
|
|
persistent = persistent->next;
|
2014-10-21 09:05:00 +00:00
|
|
|
|
|
|
|
if (m_d->engine->jsStackTop >= m_d->engine->jsStackLimit)
|
|
|
|
drainMarkStack(m_d->engine, markBase);
|
2013-04-16 09:36:56 +00:00
|
|
|
}
|
|
|
|
|
2013-10-15 14:00:49 +00:00
|
|
|
collectFromJSStack();
|
|
|
|
|
2013-05-24 11:19:15 +00:00
|
|
|
// Preserve QObject ownership rules within JavaScript: A parent with c++ ownership
|
|
|
|
// keeps all of its children alive in JavaScript.
|
|
|
|
|
|
|
|
// Do this _after_ collectFromStack to ensure that processing the weak
|
|
|
|
// managed objects in the loop down there doesn't make then end up as leftovers
|
|
|
|
// on the stack and thus always get collected.
|
|
|
|
for (PersistentValuePrivate *weak = m_weakValues; weak; weak = weak->next) {
|
2014-11-11 22:30:54 +00:00
|
|
|
if (!weak->refcount || !weak->value.isManaged())
|
2013-05-24 11:19:15 +00:00
|
|
|
continue;
|
2014-11-11 22:30:54 +00:00
|
|
|
QObjectWrapper *qobjectWrapper = weak->value.managed()->as<QObjectWrapper>();
|
2013-05-24 11:19:15 +00:00
|
|
|
if (!qobjectWrapper)
|
|
|
|
continue;
|
2014-11-11 22:30:54 +00:00
|
|
|
QObject *qobject = qobjectWrapper->object();
|
2013-05-24 11:19:15 +00:00
|
|
|
if (!qobject)
|
|
|
|
continue;
|
2013-05-29 10:18:31 +00:00
|
|
|
bool keepAlive = QQmlData::keepAliveDuringGarbageCollection(qobject);
|
2013-05-24 11:19:15 +00:00
|
|
|
|
|
|
|
if (!keepAlive) {
|
|
|
|
if (QObject *parent = qobject->parent()) {
|
|
|
|
while (parent->parent())
|
|
|
|
parent = parent->parent();
|
|
|
|
|
2013-05-29 10:18:31 +00:00
|
|
|
keepAlive = QQmlData::keepAliveDuringGarbageCollection(parent);
|
2013-05-24 11:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keepAlive)
|
2014-11-11 22:30:54 +00:00
|
|
|
qobjectWrapper->mark(m_d->engine);
|
2013-11-02 15:30:26 +00:00
|
|
|
|
2014-10-21 09:05:00 +00:00
|
|
|
if (m_d->engine->jsStackTop >= m_d->engine->jsStackLimit)
|
|
|
|
drainMarkStack(m_d->engine, markBase);
|
2013-05-24 11:19:15 +00:00
|
|
|
}
|
2014-10-21 09:05:00 +00:00
|
|
|
|
|
|
|
drainMarkStack(m_d->engine, markBase);
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-10-15 20:27:10 +00:00
|
|
|
void MemoryManager::sweep(bool lastSweep)
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
2013-05-23 20:13:42 +00:00
|
|
|
PersistentValuePrivate *weak = m_weakValues;
|
|
|
|
while (weak) {
|
|
|
|
if (!weak->refcount) {
|
|
|
|
PersistentValuePrivate *n = weak->next;
|
|
|
|
weak->removeFromList();
|
|
|
|
delete weak;
|
|
|
|
weak = n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Managed *m = weak->value.asManaged()) {
|
2014-04-05 18:23:20 +00:00
|
|
|
if (!m->markBit()) {
|
2013-09-25 10:24:36 +00:00
|
|
|
weak->value = Primitive::undefinedValue();
|
2013-05-29 12:58:52 +00:00
|
|
|
PersistentValuePrivate *n = weak->next;
|
2013-05-23 20:13:42 +00:00
|
|
|
weak->removeFromList();
|
2013-05-29 12:58:52 +00:00
|
|
|
weak = n;
|
2013-05-23 20:13:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
weak = weak->next;
|
|
|
|
}
|
|
|
|
|
2013-06-04 12:28:13 +00:00
|
|
|
if (MultiplyWrappedQObjectMap *multiplyWrappedQObjects = m_d->engine->m_multiplyWrappedQObjects) {
|
|
|
|
for (MultiplyWrappedQObjectMap::Iterator it = multiplyWrappedQObjects->begin(); it != multiplyWrappedQObjects->end();) {
|
2014-04-05 18:23:20 +00:00
|
|
|
if (!it.value()->markBit())
|
2013-06-04 12:28:13 +00:00
|
|
|
it = multiplyWrappedQObjects->erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 16:25:11 +00:00
|
|
|
QVarLengthArray<ChunkSweepData> chunkSweepData(m_d->heapChunks.size());
|
|
|
|
uint itemsInUse[MemoryManager::Data::MaxItemSize/16];
|
|
|
|
memset(itemsInUse, 0, sizeof(itemsInUse));
|
|
|
|
|
|
|
|
for (int i = 0; i < m_d->heapChunks.size(); ++i) {
|
|
|
|
const MemoryManager::Data::Chunk &chunk = m_d->heapChunks[i];
|
2014-12-12 15:38:09 +00:00
|
|
|
sweepChunk(chunk, &chunkSweepData[i], &itemsInUse[chunk.chunkSize >> 4], m_d->engine);
|
2014-12-03 16:25:11 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 10:41:21 +00:00
|
|
|
Heap::Base **tails[MemoryManager::Data::MaxItemSize/16];
|
2014-12-03 16:25:11 +00:00
|
|
|
memset(m_d->smallItems, 0, sizeof(m_d->smallItems));
|
|
|
|
for (int pos = 0; pos < MemoryManager::Data::MaxItemSize/16; ++pos)
|
|
|
|
tails[pos] = &m_d->smallItems[pos];
|
|
|
|
|
2014-12-12 10:41:21 +00:00
|
|
|
#ifdef V4_USE_VALGRIND
|
|
|
|
VALGRIND_DISABLE_ERROR_REPORTING;
|
|
|
|
#endif
|
2014-12-03 16:25:11 +00:00
|
|
|
QVector<Data::Chunk>::iterator chunkIter = m_d->heapChunks.begin();
|
|
|
|
for (int i = 0; i < chunkSweepData.size(); ++i) {
|
|
|
|
Q_ASSERT(chunkIter != m_d->heapChunks.end());
|
|
|
|
const size_t pos = chunkIter->chunkSize >> 4;
|
|
|
|
const size_t decrease = chunkIter->memory.size()/chunkIter->chunkSize - 1;
|
|
|
|
|
|
|
|
// Release that chunk if it could have been spared since the last GC run without any difference.
|
|
|
|
if (chunkSweepData[i].isEmpty && m_d->availableItems[pos] - decrease >= itemsInUse[pos]) {
|
|
|
|
Q_V4_PROFILE_DEALLOC(m_d->engine, 0, chunkIter->memory.size(), Profiling::HeapPage);
|
|
|
|
--m_d->nChunks[pos];
|
|
|
|
m_d->availableItems[pos] -= uint(decrease);
|
|
|
|
m_d->totalItems -= int(decrease);
|
|
|
|
chunkIter->memory.deallocate();
|
|
|
|
chunkIter = m_d->heapChunks.erase(chunkIter);
|
|
|
|
continue;
|
|
|
|
} else if (chunkSweepData[i].head) {
|
|
|
|
*tails[pos] = chunkSweepData[i].head;
|
|
|
|
tails[pos] = chunkSweepData[i].tail;
|
|
|
|
}
|
|
|
|
++chunkIter;
|
|
|
|
}
|
|
|
|
for (int pos = 0; pos < MemoryManager::Data::MaxItemSize/16; ++pos)
|
|
|
|
*tails[pos] = 0;
|
2014-12-12 10:41:21 +00:00
|
|
|
#ifdef V4_USE_VALGRIND
|
|
|
|
VALGRIND_ENABLE_ERROR_REPORTING;
|
|
|
|
#endif
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2013-11-14 11:05:42 +00:00
|
|
|
Data::LargeItem *i = m_d->largeItems;
|
|
|
|
Data::LargeItem **last = &m_d->largeItems;
|
|
|
|
while (i) {
|
2014-11-13 20:53:27 +00:00
|
|
|
Heap::Base *m = i->heapObject();
|
|
|
|
Q_ASSERT(m->inUse);
|
|
|
|
if (m->markBit) {
|
|
|
|
m->markBit = 0;
|
2013-11-14 11:05:42 +00:00
|
|
|
last = &i->next;
|
|
|
|
i = i->next;
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-13 20:53:27 +00:00
|
|
|
if (m->internalClass->vtable->destroy)
|
|
|
|
m->internalClass->vtable->destroy(m);
|
2013-11-14 11:05:42 +00:00
|
|
|
|
|
|
|
*last = i->next;
|
2014-06-13 18:51:00 +00:00
|
|
|
free(Q_V4_PROFILE_DEALLOC(m_d->engine, i, i->size + sizeof(Data::LargeItem),
|
2014-06-02 16:33:19 +00:00
|
|
|
Profiling::LargeItem));
|
2013-11-14 11:05:42 +00:00
|
|
|
i = *last;
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:11:48 +00:00
|
|
|
GCDeletable *deletable = m_d->deletable;
|
2014-03-28 12:04:42 +00:00
|
|
|
m_d->deletable = 0;
|
2013-06-13 13:27:00 +00:00
|
|
|
while (deletable) {
|
|
|
|
GCDeletable *next = deletable->next;
|
|
|
|
deletable->lastCall = lastSweep;
|
|
|
|
delete deletable;
|
|
|
|
deletable = next;
|
|
|
|
}
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryManager::isGCBlocked() const
|
|
|
|
{
|
|
|
|
return m_d->gcBlocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::setGCBlocked(bool blockGC)
|
|
|
|
{
|
|
|
|
m_d->gcBlocked = blockGC;
|
|
|
|
}
|
|
|
|
|
2013-01-02 15:43:47 +00:00
|
|
|
void MemoryManager::runGC()
|
2012-12-04 12:40:18 +00:00
|
|
|
{
|
2014-03-25 08:44:52 +00:00
|
|
|
if (m_d->gcBlocked) {
|
2012-12-04 12:40:18 +00:00
|
|
|
// qDebug() << "Not running GC.";
|
2013-01-02 15:43:47 +00:00
|
|
|
return;
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 08:46:43 +00:00
|
|
|
if (!m_d->gcStats) {
|
|
|
|
mark();
|
|
|
|
sweep();
|
|
|
|
} else {
|
2014-06-12 12:33:05 +00:00
|
|
|
int totalMem = getAllocatedMem();
|
2014-03-25 08:46:43 +00:00
|
|
|
|
|
|
|
QTime t;
|
|
|
|
t.start();
|
|
|
|
mark();
|
|
|
|
int markTime = t.elapsed();
|
|
|
|
t.restart();
|
|
|
|
int usedBefore = getUsedMem();
|
2014-12-03 16:25:11 +00:00
|
|
|
int chunksBefore = m_d->heapChunks.size();
|
2014-03-25 08:46:43 +00:00
|
|
|
sweep();
|
|
|
|
int usedAfter = getUsedMem();
|
|
|
|
int sweepTime = t.elapsed();
|
|
|
|
|
|
|
|
qDebug() << "========== GC ==========";
|
|
|
|
qDebug() << "Marked object in" << markTime << "ms.";
|
|
|
|
qDebug() << "Sweeped object in" << sweepTime << "ms.";
|
|
|
|
qDebug() << "Allocated" << totalMem << "bytes in" << m_d->heapChunks.size() << "chunks.";
|
|
|
|
qDebug() << "Used memory before GC:" << usedBefore;
|
|
|
|
qDebug() << "Used memory after GC:" << usedAfter;
|
|
|
|
qDebug() << "Freed up bytes:" << (usedBefore - usedAfter);
|
2014-12-03 16:25:11 +00:00
|
|
|
qDebug() << "Released chunks:" << (chunksBefore - m_d->heapChunks.size());
|
2014-03-25 08:46:43 +00:00
|
|
|
qDebug() << "======== End GC ========";
|
|
|
|
}
|
2012-12-04 12:40:18 +00:00
|
|
|
|
2013-03-01 12:34:52 +00:00
|
|
|
memset(m_d->allocCount, 0, sizeof(m_d->allocCount));
|
2013-11-02 17:39:55 +00:00
|
|
|
m_d->totalAlloc = 0;
|
2014-10-27 12:51:27 +00:00
|
|
|
m_d->totalLargeItemsAllocated = 0;
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 12:33:05 +00:00
|
|
|
size_t MemoryManager::getUsedMem() const
|
2014-03-25 08:46:43 +00:00
|
|
|
{
|
2014-06-12 12:33:05 +00:00
|
|
|
size_t usedMem = 0;
|
|
|
|
for (QVector<Data::Chunk>::const_iterator i = m_d->heapChunks.begin(), ei = m_d->heapChunks.end(); i != ei; ++i) {
|
2014-03-25 08:46:43 +00:00
|
|
|
char *chunkStart = reinterpret_cast<char *>(i->memory.base());
|
|
|
|
char *chunkEnd = chunkStart + i->memory.size() - i->chunkSize;
|
|
|
|
for (char *chunk = chunkStart; chunk <= chunkEnd; chunk += i->chunkSize) {
|
2014-11-01 22:04:20 +00:00
|
|
|
Heap::Base *m = reinterpret_cast<Heap::Base *>(chunk);
|
2014-03-25 08:46:43 +00:00
|
|
|
Q_ASSERT((qintptr) chunk % 16 == 0);
|
2014-07-24 20:32:38 +00:00
|
|
|
if (m->inUse)
|
2014-03-25 08:46:43 +00:00
|
|
|
usedMem += i->chunkSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return usedMem;
|
|
|
|
}
|
|
|
|
|
2014-06-12 12:33:05 +00:00
|
|
|
size_t MemoryManager::getAllocatedMem() const
|
|
|
|
{
|
|
|
|
size_t total = 0;
|
|
|
|
for (int i = 0; i < m_d->heapChunks.size(); ++i)
|
|
|
|
total += m_d->heapChunks.at(i).memory.size();
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MemoryManager::getLargeItemsMem() const
|
|
|
|
{
|
|
|
|
size_t total = 0;
|
|
|
|
for (const Data::LargeItem *i = m_d->largeItems; i != 0; i = i->next)
|
|
|
|
total += i->size;
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
MemoryManager::~MemoryManager()
|
|
|
|
{
|
2013-04-16 09:36:56 +00:00
|
|
|
PersistentValuePrivate *persistent = m_persistentValues;
|
|
|
|
while (persistent) {
|
|
|
|
PersistentValuePrivate *n = persistent->next;
|
2013-09-25 10:24:36 +00:00
|
|
|
persistent->value = Primitive::undefinedValue();
|
2013-06-21 13:19:20 +00:00
|
|
|
persistent->engine = 0;
|
2013-05-06 13:11:01 +00:00
|
|
|
persistent->prev = 0;
|
2013-04-16 09:36:56 +00:00
|
|
|
persistent->next = 0;
|
|
|
|
persistent = n;
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:27:00 +00:00
|
|
|
sweep(/*lastSweep*/true);
|
2013-06-01 12:27:45 +00:00
|
|
|
#ifdef V4_USE_VALGRIND
|
2014-12-12 15:38:09 +00:00
|
|
|
VALGRIND_DESTROY_MEMPOOL(this);
|
2013-06-01 12:27:45 +00:00
|
|
|
#endif
|
2012-12-04 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 11:56:43 +00:00
|
|
|
ExecutionEngine *MemoryManager::engine() const
|
|
|
|
{
|
|
|
|
return m_d->engine;
|
|
|
|
}
|
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
void MemoryManager::setExecutionEngine(ExecutionEngine *engine)
|
|
|
|
{
|
|
|
|
m_d->engine = engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::dumpStats() const
|
|
|
|
{
|
2013-01-25 18:32:30 +00:00
|
|
|
#ifdef DETAILED_MM_STATS
|
2012-12-04 12:40:18 +00:00
|
|
|
std::cerr << "=================" << std::endl;
|
|
|
|
std::cerr << "Allocation stats:" << std::endl;
|
|
|
|
std::cerr << "Requests for each chunk size:" << std::endl;
|
|
|
|
for (int i = 0; i < m_d->allocSizeCounters.size(); ++i) {
|
|
|
|
if (unsigned count = m_d->allocSizeCounters[i]) {
|
|
|
|
std::cerr << "\t" << (i << 4) << " bytes chunks: " << count << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // DETAILED_MM_STATS
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:11:48 +00:00
|
|
|
void MemoryManager::registerDeletable(GCDeletable *d)
|
|
|
|
{
|
|
|
|
d->next = m_d->deletable;
|
|
|
|
m_d->deletable = d;
|
|
|
|
}
|
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
#ifdef DETAILED_MM_STATS
|
|
|
|
void MemoryManager::willAllocate(std::size_t size)
|
|
|
|
{
|
|
|
|
unsigned alignedSize = (size + 15) >> 4;
|
|
|
|
QVector<unsigned> &counters = m_d->allocSizeCounters;
|
|
|
|
if ((unsigned) counters.size() < alignedSize + 1)
|
|
|
|
counters.resize(alignedSize + 1);
|
|
|
|
counters[alignedSize]++;
|
|
|
|
}
|
2012-12-13 22:46:51 +00:00
|
|
|
|
2012-12-04 12:40:18 +00:00
|
|
|
#endif // DETAILED_MM_STATS
|
|
|
|
|
2013-09-03 10:40:07 +00:00
|
|
|
void MemoryManager::collectFromJSStack() const
|
|
|
|
{
|
2014-06-13 18:51:00 +00:00
|
|
|
Value *v = m_d->engine->jsStackBase;
|
|
|
|
Value *top = m_d->engine->jsStackTop;
|
2013-09-03 10:40:07 +00:00
|
|
|
while (v < top) {
|
|
|
|
Managed *m = v->asManaged();
|
2014-04-05 18:23:20 +00:00
|
|
|
if (m && m->inUse())
|
2013-09-03 10:40:07 +00:00
|
|
|
// Skip pointers to already freed objects, they are bogus as well
|
2013-11-02 15:30:26 +00:00
|
|
|
m->mark(m_d->engine);
|
2013-09-03 10:40:07 +00:00
|
|
|
++v;
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 13:28:00 +00:00
|
|
|
QT_END_NAMESPACE
|