Exemplo n.º 1
0
void* RegionAllocator::allocate(size_t size, size_t alignment)
{
	const size_t realSize = detail::sizeWithPadding<SizeHeader>(size, alignment);
	SizeHeader* header = (_currentRegion != NULL)
		? align(_currentRegion->currentPtr, alignment)
		: NULL;
	if(header == NULL || !hasSpace(_currentRegion, _regionSize, header, realSize))
	{
		Region* oldRegion = _currentRegion;

		_currentRegion = static_cast<Region*>(
			_baseAllocator.allocate(_regionSize, sizeof(void*)));
		_currentRegion->next = oldRegion;

		onAllocate(_regionSize);

		header = align(reinterpret_cast<char*>(_currentRegion) + sizeof(Region), alignment);
	}

	_currentRegion->currentPtr = reinterpret_cast<char*>(header) + realSize;

	void* realPtr = detail::dataPointer(header, alignment);
	detail::fill(header, realPtr, realSize);

	return realPtr;
}
void* RegionAllocatorFixed::allocate(size_t size, size_t alignment)
{
	if(size != _allocationSize)
	{
		assert(false);
		return NULL;
	}
	
	void* ptr = (_currentRegion != NULL)
		? align(_currentRegion->currentPtr, alignment)
		: NULL;
	if(ptr == NULL || !hasSpace(_currentRegion, _regionSize, ptr, size))
	{
		Region* oldRegion = _currentRegion;
		
		_currentRegion = static_cast<Region*>(
			_baseAllocator.allocate(_regionSize, sizeof(void*)));
		_currentRegion->next = oldRegion;
		
		onAllocate(_regionSize);
		
		ptr = align(reinterpret_cast<char*>(_currentRegion) + sizeof(Region), alignment);
		_currentRegion->currentPtr = static_cast<char*>(ptr) + size;
		
		return ptr;
	}
	
	_currentRegion->currentPtr = static_cast<char*>(ptr) + size;
	
	return ptr;
}
Exemplo n.º 3
0
bool DiskStorage::write(StorageEntry *entry)
{

  if(hasSpace())
  {
    Data d = entry->getData();
    Block b = d.wireEncode();

    std::string dname = entry->getFullName ()[-1].toUri();
    boost::erase_all(dname, "/");
    std::string fname (DISK_STORE_PATH + dname);

    //write data to disk
    FILE* f = fopen(fname.c_str (), "wb");
    fwrite(b.wire(), sizeof(uint8_t), b.size(), f);
    fclose(f);

    //delete data from MM
    entry->m_data.reset();

    currentEntries++;

    //fprintf(stderr, "DiskStorage: Added %s\n", entry->getName ().toUri().c_str());

    return true;
  }

  fprintf(stderr, "DiskStorage: Could NOT add %s\n", entry->getName ().toUri().c_str());

  return false;
}
Exemplo n.º 4
0
void NetworkMessage::putPadding(uint32_t amount)
{
	if(!hasSpace(amount))
		return;

	memset((void*)&m_buffer[m_position], 0x33, amount);
	m_size += amount;
}
Exemplo n.º 5
0
void NetworkMessage::putString(const char* value, int length, bool addSize/* = true*/)
{
	uint32_t size = (uint32_t)length;
	if(!hasSpace(size + (addSize ? 2 : 0)) || size > 8192)
		return;

	if(addSize)
		put<uint16_t>(size);

	memcpy((char*)(m_buffer + m_position), value, length);
	m_position += size;
	m_size += size;
}
Exemplo n.º 6
0
bool pc_record_type::giveToPC(item_record_type item, bool print_result)
{
	short free_space;
	char announce_string[60];

	if (item.variety == ITEM_TYPE_NO_ITEM)
		return true;

	if (item.variety == ITEM_TYPE_GOLD) {
		party.gold += item.item_level;
		ASB("You get some gold.");
		return true;
		}
	if (item.variety == ITEM_TYPE_FOOD) {
		party.food += item.item_level;
		ASB("You get some food.");
		return true;
		}
	if (item_weight(item) >  amountCanCarry() - amountCarried()) {
	  	if (print_result == true) {
		  	MessageBeep(MB_OK);
			ASB("Item too heavy to carry.");
			}
		return false;
	  	}
	if (((free_space = hasSpace()) == 24) || (!isAlive()))
		return false;
	else {
			item.item_properties = item.item_properties & 253; // not property
			item.item_properties = item.item_properties & 247; // not contained
			items[free_space] = item;


			if (print_result == 1) {
				if (stat_window == getNum())
					put_item_screen(stat_window,0);
			}
			if (in_startup_mode == false) {
				if (item.isIdent() == false)
					sprintf((char *) announce_string,"  %s gets %s.",name,item.name);
					else sprintf((char *) announce_string,"  %s gets %s.",name,item.full_name);
				if (print_result)
					add_string_to_buf((char *)announce_string);
				}

			combineThings();
			sortItems();
			return true;
			}
	return false;
}
Exemplo n.º 7
0
// returns 1 if OK, 2 if no room, 3 if not enough cash, 4 if too heavy, 5 if too many of item
short pc_record_type::okToBuy(short cost, item_record_type item)
{
	int i;

	if ((item.variety != ITEM_TYPE_GOLD) && (item.variety != ITEM_TYPE_FOOD)) {
		for (i = 0; i < 24; i++)
			if ((items[i].variety > ITEM_TYPE_NO_ITEM) && (items[i].type_flag == item.type_flag)
				&& (items[i].charges > 123))
					return 5;

		if (hasSpace() == 24) return 2;
		if (item_weight(item) > amountCanCarry() - amountCarried()) return 4;
	  	}

	if (party.takeGold(cost, false) == false)
		return 3;
	return 1;
}
Exemplo n.º 8
0
	T* pushFront(RingBuffer<T, T_ASSERT>& array, T value){
		if(T_ASSERT){
			XenAssert(xen::hasSpace(array),
			          "Expected room in order to push element to front of ring buffer"
			          );
		}

		array.first_index -= 1;
		array.size        += hasSpace(array);

		if(array.front == 0){
			array.front = array.capacity-1;
		} else {
			--array.front;
		}
		array.elements[array.front] = value;
		return &array.elements[array.front];
	}
Exemplo n.º 9
0
	T* pushBack(RingBuffer<T, T_ASSERT>& array, T value){
		if(T_ASSERT){
			XenAssert(xen::hasSpace(array),
			          "Expected room in order to push element to back of ring buffer"
			          );
		}

		if(hasSpace(array)){
			array.size += 1;
		} else {
			array.first_index += 1;
			array.front += 1;
			array.front %= array.capacity;
		}

		u64 index = (array.front + array.size - 1) % array.capacity;
		array.elements[index] = value;
		return &array.elements[index];
	}
Exemplo n.º 10
0
bool IndexPage::hasSpace(const char* key)
{
	return hasSpace(IndexNode::getSize(key));
}