Example #1
0
void Vector<T>::__DeepCopy(const Vector& src) {
	src.Chk();
	items = alloc = src.items;
	if(src.vector) {
		vector = RawAlloc(alloc);
		DeepCopyConstructArray(vector, src.vector, src.vector + items);
	}
	else
		vector = NULL;
}
Example #2
0
void* malloc(size_t size)
{
	void *address = 0;
	int binIndex;
	for (binIndex = 0; binIndex < kBinCount; binIndex++)
		if (size <= bins[binIndex].elementSize)
			break;

	if (binIndex == kBinCount)
		address = RawAlloc(size, binIndex);
	else {
		HeapBin &bin = bins[binIndex];
		bin.lock.Lock();
		if (bin.freeList) {
			address = bin.freeList;
			bin.freeList = *reinterpret_cast<void**>(bin.freeList);
			bin.freeCount--;
		} else {
			if (bin.rawCount == 0) {
				bin.rawList = RawAlloc(bin.growSize, binIndex);
				bin.rawCount = bin.growSize / bin.elementSize;
			}

			bin.rawCount--;
			address = bin.rawList;
			bin.rawList += bin.elementSize;
		}
		
		bin.allocCount++;
		HeapPage *pages = &pageDescriptors[(reinterpret_cast<unsigned int>(address) - kHeapBase) / PAGE_SIZE];
		for (unsigned int index = 0; index < bin.elementSize / PAGE_SIZE; index++)
			pages[index].freeCount--;

		bin.lock.Unlock();
	}

	return address;
}
Example #3
0
void Vector<T>::RawInsert(int q, int count)
{
	ASSERT(count >= 0);
	ASSERT(q >= 0 && q <= items);
	if(!count) return;
	if(items + count > alloc) {
		T *newvector = RawAlloc(alloc = alloc + ntl_max(alloc, count));
		if(vector) {
			memcpy(newvector, vector, q * sizeof(T));
			memcpy(newvector + q + count, vector + q, (items - q) * sizeof(T));
			RawFree(vector);
		}
		vector = newvector;
	}
	else
		memmove(vector + q + count, vector + q, (items - q) * sizeof(T));
	items += count;
}
Example #4
0
	/** reallocate blob data if needed */
	void SmartAlloc(size_t new_size)
	{
		if (Capacity() >= new_size) return;
		/* calculate minimum block size we need to allocate
		 * and ask allocation policy for some reasonable block size */
		new_size = AllocPolicy(header_size + new_size + tail_reserve);

		/* allocate new block and setup header */
		BlobHeader *tmp = RawAlloc(new_size);
		tmp->items = Length();
		tmp->capacity = new_size - (header_size + tail_reserve);

		/* copy existing data */
		if (tmp->items != 0)
			memcpy(tmp + 1, data, tmp->items);

		/* replace our block with new one */
		if (Capacity() > 0)
			RawFree(&Hdr());
		Init(tmp);
	}