<commit_before>/*

Copyright (c) 2007, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/storage.hpp"
#include <deque>
#include "libtorrent/disk_io_thread.hpp"

#ifdef TORRENT_DISK_STATS

#include "libtorrent/time.hpp"

#endif

namespace libtorrent
{

	disk_io_thread::disk_io_thread(int block_size)
		: m_abort(false)
		, m_queue_buffer_size(0)
		, m_pool(block_size)
#ifndef NDEBUG
		, m_block_size(block_size)
#endif
		, m_disk_io_thread(boost::ref(*this))
	{
#ifdef TORRENT_STATS
		m_allocations = 0;
#endif
#ifdef TORRENT_DISK_STATS
		m_log.open("disk_io_thread.log", std::ios::trunc);
#endif
	}

	disk_io_thread::~disk_io_thread()
	{
		boost::mutex::scoped_lock l(m_mutex);
		m_abort = true;
		m_signal.notify_all();
		l.unlock();

		m_disk_io_thread.join();
	}

	// aborts read operations
	void disk_io_thread::stop(boost::intrusive_ptr<piece_manager> s)
	{
		boost::mutex::scoped_lock l(m_mutex);
		// read jobs are aborted, write and move jobs are syncronized
		for (std::deque<disk_io_job>::iterator i = m_jobs.begin();
			i != m_jobs.end();)
		{
			if (i->storage != s)
			{
				++i;
				continue;
			}
			if (i->action == disk_io_job::read)
			{
				i->callback(-1, *i);
				m_jobs.erase(i++);
				continue;
			}
			++i;
		}
		m_signal.notify_all();
	}

	bool range_overlap(int start1, int length1, int start2, int length2)
	{
		return (start1 <= start2 && start1 + length1 > start2)
			|| (start2 <= start1 && start2 + length2 > start1);
	}

	namespace
	{
		// The semantic of this operator is:
		// shouls lhs come before rhs in the job queue
		bool operator<(disk_io_job const& lhs, disk_io_job const& rhs)
		{
			// NOTE: comparison inverted to make higher priority
			// skip _in_front_of_ lower priority
			if (lhs.priority > rhs.priority) return true;
			if (lhs.priority < rhs.priority) return false;

			if (lhs.storage.get() < rhs.storage.get()) return true;
			if (lhs.storage.get() > rhs.storage.get()) return false;
			if (lhs.piece < rhs.piece) return true;
			if (lhs.piece > rhs.piece) return false;
			if (lhs.offset < rhs.offset) return true;
//			if (lhs.offset > rhs.offset) return false;
			return false;
		}
	}

	void disk_io_thread::add_job(disk_io_job const& j
		, boost::function<void(int, disk_io_job const&)> const& f)
	{
		assert(!j.callback);
		boost::mutex::scoped_lock l(m_mutex);

		std::deque<disk_io_job>::reverse_iterator i = m_jobs.rbegin();
		if (j.action == disk_io_job::read)
		{
			// when we're reading, we may not skip
			// ahead of any write operation that overlaps
			// the region we're reading
			for (; i != m_jobs.rend(); ++i)
			{
				if (i->action == disk_io_job::read && *i < j)
					break;
				if (i->action == disk_io_job::write
					&& i->storage == j.storage
					&& i->piece == j.piece
					&& range_overlap(i->offset, i->buffer_size
						, j.offset, j.buffer_size))
				{
					// we have to stop, and we haven't
					// found a suitable place for this job
					// so just queue it up at the end
					i = m_jobs.rbegin();
					break;
				}
			}
		}
		else if (j.action == disk_io_job::write)
		{
			for (; i != m_jobs.rend(); ++i)
			{
				if (i->action == disk_io_job::write && *i < j)
				{
					if (i != m_jobs.rbegin()
						&& i.base()->storage.get() != j.storage.get())
						i = m_jobs.rbegin();
					break;
				}
			}
		}

		if (i == m_jobs.rend()) i = m_jobs.rbegin();

		std::deque<disk_io_job>::iterator k = m_jobs.insert(i.base(), j);
		k->callback.swap(const_cast<boost::function<void(int, disk_io_job const&)>&>(f));
		if (j.action == disk_io_job::write)
			m_queue_buffer_size += j.buffer_size;
		assert(j.storage.get());
		m_signal.notify_all();
	}

	char* disk_io_thread::allocate_buffer()
	{
		boost::mutex::scoped_lock l(m_mutex);
#ifdef TORRENT_STATS
		++m_allocations;
#endif
		return (char*)m_pool.ordered_malloc();
	}

	void disk_io_thread::free_buffer(char* buf)
	{
		boost::mutex::scoped_lock l(m_mutex);
#ifdef TORRENT_STATS
		--m_allocations;
#endif
		m_pool.ordered_free(buf);
	}

	void disk_io_thread::operator()()
	{
		for (;;)
		{
#ifdef TORRENT_DISK_STATS
			m_log << log_time() << " idle" << std::endl;
#endif
			boost::mutex::scoped_lock l(m_mutex);
			while (m_jobs.empty() && !m_abort)
				m_signal.wait(l);
			if (m_abort && m_jobs.empty()) return;

			boost::function<void(int, disk_io_job const&)> handler;
			handler.swap(m_jobs.front().callback);
			disk_io_job j = m_jobs.front();
			m_jobs.pop_front();
			m_queue_buffer_size -= j.buffer_size;
			l.unlock();

			int ret = 0;

			bool free_buffer = true;
			try
			{
#ifdef TORRENT_DISK_STATS
				ptime start = time_now();
#endif
//				std::cerr << "DISK THREAD: executing job: " << j.action << std::endl;
				switch (j.action)
				{
					case disk_io_job::read:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " read " << j.buffer_size << std::endl;
#endif
						free_buffer = false;
						if (j.buffer == 0)
						{
							l.lock();
							j.buffer = (char*)m_pool.ordered_malloc();
#ifdef TORRENT_STATS
							++m_allocations;
#endif
							l.unlock();
							assert(j.buffer_size <= m_block_size);
							if (j.buffer == 0)
							{
								ret = -1;
								j.str = "out of memory";
								break;
							}
						}
						ret = j.storage->read_impl(j.buffer, j.piece, j.offset
							, j.buffer_size);

						// simulates slow drives
						// usleep(300);
						break;
					case disk_io_job::write:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " write " << j.buffer_size << std::endl;
#endif
						assert(j.buffer);
						assert(j.buffer_size <= m_block_size);
						j.storage->write_impl(j.buffer, j.piece, j.offset
							, j.buffer_size);

						// simulates a slow drive
						// usleep(300);
						break;
					case disk_io_job::hash:
						{
#ifdef TORRENT_DISK_STATS
							m_log << log_time() << " hash" << std::endl;
#endif
							sha1_hash h = j.storage->hash_for_piece_impl(j.piece);
							j.str.resize(20);
							std::memcpy(&j.str[0], &h[0], 20);
						}
						break;
					case disk_io_job::move_storage:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " move" << std::endl;
#endif
						ret = j.storage->move_storage_impl(j.str) ? 1 : 0;
						j.str = j.storage->save_path().string();
						break;
					case disk_io_job::release_files:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " release" << std::endl;
#endif
						j.storage->release_files_impl();
						break;
				}
			}
			catch (std::exception& e)
			{
//				std::cerr << "DISK THREAD: exception: " << e.what() << std::endl;
				j.str = e.what();
				ret = -1;
			}

//			if (!handler) std::cerr << "DISK THREAD: no callback specified" << std::endl;
//			else std::cerr << "DISK THREAD: invoking callback" << std::endl;
			try { if (handler) handler(ret, j); }
			catch (std::exception&) {}

			if (j.buffer && free_buffer)
			{
				l.lock();
				m_pool.ordered_free(j.buffer);
#ifdef TORRENT_STATS
				--m_allocations;
#endif
			}
		}
	}
}

<commit_msg>disk io priority fix<commit_after>/*

Copyright (c) 2007, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/storage.hpp"
#include <deque>
#include "libtorrent/disk_io_thread.hpp"

#ifdef TORRENT_DISK_STATS

#include "libtorrent/time.hpp"

#endif

namespace libtorrent
{

	disk_io_thread::disk_io_thread(int block_size)
		: m_abort(false)
		, m_queue_buffer_size(0)
		, m_pool(block_size)
#ifndef NDEBUG
		, m_block_size(block_size)
#endif
		, m_disk_io_thread(boost::ref(*this))
	{
#ifdef TORRENT_STATS
		m_allocations = 0;
#endif
#ifdef TORRENT_DISK_STATS
		m_log.open("disk_io_thread.log", std::ios::trunc);
#endif
	}

	disk_io_thread::~disk_io_thread()
	{
		boost::mutex::scoped_lock l(m_mutex);
		m_abort = true;
		m_signal.notify_all();
		l.unlock();

		m_disk_io_thread.join();
	}

	// aborts read operations
	void disk_io_thread::stop(boost::intrusive_ptr<piece_manager> s)
	{
		boost::mutex::scoped_lock l(m_mutex);
		// read jobs are aborted, write and move jobs are syncronized
		for (std::deque<disk_io_job>::iterator i = m_jobs.begin();
			i != m_jobs.end();)
		{
			if (i->storage != s)
			{
				++i;
				continue;
			}
			if (i->action == disk_io_job::read)
			{
				i->callback(-1, *i);
				m_jobs.erase(i++);
				continue;
			}
			++i;
		}
		m_signal.notify_all();
	}

	bool range_overlap(int start1, int length1, int start2, int length2)
	{
		return (start1 <= start2 && start1 + length1 > start2)
			|| (start2 <= start1 && start2 + length2 > start1);
	}

	namespace
	{
		// The semantic of this operator is:
		// shouls lhs come before rhs in the job queue
		bool operator<(disk_io_job const& lhs, disk_io_job const& rhs)
		{
			// NOTE: comparison inverted to make higher priority
			// skip _in_front_of_ lower priority
			if (lhs.priority > rhs.priority) return true;
			if (lhs.priority < rhs.priority) return false;

			if (lhs.storage.get() < rhs.storage.get()) return true;
			if (lhs.storage.get() > rhs.storage.get()) return false;
			if (lhs.piece < rhs.piece) return true;
			if (lhs.piece > rhs.piece) return false;
			if (lhs.offset < rhs.offset) return true;
//			if (lhs.offset > rhs.offset) return false;
			return false;
		}
	}

	void disk_io_thread::add_job(disk_io_job const& j
		, boost::function<void(int, disk_io_job const&)> const& f)
	{
		assert(!j.callback);
		boost::mutex::scoped_lock l(m_mutex);

		std::deque<disk_io_job>::reverse_iterator i = m_jobs.rbegin();
		if (j.action == disk_io_job::read)
		{
			// when we're reading, we may not skip
			// ahead of any write operation that overlaps
			// the region we're reading
			for (; i != m_jobs.rend(); i++)
			{
				// if *i should come before j, stop
				// and insert j before i
				if (*i < j) break;
				// if we come across a write operation that
				// overlaps the region we're reading, we need
				// to stop
				if (i->action == disk_io_job::write
					&& i->storage == j.storage
					&& i->piece == j.piece
					&& range_overlap(i->offset, i->buffer_size
						, j.offset, j.buffer_size))
					break;
			}
		}
		else if (j.action == disk_io_job::write)
		{
			for (; i != m_jobs.rend(); ++i)
			{
				if (*i < j)
				{
					if (i != m_jobs.rbegin()
						&& i.base()->storage.get() != j.storage.get())
						i = m_jobs.rbegin();
					break;
				}
			}
		}

		// if we are placed in front of all other jobs, put it on the back of
		// the queue, to sweep the disk in the same direction, and to avoid
		// starvation. The exception is if the priority is higher than the
		// job at the front of the queue
		if (i == m_jobs.rend() && (m_jobs.empty() || j.priority <= m_jobs.back().priority))
			i = m_jobs.rbegin();

		std::deque<disk_io_job>::iterator k = m_jobs.insert(i.base(), j);
		k->callback.swap(const_cast<boost::function<void(int, disk_io_job const&)>&>(f));
		if (j.action == disk_io_job::write)
			m_queue_buffer_size += j.buffer_size;
		assert(j.storage.get());
		m_signal.notify_all();
	}

	char* disk_io_thread::allocate_buffer()
	{
		boost::mutex::scoped_lock l(m_mutex);
#ifdef TORRENT_STATS
		++m_allocations;
#endif
		return (char*)m_pool.ordered_malloc();
	}

	void disk_io_thread::free_buffer(char* buf)
	{
		boost::mutex::scoped_lock l(m_mutex);
#ifdef TORRENT_STATS
		--m_allocations;
#endif
		m_pool.ordered_free(buf);
	}

	void disk_io_thread::operator()()
	{
		for (;;)
		{
#ifdef TORRENT_DISK_STATS
			m_log << log_time() << " idle" << std::endl;
#endif
			boost::mutex::scoped_lock l(m_mutex);
			while (m_jobs.empty() && !m_abort)
				m_signal.wait(l);
			if (m_abort && m_jobs.empty()) return;

			boost::function<void(int, disk_io_job const&)> handler;
			handler.swap(m_jobs.front().callback);
			disk_io_job j = m_jobs.front();
			m_jobs.pop_front();
			m_queue_buffer_size -= j.buffer_size;
			l.unlock();

			int ret = 0;

			bool free_buffer = true;
			try
			{
#ifdef TORRENT_DISK_STATS
				ptime start = time_now();
#endif
//				std::cerr << "DISK THREAD: executing job: " << j.action << std::endl;
				switch (j.action)
				{
					case disk_io_job::read:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " read " << j.buffer_size << std::endl;
#endif
						free_buffer = false;
						if (j.buffer == 0)
						{
							l.lock();
							j.buffer = (char*)m_pool.ordered_malloc();
#ifdef TORRENT_STATS
							++m_allocations;
#endif
							l.unlock();
							assert(j.buffer_size <= m_block_size);
							if (j.buffer == 0)
							{
								ret = -1;
								j.str = "out of memory";
								break;
							}
						}
						ret = j.storage->read_impl(j.buffer, j.piece, j.offset
							, j.buffer_size);

						// simulates slow drives
						// usleep(300);
						break;
					case disk_io_job::write:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " write " << j.buffer_size << std::endl;
#endif
						assert(j.buffer);
						assert(j.buffer_size <= m_block_size);
						j.storage->write_impl(j.buffer, j.piece, j.offset
							, j.buffer_size);

						// simulates a slow drive
						// usleep(300);
						break;
					case disk_io_job::hash:
						{
#ifdef TORRENT_DISK_STATS
							m_log << log_time() << " hash" << std::endl;
#endif
							sha1_hash h = j.storage->hash_for_piece_impl(j.piece);
							j.str.resize(20);
							std::memcpy(&j.str[0], &h[0], 20);
						}
						break;
					case disk_io_job::move_storage:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " move" << std::endl;
#endif
						ret = j.storage->move_storage_impl(j.str) ? 1 : 0;
						j.str = j.storage->save_path().string();
						break;
					case disk_io_job::release_files:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " release" << std::endl;
#endif
						j.storage->release_files_impl();
						break;
				}
			}
			catch (std::exception& e)
			{
//				std::cerr << "DISK THREAD: exception: " << e.what() << std::endl;
				j.str = e.what();
				ret = -1;
			}

//			if (!handler) std::cerr << "DISK THREAD: no callback specified" << std::endl;
//			else std::cerr << "DISK THREAD: invoking callback" << std::endl;
			try { if (handler) handler(ret, j); }
			catch (std::exception&) {}

			if (j.buffer && free_buffer)
			{
				l.lock();
				m_pool.ordered_free(j.buffer);
#ifdef TORRENT_STATS
				--m_allocations;
#endif
			}
		}
	}
}

<|endoftext|>


<commit_before>#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

static void panic(...) __attribute__((noreturn));

#ifdef DEBUG
#define debug printf
#define assert xassert
#define xassert(e) if (e); else panic(#e)
#define IFDEBUG(X) X
#else
#define debug(...) (void)0
#define assert(...) (void)0
#define IFDEBUG(X) /* nothing */
#endif

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

/**
 * These are adapted to be stored internally in whatever data we have.
 */
struct pairing_ptr_heap
{
	typedef pairing_ptr_heap T;
	typedef pairing_ptr_heap* Tp;

	/**
	 * down,right: first child and right sibling of this page in pairing heap
	 * of pages with free space.
	 */
	pairing_ptr_heap* down;
	pairing_ptr_heap* right;

	Tp delete_min()
	{
		assert(!right);
		Tp l = down;
		down = NULL;
		return mergePairs(l);
	}

	static Tp mergePairs(Tp l)
	{
		if (!l || !l->right) return l;

		Tp r = l->right;
		Tp hs = r->right;
		l->right = r->right = NULL;
		assert(hs != l && hs != r && r != l);
		// FIXME recursion...
		// We can use l->right after merge returns, since merge() always
		// returns something with a right-value of NULL. l will never be
		// touched until we're about to merge it with the result of mergePairs
		l = merge(l,r);
		hs = mergePairs(hs);
		return merge(l,hs);
	}

	static Tp merge(Tp l, Tp r)
	{
		if (!l)
		{
			assert(!r->right);
			return r;
		}
		if (!r)
		{
			assert(!l->right);
			return l;
		}

		assert(!l->right && !r->right);

		if (r < l)
		{
			Tp tmp = r;
			r = l;
			l = tmp;
		}
		// l <= r!

		r->right = l->down;
		l->down = r;
		//l->right = NULL; // we know it's already null
		return l;
	}
};
static pairing_ptr_heap* delete_min(pairing_ptr_heap* p)
{
	assert(p);
	return p->delete_min();
}
static pairing_ptr_heap* insert(pairing_ptr_heap* l, pairing_ptr_heap* r)
{
	return pairing_ptr_heap::merge(l, r);
}

struct pageinfo
{
	/**
	 * Pointer to where the page starts.
	 * TODO: Pack some stuff in here. *Twelve* vacant bits!
	 */
	void* page;

	/**
	 * The heap of address-sorted pages in this category that have free pages
	 */
	pairing_ptr_heap heap;

	uint16_t size;
	//uint16_t isize;
	uint16_t chunks;
	uint16_t chunks_free;
	uint8_t index; // index into array of pages

	/**
	 * 1-32 bytes of bitmap data. A set bit means *free* chunk.
	 */
	uint8_t bitmap[];

	// TODO merge bitmap into page?
};
#define pageinfo_from_heap(heap_) \
	((pageinfo*)((char*)(heap_) - offsetof(pageinfo, heap)))

static bool page_filled(pageinfo* page)
{
	return !page->chunks_free;
}
static void* page_get_chunk(pageinfo* page)
{
	page->chunks_free--;
	uint8_t* bitmap = page->bitmap;
	for (size_t i = 0; i < page->chunks / 8; i++)
	{
		const uint8_t found = bitmap[i];
		if (found)
		{
			uint8_t mask = 0x80;
			size_t n = i << 3;
			while (mask)
			{
				if (mask & found)
				{
					bitmap[i] = found & ~mask;
					return (uint8_t*)page->page + (n * page->size);
				}
				mask >>= 1;
				n++;
			}
		}
	}
	panic("No free chunks found?");
}
static void page_free_chunk(pageinfo* page, void* ptr)
{
	size_t offset_in_page = (uint8_t*)ptr - (uint8_t*)page->page;
	size_t ix = offset_in_page;
	ix /= page->size; // FIXME store inverse or something instead
	uint8_t mask = 1 << (7 - (ix & 7));
	size_t byte = ix >> 3;
	debug("Freeing %p in %p (size %d): oring byte %d with %#x\n", ptr, page->page, page->size, byte, mask);
	page->bitmap[byte] |= mask;
	page->chunks_free++;
}

#define N_SIZES (128/16+5)
#define PAGE_SHIFT 12
#define PAGE_SIZE (1 << PAGE_SHIFT)

//static pairing_ptr_heap* g_free_pages;
static pageinfo* g_chunk_pages[N_SIZES];
static uintptr_t g_first_page;
static uintptr_t g_n_pages;
// Assumes mostly contiguous pages...
static pageinfo** g_pages;

static void panic(...)
{
	abort();
}

static size_t size_ix(size_t size)
{
	if (size <= 128)
		return (size + 15) / 16 - 1;

	size_t ix = 8; // 256 bytes
	size >>= 8;
	while (size) { size >>= 1; ix++; }
	return ix;
}

static size_t ix_size(size_t ix)
{
	return ix < 8 ? 16 * (ix + 1) : (1 << ix);
}

static void* get_page()
{
	/*if (void* ret = g_free_pages)
	{
		g_free_pages = delete_min(g_free_pages);
		return ret;
	}
	else*/
	{
		uintptr_t cur = (uintptr_t)sbrk(0);
		sbrk((PAGE_SIZE - cur) & (PAGE_SIZE - 1));
		return sbrk(PAGE_SIZE);
	}
}

static void set_pageinfo(void* page, pageinfo* info)
{
	uintptr_t offset;
	if (g_pages)
	{
		offset = ((uintptr_t)page - g_first_page) >> PAGE_SHIFT;
	}
	else
	{
		g_first_page = (uintptr_t)page;
		offset = 0;
	}

	if (offset >= g_n_pages)
	{
		size_t required = (offset + PAGE_SIZE) & ~(PAGE_SIZE-1);
		pageinfo** new_pages = (pageinfo**)mmap(NULL, required, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
		assert(new_pages != MAP_FAILED);

		memcpy(new_pages, g_pages, g_n_pages * sizeof(pageinfo*));
		munmap(g_pages, g_n_pages * sizeof(pageinfo*));

		g_pages = new_pages;
		g_n_pages = required / sizeof(pageinfo*);
	}

	g_pages[offset] = info;
}

static pageinfo* new_chunkpage(size_t size)
{
	size_t ix = size_ix(size);
	size = ix_size(ix);

	size_t nchunks = 4096/size;
	pageinfo* ret = NULL;
	size_t pisize = sizeof(pageinfo) + nchunks/8;
	if (!g_chunk_pages[size_ix(pisize)])
	{
		ret = (pageinfo*)get_page();
		set_pageinfo(ret, NULL);
	}
	else
	{
		ret = (pageinfo*)malloc(pisize);
	}

	memset(&ret->heap, 0, sizeof(ret->heap));
	ret->page = get_page();
	ret->size = size;
//	ret->shift = ix_shift(ix);
	ret->chunks = nchunks;
	ret->chunks_free = nchunks;
	ret->index = ix;

	memset(ret->bitmap, 0xff, nchunks/8);

	set_pageinfo(ret->page, ret);

	return ret;
}

static pageinfo* ptr_pageinfo(void* ptr)
{
	uintptr_t offset = ((uintptr_t)ptr - g_first_page) >> PAGE_SHIFT;
	if (offset > g_n_pages) return NULL;
	pageinfo* ret = g_pages[offset];
	assert(!ret || !(((uintptr_t)ret->page ^ (uintptr_t)ptr) >> PAGE_SHIFT));
	return ret;
}

static size_t get_alloc_size(void* ptr)
{
	pageinfo* info = ptr_pageinfo(ptr);
	if (!info) panic("get_alloc_size for unknown pointer %p", ptr);
	return info->size;
}

void *malloc(size_t size)
{
	if (!size) return NULL;

	pageinfo** pagep = g_chunk_pages + size_ix(size);
	pageinfo* page = *pagep;
	if (unlikely(!page))
	{
		debug("Adding new chunk page for size %lu (cat %ld)\n", size, size_ix(size));
		page = new_chunkpage(size);
		*pagep = page;
	}
	debug("Allocating chunk from %p (info %p, %d left)\n", page->page, page, page->chunks_free);
	void* ret = page_get_chunk(page);
	if (unlikely(page_filled(page)))
	{
		debug("Page %p (info %p) filled\n", page->page, page);
		pairing_ptr_heap* newpage = delete_min(&page->heap);
		*pagep = newpage ? pageinfo_from_heap(newpage) : NULL;
	}
	return ret;
}

void* calloc(size_t n, size_t sz)
{
	size_t size = n * sz;
	void* ptr = malloc(size);
	if (likely(ptr)) memset(ptr, 0, size);
	return ptr;
}

void* realloc(void* ptr, size_t new_size)
{
	size_t old_size = get_alloc_size(ptr);
	void* ret = malloc(new_size);
	if (likely(ret))
	{
		memcpy(ret, ptr, unlikely(new_size < old_size) ? new_size : old_size);
	}
	return ret;
}

void free(void *ptr)
{
	if (unlikely(!ptr)) return;

	pageinfo* page = ptr_pageinfo(ptr);
	if (unlikely(!page)) panic("free on unknown pointer %p", ptr);

	bool was_filled = page_filled(page);
	page_free_chunk(page, ptr);
	if (was_filled)
	{
		pageinfo** pagep = g_chunk_pages + page->index;
		pageinfo* free_page = *pagep;
		if (!free_page)
			*pagep = page;
		else
			*pagep = pageinfo_from_heap(insert(&free_page->heap, &page->heap));
	}
	else if (unlikely(page->chunks_free == page->chunks))
	{
		debug("Free: page %p (info %p) is now free\n", page->page, page);
	}
}

#ifdef TEST

static int32_t xrand()
{
	static int32_t m_w = 1246987127, m_z = 789456123;
	m_z = 36969 * (m_z & 65535) + (m_z >> 16);
	m_w = 18000 * (m_w & 65535) + (m_w >> 16);
	return (m_z << 16) + m_w;  /* 32-bit result */
}

const size_t DELAY = 10;
const size_t NTESTS = 100000;
const size_t MAXALLOC = 512;

int main()
{
	void* ptrs[DELAY] = {0};
	for (size_t i = 0; i < NTESTS; i++)
	{
		size_t size = i % MAXALLOC;
		void** p = ptrs + (i % DELAY);
		debug("free(%p)\n", *p);
		free(*p);
		debug("malloc(%lu)\n", (unsigned long)size);
		*p = malloc(size);
		debug("malloc(%lu): %p\n", (unsigned long)size, *p);
		for (size_t j = 0; j < DELAY; j++)
		{
			assert(ptrs + j == p || !p[0] || ptrs[j] != p[0]);
		}
	}
}
#endif
<commit_msg>Add some asserts<commit_after>#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

static void panic(...) __attribute__((noreturn));

#ifdef DEBUG
#define debug printf
#define assert xassert
#define xassert(e) if (e); else panic(#e)
#define IFDEBUG(X) X
#else
#define debug(...) (void)0
#define assert(...) (void)0
#define IFDEBUG(X) /* nothing */
#endif

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

/**
 * These are adapted to be stored internally in whatever data we have.
 */
struct pairing_ptr_heap
{
	typedef pairing_ptr_heap T;
	typedef pairing_ptr_heap* Tp;

	/**
	 * down,right: first child and right sibling of this page in pairing heap
	 * of pages with free space.
	 */
	pairing_ptr_heap* down;
	pairing_ptr_heap* right;

	Tp delete_min()
	{
		assert(!right);
		Tp l = down;
		down = NULL;
		return mergePairs(l);
	}

	static Tp mergePairs(Tp l)
	{
		if (!l || !l->right) return l;

		Tp r = l->right;
		Tp hs = r->right;
		l->right = r->right = NULL;
		assert(hs != l && hs != r && r != l);
		// FIXME recursion...
		// We can use l->right after merge returns, since merge() always
		// returns something with a right-value of NULL. l will never be
		// touched until we're about to merge it with the result of mergePairs
		l = merge(l,r);
		hs = mergePairs(hs);
		return merge(l,hs);
	}

	static Tp merge(Tp l, Tp r)
	{
		if (!l)
		{
			assert(!r->right);
			return r;
		}
		if (!r)
		{
			assert(!l->right);
			return l;
		}

		assert(!l->right && !r->right);

		if (r < l)
		{
			Tp tmp = r;
			r = l;
			l = tmp;
		}
		// l <= r!

		r->right = l->down;
		l->down = r;
		//l->right = NULL; // we know it's already null
		return l;
	}
};
static pairing_ptr_heap* delete_min(pairing_ptr_heap* p)
{
	assert(p);
	return p->delete_min();
}
static pairing_ptr_heap* insert(pairing_ptr_heap* l, pairing_ptr_heap* r)
{
	return pairing_ptr_heap::merge(l, r);
}

struct pageinfo
{
	/**
	 * Pointer to where the page starts.
	 * TODO: Pack some stuff in here. *Twelve* vacant bits!
	 */
	void* page;

	/**
	 * The heap of address-sorted pages in this category that have free pages
	 */
	pairing_ptr_heap heap;

	uint16_t size;
	//uint16_t isize;
	uint16_t chunks;
	uint16_t chunks_free;
	uint8_t index; // index into array of pages

	/**
	 * 1-32 bytes of bitmap data. A set bit means *free* chunk.
	 */
	uint8_t bitmap[];

	// TODO merge bitmap into page?
};
#define pageinfo_from_heap(heap_) \
	((pageinfo*)((char*)(heap_) - offsetof(pageinfo, heap)))

static bool page_filled(pageinfo* page)
{
	return !page->chunks_free;
}
static void* page_get_chunk(pageinfo* page)
{
	page->chunks_free--;
	uint8_t* bitmap = page->bitmap;
	for (size_t i = 0; i < page->chunks / 8; i++)
	{
		const uint8_t found = bitmap[i];
		if (found)
		{
			uint8_t mask = 0x80;
			size_t n = i << 3;
			while (mask)
			{
				if (mask & found)
				{
					bitmap[i] = found & ~mask;
					return (uint8_t*)page->page + (n * page->size);
				}
				mask >>= 1;
				n++;
			}
		}
	}
	panic("No free chunks found?");
}
static void page_free_chunk(pageinfo* page, void* ptr)
{
	size_t offset_in_page = (uint8_t*)ptr - (uint8_t*)page->page;
	size_t ix = offset_in_page;
	ix /= page->size; // FIXME store inverse or something instead
	uint8_t mask = 1 << (7 - (ix & 7));
	size_t byte = ix >> 3;
	debug("Freeing %p in %p (size %d): oring byte %d with %#x\n", ptr, page->page, page->size, byte, mask);
	page->bitmap[byte] |= mask;
	page->chunks_free++;
}

#define N_SIZES (128/16+5)
#define PAGE_SHIFT 12
#define PAGE_SIZE (1 << PAGE_SHIFT)

//static pairing_ptr_heap* g_free_pages;
static pageinfo* g_chunk_pages[N_SIZES];
static uintptr_t g_first_page;
static uintptr_t g_n_pages;
// Assumes mostly contiguous pages...
static pageinfo** g_pages;

static void panic(...)
{
	abort();
}

static size_t size_ix(size_t size)
{
	if (size <= 128)
		return (size + 15) / 16 - 1;

	size_t ix = 8; // 256 bytes
	size >>= 8;
	while (size) { size >>= 1; ix++; }
	return ix;
}

static size_t ix_size(size_t ix)
{
	return ix < 8 ? 16 * (ix + 1) : (1 << ix);
}

static void* get_page()
{
	/*if (void* ret = g_free_pages)
	{
		g_free_pages = delete_min(g_free_pages);
		return ret;
	}
	else*/
	{
		uintptr_t cur = (uintptr_t)sbrk(0);
		sbrk((PAGE_SIZE - cur) & (PAGE_SIZE - 1));
		return sbrk(PAGE_SIZE);
	}
}

static void set_pageinfo(void* page, pageinfo* info)
{
	uintptr_t offset;
	if (g_pages)
	{
		offset = ((uintptr_t)page - g_first_page) >> PAGE_SHIFT;
	}
	else
	{
		g_first_page = (uintptr_t)page;
		offset = 0;
	}

	if (offset >= g_n_pages)
	{
		size_t required = (offset + PAGE_SIZE) & ~(PAGE_SIZE-1);
		pageinfo** new_pages = (pageinfo**)mmap(NULL, required, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
		assert(new_pages != MAP_FAILED);

		memcpy(new_pages, g_pages, g_n_pages * sizeof(pageinfo*));
		munmap(g_pages, g_n_pages * sizeof(pageinfo*));

		g_pages = new_pages;
		g_n_pages = required / sizeof(pageinfo*);
	}

	g_pages[offset] = info;
}

static pageinfo* new_chunkpage(size_t size)
{
	size_t ix = size_ix(size);
	assert(ix_size(ix) >= size);
	size = ix_size(ix);

	size_t nchunks = 4096/size;
	pageinfo* ret = NULL;
	size_t pisize = sizeof(pageinfo) + nchunks/8;
	if (!g_chunk_pages[size_ix(pisize)])
	{
		ret = (pageinfo*)get_page();
		set_pageinfo(ret, NULL);
	}
	else
	{
		ret = (pageinfo*)malloc(pisize);
	}

	memset(&ret->heap, 0, sizeof(ret->heap));
	ret->page = get_page();
	ret->size = size;
//	ret->shift = ix_shift(ix);
	ret->chunks = nchunks;
	ret->chunks_free = nchunks;
	ret->index = ix;

	memset(ret->bitmap, 0xff, nchunks/8);

	set_pageinfo(ret->page, ret);

	return ret;
}

static pageinfo* ptr_pageinfo(void* ptr)
{
	uintptr_t offset = ((uintptr_t)ptr - g_first_page) >> PAGE_SHIFT;
	if (offset > g_n_pages) return NULL;
	pageinfo* ret = g_pages[offset];
	assert(!ret || !(((uintptr_t)ret->page ^ (uintptr_t)ptr) >> PAGE_SHIFT));
	return ret;
}

static size_t get_alloc_size(void* ptr)
{
	pageinfo* info = ptr_pageinfo(ptr);
	if (!info) panic("get_alloc_size for unknown pointer %p", ptr);
	return info->size;
}

void *malloc(size_t size)
{
	if (!size) return NULL;

	assert(size < PAGE_SIZE / 2);

	pageinfo** pagep = g_chunk_pages + size_ix(size);
	pageinfo* page = *pagep;
	if (unlikely(!page))
	{
		debug("Adding new chunk page for size %lu (cat %ld, size %ld)\n", size, size_ix(size), ix_size(size_ix(size)));
		page = new_chunkpage(size);
		*pagep = page;
	}
	debug("Allocating chunk from %p (info %p, %d left)\n", page->page, page, page->chunks_free);
	void* ret = page_get_chunk(page);
	if (unlikely(page_filled(page)))
	{
		debug("Page %p (info %p) filled\n", page->page, page);
		pairing_ptr_heap* newpage = delete_min(&page->heap);
		*pagep = newpage ? pageinfo_from_heap(newpage) : NULL;
	}
	return ret;
}

void* calloc(size_t n, size_t sz)
{
	size_t size = n * sz;
	void* ptr = malloc(size);
	if (likely(ptr)) memset(ptr, 0, size);
	return ptr;
}

void* realloc(void* ptr, size_t new_size)
{
	size_t old_size = get_alloc_size(ptr);
	void* ret = malloc(new_size);
	if (likely(ret))
	{
		memcpy(ret, ptr, unlikely(new_size < old_size) ? new_size : old_size);
	}
	return ret;
}

void free(void *ptr)
{
	if (unlikely(!ptr)) return;

	pageinfo* page = ptr_pageinfo(ptr);
	if (unlikely(!page)) panic("free on unknown pointer %p", ptr);

	bool was_filled = page_filled(page);
	page_free_chunk(page, ptr);
	if (was_filled)
	{
		pageinfo** pagep = g_chunk_pages + page->index;
		pageinfo* free_page = *pagep;
		if (!free_page)
			*pagep = page;
		else
			*pagep = pageinfo_from_heap(insert(&free_page->heap, &page->heap));
	}
	else if (unlikely(page->chunks_free == page->chunks))
	{
		debug("Free: page %p (info %p) is now free\n", page->page, page);
	}
}

#ifdef TEST

static int32_t xrand()
{
	static int32_t m_w = 1246987127, m_z = 789456123;
	m_z = 36969 * (m_z & 65535) + (m_z >> 16);
	m_w = 18000 * (m_w & 65535) + (m_w >> 16);
	return (m_z << 16) + m_w;  /* 32-bit result */
}

const size_t DELAY = 10;
const size_t NTESTS = 100000;
const size_t MAXALLOC = 512;

int main()
{
	void* ptrs[DELAY] = {0};
	for (size_t i = 0; i < NTESTS; i++)
	{
		size_t size = i % MAXALLOC;
		void** p = ptrs + (i % DELAY);
		debug("free(%p)\n", *p);
		free(*p);
		debug("malloc(%lu)\n", (unsigned long)size);
		*p = malloc(size);
		debug("malloc(%lu): %p\n", (unsigned long)size, *p);
		for (size_t j = 0; j < DELAY; j++)
		{
			assert(ptrs + j == p || !p[0] || ptrs[j] != p[0]);
		}
	}
}
#endif
<|endoftext|>


<commit_before>//===- EhFrame.cpp -------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// .eh_frame section contains information on how to unwind the stack when
// an exception is thrown. The section consists of sequence of CIE and FDE
// records. The linker needs to merge CIEs and associate FDEs to CIEs.
// That means the linker has to understand the format of the section.
//
// This file contains a few utility functions to read .eh_frame contents.
//
//===----------------------------------------------------------------------===//

#include "EhFrame.h"
#include "Config.h"
#include "InputSection.h"
#include "Relocations.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Strings.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/Object/ELF.h"
#include "llvm/Support/Endian.h"

using namespace llvm;
using namespace llvm::ELF;
using namespace llvm::dwarf;
using namespace llvm::object;
using namespace llvm::support::endian;

using namespace lld;
using namespace lld::elf;

namespace {
class EhReader {
public:
  EhReader(InputSectionBase *S, ArrayRef<uint8_t> D) : IS(S), D(D) {}
  size_t readEhRecordSize();
  uint8_t getFdeEncoding();

private:
  template <class P> void failOn(const P *Loc, const Twine &Msg) {
    fatal("corrupted .eh_frame: " + Msg + "\n>>> defined in " +
          IS->getObjMsg((const uint8_t *)Loc - IS->Data.data()));
  }

  uint8_t readByte();
  void skipBytes(size_t Count);
  StringRef readString();
  void skipLeb128();
  void skipAugP();

  InputSectionBase *IS;
  ArrayRef<uint8_t> D;
};
}

size_t elf::readEhRecordSize(InputSectionBase *S, size_t Off) {
  return EhReader(S, S->Data.slice(Off)).readEhRecordSize();
}

// .eh_frame section is a sequence of records. Each record starts with
// a 4 byte length field. This function reads the length.
size_t EhReader::readEhRecordSize() {
  if (D.size() < 4)
    failOn(D.data(), "CIE/FDE too small");

  // First 4 bytes of CIE/FDE is the size of the record.
  // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
  // but we do not support that format yet.
  uint64_t V = read32(D.data(), Config->Endianness);
  if (V == UINT32_MAX)
    failOn(D.data(), "CIE/FDE too large");
  uint64_t Size = V + 4;
  if (Size > D.size())
    failOn(D.data(), "CIE/FDE ends past the end of the section");
  return Size;
}

// Read a byte and advance D by one byte.
uint8_t EhReader::readByte() {
  if (D.empty())
    failOn(D.data(), "unexpected end of CIE");
  uint8_t B = D.front();
  D = D.slice(1);
  return B;
}

void EhReader::skipBytes(size_t Count) {
  if (D.size() < Count)
    failOn(D.data(), "CIE is too small");
  D = D.slice(Count);
}

// Read a null-terminated string.
StringRef EhReader::readString() {
  const uint8_t *End = std::find(D.begin(), D.end(), '\0');
  if (End == D.end())
    failOn(D.data(), "corrupted CIE (failed to read string)");
  StringRef S = toStringRef(D.slice(0, End - D.begin()));
  D = D.slice(S.size() + 1);
  return S;
}

// Skip an integer encoded in the LEB128 format.
// Actual number is not of interest because only the runtime needs it.
// But we need to be at least able to skip it so that we can read
// the field that follows a LEB128 number.
void EhReader::skipLeb128() {
  const uint8_t *ErrPos = D.data();
  while (!D.empty()) {
    uint8_t Val = D.front();
    D = D.slice(1);
    if ((Val & 0x80) == 0)
      return;
  }
  failOn(ErrPos, "corrupted CIE (failed to read LEB128)");
}

static size_t getAugPSize(unsigned Enc) {
  switch (Enc & 0x0f) {
  case DW_EH_PE_absptr:
  case DW_EH_PE_signed:
    return Config->Wordsize;
  case DW_EH_PE_udata2:
  case DW_EH_PE_sdata2:
    return 2;
  case DW_EH_PE_udata4:
  case DW_EH_PE_sdata4:
    return 4;
  case DW_EH_PE_udata8:
  case DW_EH_PE_sdata8:
    return 8;
  }
  return 0;
}

void EhReader::skipAugP() {
  uint8_t Enc = readByte();
  if ((Enc & 0xf0) == DW_EH_PE_aligned)
    failOn(D.data() - 1, "DW_EH_PE_aligned encoding is not supported");
  size_t Size = getAugPSize(Enc);
  if (Size == 0)
    failOn(D.data() - 1, "unknown FDE encoding");
  if (Size >= D.size())
    failOn(D.data() - 1, "corrupted CIE");
  D = D.slice(Size);
}

uint8_t elf::getFdeEncoding(EhSectionPiece *P) {
  return EhReader(P->Sec, P->data()).getFdeEncoding();
}

uint8_t EhReader::getFdeEncoding() {
  skipBytes(8);
  int Version = readByte();
  if (Version != 1 && Version != 3)
    failOn(D.data() - 1,
           "FDE version 1 or 3 expected, but got " + Twine(Version));

  StringRef Aug = readString();

  // Skip code and data alignment factors.
  skipLeb128();
  skipLeb128();

  // Skip the return address register. In CIE version 1 this is a single
  // byte. In CIE version 3 this is an unsigned LEB128.
  if (Version == 1)
    readByte();
  else
    skipLeb128();

  // We only care about an 'R' value, but other records may precede an 'R'
  // record. Unfortunately records are not in TLV (type-length-value) format,
  // so we need to teach the linker how to skip records for each type.
  for (char C : Aug) {
    if (C == 'R')
      return readByte();
    if (C == 'z') {
      skipLeb128();
      continue;
    }
    if (C == 'P') {
      skipAugP();
      continue;
    }
    if (C == 'L') {
      readByte();
      continue;
    }
    failOn(Aug.data(), "unknown .eh_frame augmentation string: " + Aug);
  }
  return DW_EH_PE_absptr;
}
<commit_msg>[ELF] Simplify read32. NFC<commit_after>//===- EhFrame.cpp -------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// .eh_frame section contains information on how to unwind the stack when
// an exception is thrown. The section consists of sequence of CIE and FDE
// records. The linker needs to merge CIEs and associate FDEs to CIEs.
// That means the linker has to understand the format of the section.
//
// This file contains a few utility functions to read .eh_frame contents.
//
//===----------------------------------------------------------------------===//

#include "EhFrame.h"
#include "Config.h"
#include "InputSection.h"
#include "Relocations.h"
#include "Target.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Strings.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/Object/ELF.h"

using namespace llvm;
using namespace llvm::ELF;
using namespace llvm::dwarf;
using namespace llvm::object;

using namespace lld;
using namespace lld::elf;

namespace {
class EhReader {
public:
  EhReader(InputSectionBase *S, ArrayRef<uint8_t> D) : IS(S), D(D) {}
  size_t readEhRecordSize();
  uint8_t getFdeEncoding();

private:
  template <class P> void failOn(const P *Loc, const Twine &Msg) {
    fatal("corrupted .eh_frame: " + Msg + "\n>>> defined in " +
          IS->getObjMsg((const uint8_t *)Loc - IS->Data.data()));
  }

  uint8_t readByte();
  void skipBytes(size_t Count);
  StringRef readString();
  void skipLeb128();
  void skipAugP();

  InputSectionBase *IS;
  ArrayRef<uint8_t> D;
};
}

size_t elf::readEhRecordSize(InputSectionBase *S, size_t Off) {
  return EhReader(S, S->Data.slice(Off)).readEhRecordSize();
}

// .eh_frame section is a sequence of records. Each record starts with
// a 4 byte length field. This function reads the length.
size_t EhReader::readEhRecordSize() {
  if (D.size() < 4)
    failOn(D.data(), "CIE/FDE too small");

  // First 4 bytes of CIE/FDE is the size of the record.
  // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
  // but we do not support that format yet.
  uint64_t V = read32(D.data());
  if (V == UINT32_MAX)
    failOn(D.data(), "CIE/FDE too large");
  uint64_t Size = V + 4;
  if (Size > D.size())
    failOn(D.data(), "CIE/FDE ends past the end of the section");
  return Size;
}

// Read a byte and advance D by one byte.
uint8_t EhReader::readByte() {
  if (D.empty())
    failOn(D.data(), "unexpected end of CIE");
  uint8_t B = D.front();
  D = D.slice(1);
  return B;
}

void EhReader::skipBytes(size_t Count) {
  if (D.size() < Count)
    failOn(D.data(), "CIE is too small");
  D = D.slice(Count);
}

// Read a null-terminated string.
StringRef EhReader::readString() {
  const uint8_t *End = std::find(D.begin(), D.end(), '\0');
  if (End == D.end())
    failOn(D.data(), "corrupted CIE (failed to read string)");
  StringRef S = toStringRef(D.slice(0, End - D.begin()));
  D = D.slice(S.size() + 1);
  return S;
}

// Skip an integer encoded in the LEB128 format.
// Actual number is not of interest because only the runtime needs it.
// But we need to be at least able to skip it so that we can read
// the field that follows a LEB128 number.
void EhReader::skipLeb128() {
  const uint8_t *ErrPos = D.data();
  while (!D.empty()) {
    uint8_t Val = D.front();
    D = D.slice(1);
    if ((Val & 0x80) == 0)
      return;
  }
  failOn(ErrPos, "corrupted CIE (failed to read LEB128)");
}

static size_t getAugPSize(unsigned Enc) {
  switch (Enc & 0x0f) {
  case DW_EH_PE_absptr:
  case DW_EH_PE_signed:
    return Config->Wordsize;
  case DW_EH_PE_udata2:
  case DW_EH_PE_sdata2:
    return 2;
  case DW_EH_PE_udata4:
  case DW_EH_PE_sdata4:
    return 4;
  case DW_EH_PE_udata8:
  case DW_EH_PE_sdata8:
    return 8;
  }
  return 0;
}

void EhReader::skipAugP() {
  uint8_t Enc = readByte();
  if ((Enc & 0xf0) == DW_EH_PE_aligned)
    failOn(D.data() - 1, "DW_EH_PE_aligned encoding is not supported");
  size_t Size = getAugPSize(Enc);
  if (Size == 0)
    failOn(D.data() - 1, "unknown FDE encoding");
  if (Size >= D.size())
    failOn(D.data() - 1, "corrupted CIE");
  D = D.slice(Size);
}

uint8_t elf::getFdeEncoding(EhSectionPiece *P) {
  return EhReader(P->Sec, P->data()).getFdeEncoding();
}

uint8_t EhReader::getFdeEncoding() {
  skipBytes(8);
  int Version = readByte();
  if (Version != 1 && Version != 3)
    failOn(D.data() - 1,
           "FDE version 1 or 3 expected, but got " + Twine(Version));

  StringRef Aug = readString();

  // Skip code and data alignment factors.
  skipLeb128();
  skipLeb128();

  // Skip the return address register. In CIE version 1 this is a single
  // byte. In CIE version 3 this is an unsigned LEB128.
  if (Version == 1)
    readByte();
  else
    skipLeb128();

  // We only care about an 'R' value, but other records may precede an 'R'
  // record. Unfortunately records are not in TLV (type-length-value) format,
  // so we need to teach the linker how to skip records for each type.
  for (char C : Aug) {
    if (C == 'R')
      return readByte();
    if (C == 'z') {
      skipLeb128();
      continue;
    }
    if (C == 'P') {
      skipAugP();
      continue;
    }
    if (C == 'L') {
      readByte();
      continue;
    }
    failOn(Aug.data(), "unknown .eh_frame augmentation string: " + Aug);
  }
  return DW_EH_PE_absptr;
}
<|endoftext|>


<commit_before>/*
   For more information, please see: http://software.sci.utah.edu

   The MIT License

   Copyright (c) 2008 Scientific Computing and Imaging Institute,
   University of Utah.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/

/**
  \file    AbstrDebugOut.cpp
  \author  Jens Krueger
           SCI Institute
           University of Utah
  \date    January 2009
*/

#include <cstdarg>
#include <cstdio>
#include <cstring>
#include "AbstrDebugOut.h"

const char *AbstrDebugOut::ChannelToString(enum DebugChannel c) const
{
  switch(c) {
    case CHANNEL_NONE:    /* FALL THROUGH */
    case CHANNEL_FINAL:   return "";
    case CHANNEL_ERROR:   return "ERROR";
    case CHANNEL_WARNING: return "WARNING";
    case CHANNEL_MESSAGE: return "MESSAGE";
    case CHANNEL_OTHER:   return "OTHER";
  }
  return "";
}

bool AbstrDebugOut::Enabled(enum DebugChannel channel) const
{
  switch(channel) {
    case CHANNEL_NONE:  /* FALL THROUGH */
    case CHANNEL_FINAL: return true;
    case CHANNEL_ERROR: return m_bShowErrors;
    case CHANNEL_WARNING: return m_bShowWarnings;
    case CHANNEL_MESSAGE: return m_bShowMessages;
    case CHANNEL_OTHER: return m_bShowOther;
  }
  return true;
}

void AbstrDebugOut::Other(const char *source, const char* format, ...)
{
  if (!m_bShowOther) return;
  char buff[16384];

  va_list args;
  va_start(args, format);
#ifdef DETECTED_OS_WINDOWS
  _vsnprintf_s(buff, 16384, sizeof(buff), format, args);
#else
  vsnprintf(buff, sizeof(buff), format, args);
#endif
  va_end(args);

  this->printf(CHANNEL_OTHER, source, buff);
}

void AbstrDebugOut::Message(const char* source, const char* format, ...)
{
  if (!m_bShowMessages) return;
  char buff[16384];

  va_list args;
  va_start(args, format);
#ifdef DETECTED_OS_WINDOWS
  _vsnprintf_s(buff, 16384, sizeof(buff), format, args);
#else
  vsnprintf(buff, sizeof(buff), format, args);
#endif
  va_end(args);

  this->printf(CHANNEL_MESSAGE, source, buff);
}
void AbstrDebugOut::Warning(const char* source, const char* format, ...)
{
  if (!m_bShowWarnings) return;
  char buff[16384];

  va_list args;
  va_start(args, format);
#ifdef DETECTED_OS_WINDOWS
  _vsnprintf_s(buff, 16384, sizeof(buff), format, args);
#else
  vsnprintf(buff, sizeof(buff), format, args);
#endif
  va_end(args);

  this->printf(CHANNEL_WARNING, source, buff);
}
void AbstrDebugOut::Error(const char* source, const char* format, ...)
{
  if (!m_bShowErrors) return;
  char buff[16384];

  va_list args;
  va_start(args, format);
#ifdef DETECTED_OS_WINDOWS
  _vsnprintf_s(buff, 16384, sizeof(buff), format, args);
#else
  vsnprintf(buff, sizeof(buff), format, args);
#endif
  va_end(args);

  this->printf(CHANNEL_ERROR, source, buff);
}

void AbstrDebugOut::PrintErrorList() {
  printf( "Printing recorded errors:" );
  for (std::deque< std::string >::const_iterator i =
            m_strLists[CHANNEL_ERROR].begin();
       i < m_strLists[CHANNEL_ERROR].end(); ++i) {
    printf( i->c_str() );
  }
  printf( "end of recorded errors" );
}

void AbstrDebugOut::PrintWarningList() {
  printf( "Printing recorded errors:" );
  for (std::deque< std::string >::const_iterator i =
            m_strLists[CHANNEL_WARNING].begin();
       i < m_strLists[CHANNEL_WARNING].end(); ++i) {
    printf( i->c_str() );
  }
  printf( "end of recorded errors" );
}

void AbstrDebugOut::PrintMessageList() {
  printf( "Printing recorded errors:" );
  for (std::deque< std::string >::const_iterator i =
            m_strLists[CHANNEL_MESSAGE].begin();
       i < m_strLists[CHANNEL_MESSAGE].end(); ++i) {
    printf( i->c_str() );
  }
  printf( "end of recorded errors" );
}

void AbstrDebugOut::SetOutput(bool bShowErrors,
                              bool bShowWarnings,
                              bool bShowMessages,
                              bool bShowOther) {
  SetShowMessages(bShowMessages);
  SetShowWarnings(bShowWarnings);
  SetShowErrors(bShowErrors);
  SetShowOther(bShowOther);
}

void AbstrDebugOut::GetOutput(bool& bShowErrors,
                              bool& bShowWarnings,
                              bool& bShowMessages,
                              bool& bShowOther) const {

  bShowMessages = ShowMessages();
  bShowWarnings = ShowWarnings();
  bShowErrors   = ShowErrors();
  bShowOther    = ShowOther();
}

void AbstrDebugOut::SetShowMessages(bool bShowMessages) {
  m_bShowMessages = bShowMessages;
}

void AbstrDebugOut::SetShowWarnings(bool bShowWarnings) {
  m_bShowWarnings = bShowWarnings;
}

void AbstrDebugOut::SetShowErrors(bool bShowErrors) {
  m_bShowErrors = bShowErrors;
}

void AbstrDebugOut::SetShowOther(bool bShowOther) {
  m_bShowOther = bShowOther;
}

void AbstrDebugOut::ReplaceSpecialChars(char* buff, size_t iSize) const {
  std::string s = buff;

  size_t j=0;
  for (;(j = s.find( "%", j )) != std::string::npos;)
  {
    s.replace( j, 1, "%%" );
    j+=2;
  }

  size_t iLength = std::min(s.length(), iSize-1);
  std::memcpy(buff, s.c_str(), iLength);
  buff[iLength] = 0;
}
<commit_msg>Fix deque iteration.<commit_after>/*
   For more information, please see: http://software.sci.utah.edu

   The MIT License

   Copyright (c) 2008 Scientific Computing and Imaging Institute,
   University of Utah.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/

/**
  \file    AbstrDebugOut.cpp
  \author  Jens Krueger
           SCI Institute
           University of Utah
  \date    January 2009
*/

#include <cstdarg>
#include <cstdio>
#include <cstring>
#include "AbstrDebugOut.h"

const char *AbstrDebugOut::ChannelToString(enum DebugChannel c) const
{
  switch(c) {
    case CHANNEL_NONE:    /* FALL THROUGH */
    case CHANNEL_FINAL:   return "";
    case CHANNEL_ERROR:   return "ERROR";
    case CHANNEL_WARNING: return "WARNING";
    case CHANNEL_MESSAGE: return "MESSAGE";
    case CHANNEL_OTHER:   return "OTHER";
  }
  return "";
}

bool AbstrDebugOut::Enabled(enum DebugChannel channel) const
{
  switch(channel) {
    case CHANNEL_NONE:  /* FALL THROUGH */
    case CHANNEL_FINAL: return true;
    case CHANNEL_ERROR: return m_bShowErrors;
    case CHANNEL_WARNING: return m_bShowWarnings;
    case CHANNEL_MESSAGE: return m_bShowMessages;
    case CHANNEL_OTHER: return m_bShowOther;
  }
  return true;
}

void AbstrDebugOut::Other(const char *source, const char* format, ...)
{
  if (!m_bShowOther) return;
  char buff[16384];

  va_list args;
  va_start(args, format);
#ifdef DETECTED_OS_WINDOWS
  _vsnprintf_s(buff, 16384, sizeof(buff), format, args);
#else
  vsnprintf(buff, sizeof(buff), format, args);
#endif
  va_end(args);

  this->printf(CHANNEL_OTHER, source, buff);
}

void AbstrDebugOut::Message(const char* source, const char* format, ...)
{
  if (!m_bShowMessages) return;
  char buff[16384];

  va_list args;
  va_start(args, format);
#ifdef DETECTED_OS_WINDOWS
  _vsnprintf_s(buff, 16384, sizeof(buff), format, args);
#else
  vsnprintf(buff, sizeof(buff), format, args);
#endif
  va_end(args);

  this->printf(CHANNEL_MESSAGE, source, buff);
}
void AbstrDebugOut::Warning(const char* source, const char* format, ...)
{
  if (!m_bShowWarnings) return;
  char buff[16384];

  va_list args;
  va_start(args, format);
#ifdef DETECTED_OS_WINDOWS
  _vsnprintf_s(buff, 16384, sizeof(buff), format, args);
#else
  vsnprintf(buff, sizeof(buff), format, args);
#endif
  va_end(args);

  this->printf(CHANNEL_WARNING, source, buff);
}
void AbstrDebugOut::Error(const char* source, const char* format, ...)
{
  if (!m_bShowErrors) return;
  char buff[16384];

  va_list args;
  va_start(args, format);
#ifdef DETECTED_OS_WINDOWS
  _vsnprintf_s(buff, 16384, sizeof(buff), format, args);
#else
  vsnprintf(buff, sizeof(buff), format, args);
#endif
  va_end(args);

  this->printf(CHANNEL_ERROR, source, buff);
}

void AbstrDebugOut::PrintErrorList() {
  printf( "Printing recorded errors:" );
  for (std::deque<std::string>::const_iterator i =
         m_strLists[CHANNEL_ERROR].begin();
       i != m_strLists[CHANNEL_ERROR].end(); ++i) {
    printf( i->c_str() );
  }
  printf( "end of recorded errors" );
}

void AbstrDebugOut::PrintWarningList() {
  printf( "Printing recorded errors:" );
  for (std::deque<std::string>::const_iterator i =
         m_strLists[CHANNEL_WARNING].begin();
       i != m_strLists[CHANNEL_WARNING].end(); ++i) {
    printf( i->c_str() );
  }
  printf( "end of recorded errors" );
}

void AbstrDebugOut::PrintMessageList() {
  printf( "Printing recorded errors:" );
  for (std::deque<std::string>::const_iterator i =
         m_strLists[CHANNEL_MESSAGE].begin();
       i != m_strLists[CHANNEL_MESSAGE].end(); ++i) {
    printf( i->c_str() );
  }
  printf( "end of recorded errors" );
}

void AbstrDebugOut::SetOutput(bool bShowErrors,
                              bool bShowWarnings,
                              bool bShowMessages,
                              bool bShowOther) {
  SetShowMessages(bShowMessages);
  SetShowWarnings(bShowWarnings);
  SetShowErrors(bShowErrors);
  SetShowOther(bShowOther);
}

void AbstrDebugOut::GetOutput(bool& bShowErrors,
                              bool& bShowWarnings,
                              bool& bShowMessages,
                              bool& bShowOther) const {

  bShowMessages = ShowMessages();
  bShowWarnings = ShowWarnings();
  bShowErrors   = ShowErrors();
  bShowOther    = ShowOther();
}

void AbstrDebugOut::SetShowMessages(bool bShowMessages) {
  m_bShowMessages = bShowMessages;
}

void AbstrDebugOut::SetShowWarnings(bool bShowWarnings) {
  m_bShowWarnings = bShowWarnings;
}

void AbstrDebugOut::SetShowErrors(bool bShowErrors) {
  m_bShowErrors = bShowErrors;
}

void AbstrDebugOut::SetShowOther(bool bShowOther) {
  m_bShowOther = bShowOther;
}

void AbstrDebugOut::ReplaceSpecialChars(char* buff, size_t iSize) const {
  std::string s = buff;

  size_t j=0;
  for (;(j = s.find( "%", j )) != std::string::npos;)
  {
    s.replace( j, 1, "%%" );
    j+=2;
  }

  size_t iLength = std::min(s.length(), iSize-1);
  std::memcpy(buff, s.c_str(), iLength);
  buff[iLength] = 0;
}
<|endoftext|>


<commit_before>/*
    Copyright (C) 2001 by W.C.A. Wijngaards

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#define SYSDEF_ALLOCA
#include "cssysdef.h"
#include "isospr.h"
#include "ivideo/graph3d.h"
#include "csgeom/math2d.h"
#include "csgeom/polyclip.h"
#include "ivideo/material.h"
#include "ivideo/texture.h"
#include "ivideo/txtmgr.h"

IMPLEMENT_IBASE (csIsoSprite)
  IMPLEMENTS_INTERFACE (iIsoSprite)
IMPLEMENT_IBASE_END

csIsoSprite::csIsoSprite (iBase *iParent)
{
  CONSTRUCT_IBASE (iParent);
  position.Set(0,0,0);
  material = NULL;
  mixmode = CS_FX_COPY;
  grid = NULL;
  gridcall = NULL;
  gridcalldata = NULL;
}

csIsoSprite::~csIsoSprite ()
{
}

int csIsoSprite::GetNumVertices() const
{
  return poly.GetNumVertices();
}

void csIsoSprite::AddVertex(const csVector3& coord, float u, float v)
{
  poly.AddVertex(coord);
  uv.AddVertex(u,v);
  colors.AddVertex(1.,1.,1.);
}

void csIsoSprite::SetPosition(const csVector3& newpos)
{
  /// manage movement of the sprite, oldpos, newpos
  csVector3 oldpos = position;
  position = newpos; // already set, so it can be overridden
  if(grid) grid->MoveSprite(this, oldpos, newpos);
}

void csIsoSprite::MovePosition(const csVector3& delta)
{
  SetPosition(position + delta);
}

#define INTERPOLATE1(component) \
  g3dpoly->vertices [i].##component## = inpoly [vt].##component## + \
    t * (inpoly [vt2].##component## - inpoly [vt].##component##);

#define INTERPOLATE(component) \
{ \
  float v1 = inpoly [edge_from [0]].##component## + \
    t1 * (inpoly [edge_to [0]].##component## - inpoly [edge_from [0]].##component##); \
  float v2 = inpoly [edge_from [1]].##component## + \
    t2 * (inpoly [edge_to [1]].##component## - inpoly [edge_from [1]].##component##); \
  g3dpoly->vertices [i].##component## = v1 + t * (v2 - v1); \
}

static void PreparePolygonFX2 (G3DPolygonDPFX* g3dpoly,
  csVector2* clipped_verts, int num_vertices, csVertexStatus* clipped_vtstats,
  int orig_num_vertices, bool gouraud)
{
  // first we copy the first texture coordinates to a local buffer
  // to avoid that they are overwritten when interpolating.
  ALLOC_STACK_ARRAY (inpoly, G3DTexturedVertex, orig_num_vertices);
  int i;
  for (i = 0; i < orig_num_vertices; i++)
    inpoly[i] = g3dpoly->vertices[i];

  int vt, vt2;
  float t;
  for (i = 0; i < num_vertices; i++)
  {
    g3dpoly->vertices [i].sx = clipped_verts [i].x;
    g3dpoly->vertices [i].sy = clipped_verts [i].y;
    switch (clipped_vtstats[i].Type)
    {
      case CS_VERTEX_ORIGINAL:
        vt = clipped_vtstats[i].Vertex;
        g3dpoly->vertices [i].z = inpoly [vt].z;
        g3dpoly->vertices [i].u = inpoly [vt].u;
        g3dpoly->vertices [i].v = inpoly [vt].v;
        if (gouraud)
        {
          g3dpoly->vertices [i].r = inpoly [vt].r;
          g3dpoly->vertices [i].g = inpoly [vt].g;
          g3dpoly->vertices [i].b = inpoly [vt].b;
        }
        break;
      case CS_VERTEX_ONEDGE:
        vt = clipped_vtstats[i].Vertex;
        vt2 = vt + 1; if (vt2 >= orig_num_vertices) vt2 = 0;
        t = clipped_vtstats [i].Pos;
        INTERPOLATE1 (z);
        INTERPOLATE1 (u);
        INTERPOLATE1 (v);
        if (gouraud)
        {
          INTERPOLATE1 (r);
          INTERPOLATE1 (g);
          INTERPOLATE1 (b);
        }
        break;
      case CS_VERTEX_INSIDE:
        float x = clipped_verts [i].x;
        float y = clipped_verts [i].y;
        int edge_from [2], edge_to [2];
        int edge = 0;
        int j, j1;
        j1 = orig_num_vertices - 1;
        for (j = 0; j < orig_num_vertices; j++)
        {
          if ((y >= inpoly [j].sy && y <= inpoly [j1].sy) ||
              (y <= inpoly [j].sy && y >= inpoly [j1].sy))
          {
            edge_from [edge] = j;
            edge_to [edge] = j1;
            edge++;
            if (edge >= 2) break;
          }
          j1 = j;
        }
        if (edge == 1)
        {
          // Safety if we only found one edge.
          edge_from [1] = edge_from [0];
          edge_to [1] = edge_to [0];
        }
        G3DTexturedVertex& A = inpoly [edge_from [0]];
        G3DTexturedVertex& B = inpoly [edge_to [0]];
        G3DTexturedVertex& C = inpoly [edge_from [1]];
        G3DTexturedVertex& D = inpoly [edge_to [1]];
        float t1 = (y - A.sy) / (B.sy - A.sy);
        float t2 = (y - C.sy) / (D.sy - C.sy);
        float x1 = A.sx + t1 * (B.sx - A.sx);
        float x2 = C.sx + t2 * (D.sx - C.sx);
        t = (x - x1) / (x2 - x1);
        INTERPOLATE (z);
        INTERPOLATE (u);
        INTERPOLATE (v);
        if (gouraud)
        {
          INTERPOLATE (r);
          INTERPOLATE (g);
          INTERPOLATE (b);
        }
        break;
    }
  }
}

#undef INTERPOLATE
#undef INTERPOLATE1

void csIsoSprite::Draw(iIsoRenderView *rview)
{
  //printf("isosprite::Draw()\n");
  if (!material)
  {
    printf ("INTERNAL ERROR: IsoSpr used without valid material handle!\n");
    return;// false;
  }

  iGraphics3D* g3d = rview->GetG3D ();
  iIsoView* view = rview->GetView ();

  // Prepare for rendering.
  if((mixmode & CS_FX_MASK_MIXMODE) == CS_FX_COPY)
    g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_USE);
  else g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_TEST);
  //g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_NONE);

  //material->Visit ();

  g3dpolyfx.num = poly.GetNumVertices ();
  g3dpolyfx.mat_handle = material;
  /// guesstimate of fov (angle) of view. 1/fov.
  g3dpolyfx.inv_aspect = 1./180.;
  g3dpolyfx.mat_handle->GetTexture ()->GetMeanColor (g3dpolyfx.flat_color_r,
    g3dpolyfx.flat_color_g, g3dpolyfx.flat_color_b);

  ALLOC_STACK_ARRAY (poly2d, csVector2, g3dpolyfx.num);
  csVector2 clipped_poly2d[MAX_OUTPUT_VERTICES];
  csVertexStatus clipped_vtstats[MAX_OUTPUT_VERTICES];

  csVector3 screenpos;
  int i;
  float zlowerbound = rview->GetMinZ();

  //view->W2S(position, screenpos);
  //printf("position %g,%g,%g to %g,%g,%g\n",
    //position.x, position.y, position.z, screenpos.x, screenpos.y, screenpos.z);
  //g3d->DrawLine(screenpos, screenpos+csVector3(5,5,0), g3dpolyfx.inv_aspect,
    //g3d->GetTextureManager()->FindRGB(255,255,255));

  for (i = 0; i < g3dpolyfx.num; i++)
  {
    view->W2S(position + poly[i], screenpos);
    //if(screenpos.z == 0.0) g3dpolyfx.vertices [i].z = 0.5; else
    //if(screenpos.z < 0.0)
         //g3dpolyfx.vertices [i].z = 1.0-.5/(-screenpos.z+1.);
    //else g3dpolyfx.vertices [i].z = .5/(screenpos.z+1.);
    g3dpolyfx.vertices [i].z = 1./(screenpos.z-zlowerbound);
    g3dpolyfx.vertices [i].sx = poly2d [i].x = screenpos.x;
    g3dpolyfx.vertices [i].sy = poly2d [i].y = screenpos.y;
    g3dpolyfx.vertices [i].r = colors [i].x;
    g3dpolyfx.vertices [i].g = colors [i].y;
    g3dpolyfx.vertices [i].b = colors [i].z;
    g3dpolyfx.vertices [i].u = uv [i].x;
    g3dpolyfx.vertices [i].v = uv [i].y;
  }

  int num_clipped_verts;
  UByte clip_result = rview->GetClipper()->Clip (poly2d, g3dpolyfx.num,
    clipped_poly2d, num_clipped_verts, clipped_vtstats);
  if (clip_result == CS_CLIP_OUTSIDE) return;// false;
  g3dpolyfx.num = num_clipped_verts;

  if (clip_result != CS_CLIP_INSIDE)
    PreparePolygonFX2 (&g3dpolyfx, clipped_poly2d, num_clipped_verts,
        clipped_vtstats, poly.GetNumVertices(), true);

  //rview->CalculateFogPolygon (g3dpolyfx);
  g3d->StartPolygonFX (g3dpolyfx.mat_handle, mixmode | CS_FX_GOURAUD);
  g3d->DrawPolygonFX (g3dpolyfx);
  g3d->FinishPolygonFX ();

  return;// true;
}

void csIsoSprite::SetGrid(iIsoGrid *grid)
{
  if(csIsoSprite::grid != grid)
  {
    csIsoSprite::grid = grid;
    if(gridcall) gridcall(this, gridcalldata);
  }
}

void csIsoSprite::SetAllColors(const csColor& color)
{
  for(int i=0; i<poly.GetNumVertices(); i++)
  {
    colors[i].Set(color.red, color.green, color.blue);
  }
}

void csIsoSprite::AddToVertexColor(int i, const csColor& color)
{
  colors[i].x += color.red; if(colors[i].x>1.0) colors[i].x=1.0;
  colors[i].y += color.green; if(colors[i].y>1.0) colors[i].y=1.0;
  colors[i].z += color.blue; if(colors[i].z>1.0) colors[i].z=1.0;
}

<commit_msg>Hope to fix isospr weirdness by clamping colors here too.<commit_after>/*
    Copyright (C) 2001 by W.C.A. Wijngaards

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#define SYSDEF_ALLOCA
#include "cssysdef.h"
#include "isospr.h"
#include "ivideo/graph3d.h"
#include "csgeom/math2d.h"
#include "csgeom/polyclip.h"
#include "ivideo/material.h"
#include "ivideo/texture.h"
#include "ivideo/txtmgr.h"

IMPLEMENT_IBASE (csIsoSprite)
  IMPLEMENTS_INTERFACE (iIsoSprite)
IMPLEMENT_IBASE_END

csIsoSprite::csIsoSprite (iBase *iParent)
{
  CONSTRUCT_IBASE (iParent);
  position.Set(0,0,0);
  material = NULL;
  mixmode = CS_FX_COPY;
  grid = NULL;
  gridcall = NULL;
  gridcalldata = NULL;
}

csIsoSprite::~csIsoSprite ()
{
}

int csIsoSprite::GetNumVertices() const
{
  return poly.GetNumVertices();
}

void csIsoSprite::AddVertex(const csVector3& coord, float u, float v)
{
  poly.AddVertex(coord);
  uv.AddVertex(u,v);
  colors.AddVertex(1.,1.,1.);
}

void csIsoSprite::SetPosition(const csVector3& newpos)
{
  /// manage movement of the sprite, oldpos, newpos
  csVector3 oldpos = position;
  position = newpos; // already set, so it can be overridden
  if(grid) grid->MoveSprite(this, oldpos, newpos);
}

void csIsoSprite::MovePosition(const csVector3& delta)
{
  SetPosition(position + delta);
}

#define INTERPOLATE1(component) \
  g3dpoly->vertices [i].##component## = inpoly [vt].##component## + \
    t * (inpoly [vt2].##component## - inpoly [vt].##component##);

#define INTERPOLATE(component) \
{ \
  float v1 = inpoly [edge_from [0]].##component## + \
    t1 * (inpoly [edge_to [0]].##component## - inpoly [edge_from [0]].##component##); \
  float v2 = inpoly [edge_from [1]].##component## + \
    t2 * (inpoly [edge_to [1]].##component## - inpoly [edge_from [1]].##component##); \
  g3dpoly->vertices [i].##component## = v1 + t * (v2 - v1); \
}

static void PreparePolygonFX2 (G3DPolygonDPFX* g3dpoly,
  csVector2* clipped_verts, int num_vertices, csVertexStatus* clipped_vtstats,
  int orig_num_vertices, bool gouraud)
{
  // first we copy the first texture coordinates to a local buffer
  // to avoid that they are overwritten when interpolating.
  ALLOC_STACK_ARRAY (inpoly, G3DTexturedVertex, orig_num_vertices);
  int i;
  for (i = 0; i < orig_num_vertices; i++)
    inpoly[i] = g3dpoly->vertices[i];

  int vt, vt2;
  float t;
  for (i = 0; i < num_vertices; i++)
  {
    g3dpoly->vertices [i].sx = clipped_verts [i].x;
    g3dpoly->vertices [i].sy = clipped_verts [i].y;
    switch (clipped_vtstats[i].Type)
    {
      case CS_VERTEX_ORIGINAL:
        vt = clipped_vtstats[i].Vertex;
        g3dpoly->vertices [i].z = inpoly [vt].z;
        g3dpoly->vertices [i].u = inpoly [vt].u;
        g3dpoly->vertices [i].v = inpoly [vt].v;
        if (gouraud)
        {
          g3dpoly->vertices [i].r = inpoly [vt].r;
          g3dpoly->vertices [i].g = inpoly [vt].g;
          g3dpoly->vertices [i].b = inpoly [vt].b;
        }
        break;
      case CS_VERTEX_ONEDGE:
        vt = clipped_vtstats[i].Vertex;
        vt2 = vt + 1; if (vt2 >= orig_num_vertices) vt2 = 0;
        t = clipped_vtstats [i].Pos;
        INTERPOLATE1 (z);
        INTERPOLATE1 (u);
        INTERPOLATE1 (v);
        if (gouraud)
        {
          INTERPOLATE1 (r);
          INTERPOLATE1 (g);
          INTERPOLATE1 (b);
        }
        break;
      case CS_VERTEX_INSIDE:
        float x = clipped_verts [i].x;
        float y = clipped_verts [i].y;
        int edge_from [2], edge_to [2];
        int edge = 0;
        int j, j1;
        j1 = orig_num_vertices - 1;
        for (j = 0; j < orig_num_vertices; j++)
        {
          if ((y >= inpoly [j].sy && y <= inpoly [j1].sy) ||
              (y <= inpoly [j].sy && y >= inpoly [j1].sy))
          {
            edge_from [edge] = j;
            edge_to [edge] = j1;
            edge++;
            if (edge >= 2) break;
          }
          j1 = j;
        }
        if (edge == 1)
        {
          // Safety if we only found one edge.
          edge_from [1] = edge_from [0];
          edge_to [1] = edge_to [0];
        }
        G3DTexturedVertex& A = inpoly [edge_from [0]];
        G3DTexturedVertex& B = inpoly [edge_to [0]];
        G3DTexturedVertex& C = inpoly [edge_from [1]];
        G3DTexturedVertex& D = inpoly [edge_to [1]];
        float t1 = (y - A.sy) / (B.sy - A.sy);
        float t2 = (y - C.sy) / (D.sy - C.sy);
        float x1 = A.sx + t1 * (B.sx - A.sx);
        float x2 = C.sx + t2 * (D.sx - C.sx);
        t = (x - x1) / (x2 - x1);
        INTERPOLATE (z);
        INTERPOLATE (u);
        INTERPOLATE (v);
        if (gouraud)
        {
          INTERPOLATE (r);
          INTERPOLATE (g);
          INTERPOLATE (b);
        }
        break;
    }
  }
}

#undef INTERPOLATE
#undef INTERPOLATE1

void csIsoSprite::Draw(iIsoRenderView *rview)
{
  //printf("isosprite::Draw()\n");
  if (!material)
  {
    printf ("INTERNAL ERROR: IsoSpr used without valid material handle!\n");
    return;// false;
  }

  iGraphics3D* g3d = rview->GetG3D ();
  iIsoView* view = rview->GetView ();

  // Prepare for rendering.
  if((mixmode & CS_FX_MASK_MIXMODE) == CS_FX_COPY)
    g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_USE);
  else g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_TEST);
  //g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_NONE);

  //material->Visit ();

  g3dpolyfx.num = poly.GetNumVertices ();
  g3dpolyfx.mat_handle = material;
  /// guesstimate of fov (angle) of view. 1/fov.
  g3dpolyfx.inv_aspect = 1./180.;
  g3dpolyfx.mat_handle->GetTexture ()->GetMeanColor (g3dpolyfx.flat_color_r,
    g3dpolyfx.flat_color_g, g3dpolyfx.flat_color_b);

  ALLOC_STACK_ARRAY (poly2d, csVector2, g3dpolyfx.num);
  csVector2 clipped_poly2d[MAX_OUTPUT_VERTICES];
  csVertexStatus clipped_vtstats[MAX_OUTPUT_VERTICES];

  csVector3 screenpos;
  int i;
  float zlowerbound = rview->GetMinZ();

  //view->W2S(position, screenpos);
  //printf("position %g,%g,%g to %g,%g,%g\n",
    //position.x, position.y, position.z, screenpos.x, screenpos.y, screenpos.z);
  //g3d->DrawLine(screenpos, screenpos+csVector3(5,5,0), g3dpolyfx.inv_aspect,
    //g3d->GetTextureManager()->FindRGB(255,255,255));

  for (i = 0; i < g3dpolyfx.num; i++)
  {
    view->W2S(position + poly[i], screenpos);
    //if(screenpos.z == 0.0) g3dpolyfx.vertices [i].z = 0.5; else
    //if(screenpos.z < 0.0)
         //g3dpolyfx.vertices [i].z = 1.0-.5/(-screenpos.z+1.);
    //else g3dpolyfx.vertices [i].z = .5/(screenpos.z+1.);
    g3dpolyfx.vertices [i].z = 1./(screenpos.z-zlowerbound);
    g3dpolyfx.vertices [i].sx = poly2d [i].x = screenpos.x;
    g3dpolyfx.vertices [i].sy = poly2d [i].y = screenpos.y;
    g3dpolyfx.vertices [i].r = colors [i].x;
    g3dpolyfx.vertices [i].g = colors [i].y;
    g3dpolyfx.vertices [i].b = colors [i].z;
    g3dpolyfx.vertices [i].u = uv [i].x;
    g3dpolyfx.vertices [i].v = uv [i].y;
  }

  int num_clipped_verts;
  UByte clip_result = rview->GetClipper()->Clip (poly2d, g3dpolyfx.num,
    clipped_poly2d, num_clipped_verts, clipped_vtstats);
  if (clip_result == CS_CLIP_OUTSIDE) return;// false;
  g3dpolyfx.num = num_clipped_verts;

  if (clip_result != CS_CLIP_INSIDE)
    PreparePolygonFX2 (&g3dpolyfx, clipped_poly2d, num_clipped_verts,
        clipped_vtstats, poly.GetNumVertices(), true);

  //rview->CalculateFogPolygon (g3dpolyfx);
  g3d->StartPolygonFX (g3dpolyfx.mat_handle, mixmode | CS_FX_GOURAUD);
  g3d->DrawPolygonFX (g3dpolyfx);
  g3d->FinishPolygonFX ();

  return;// true;
}

void csIsoSprite::SetGrid(iIsoGrid *grid)
{
  if(csIsoSprite::grid != grid)
  {
    csIsoSprite::grid = grid;
    if(gridcall) gridcall(this, gridcalldata);
  }
}

void csIsoSprite::SetAllColors(const csColor& color)
{
  for(int i=0; i<poly.GetNumVertices(); i++)
  {
    colors[i].Set(color.red, color.green, color.blue);
  }
}

void csIsoSprite::AddToVertexColor(int i, const csColor& color)
{
  colors[i].x += color.red;
  if(colors[i].x>1.0f) colors[i].x=1.0f;
  else if(colors[i].x < 0.0f) colors[i].x = 0.0f;
  colors[i].y += color.green;
  if(colors[i].y>1.0f) colors[i].y=1.0f;
  else if(colors[i].y < 0.0f) colors[i].y = 0.0f;
  colors[i].z += color.blue;
  if(colors[i].z>1.0f) colors[i].z=1.0f;
  else if(colors[i].z < 0.0f) colors[i].z = 0.0f;
}

<|endoftext|>


<commit_before>/*

Copyright (c) 2007, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/storage.hpp"
#include <deque>
#include "libtorrent/disk_io_thread.hpp"

#ifdef TORRENT_DISK_STATS

#include "libtorrent/time.hpp"

#endif

namespace libtorrent
{

	disk_io_thread::disk_io_thread(int block_size)
		: m_abort(false)
		, m_queue_buffer_size(0)
		, m_pool(block_size)
#ifndef NDEBUG
		, m_block_size(block_size)
#endif
		, m_disk_io_thread(boost::ref(*this))
	{
#ifdef TORRENT_STATS
		m_allocations = 0;
#endif
#ifdef TORRENT_DISK_STATS
		m_log.open("disk_io_thread.log", std::ios::trunc);
#endif
	}

	disk_io_thread::~disk_io_thread()
	{
		boost::mutex::scoped_lock l(m_mutex);
		m_abort = true;
		m_signal.notify_all();
		l.unlock();

		m_disk_io_thread.join();
	}

#ifndef NDEBUG
	disk_io_job disk_io_thread::find_job(boost::intrusive_ptr<piece_manager> s
		, int action, int piece) const
	{
		boost::mutex::scoped_lock l(m_mutex);
		for (std::deque<disk_io_job>::const_iterator i = m_jobs.begin();
			i != m_jobs.end(); ++i)
		{
			if (i->storage != s)
				continue;
			if ((i->action == action || action == -1) && i->piece == piece)
				return *i;
		}
		if ((m_current.action == action || action == -1)
			&& m_current.piece == piece)
			return m_current;

		disk_io_job ret;
		ret.action = (disk_io_job::action_t)-1;
		ret.piece = -1;
		return ret;
	}

#endif

	// aborts read operations
	void disk_io_thread::stop(boost::intrusive_ptr<piece_manager> s)
	{
		boost::mutex::scoped_lock l(m_mutex);
		// read jobs are aborted, write and move jobs are syncronized
		for (std::deque<disk_io_job>::iterator i = m_jobs.begin();
			i != m_jobs.end();)
		{
			if (i->storage != s)
			{
				++i;
				continue;
			}
			if (i->action == disk_io_job::read)
			{
				i->callback(-1, *i);
				m_jobs.erase(i++);
				continue;
			}
			++i;
		}
		m_signal.notify_all();
	}

	bool range_overlap(int start1, int length1, int start2, int length2)
	{
		return (start1 <= start2 && start1 + length1 > start2)
			|| (start2 <= start1 && start2 + length2 > start1);
	}

	namespace
	{
		// The semantic of this operator is:
		// shouls lhs come before rhs in the job queue
		bool operator<(disk_io_job const& lhs, disk_io_job const& rhs)
		{
			// NOTE: comparison inverted to make higher priority
			// skip _in_front_of_ lower priority
			if (lhs.priority > rhs.priority) return true;
			if (lhs.priority < rhs.priority) return false;

			if (lhs.storage.get() < rhs.storage.get()) return true;
			if (lhs.storage.get() > rhs.storage.get()) return false;
			if (lhs.piece < rhs.piece) return true;
			if (lhs.piece > rhs.piece) return false;
			if (lhs.offset < rhs.offset) return true;
//			if (lhs.offset > rhs.offset) return false;
			return false;
		}
	}

	void disk_io_thread::add_job(disk_io_job const& j
		, boost::function<void(int, disk_io_job const&)> const& f)
	{
		TORRENT_ASSERT(!j.callback);
		TORRENT_ASSERT(j.storage);
		boost::mutex::scoped_lock l(m_mutex);

		std::deque<disk_io_job>::reverse_iterator i = m_jobs.rbegin();
		if (j.action == disk_io_job::read)
		{
			// when we're reading, we may not skip
			// ahead of any write operation that overlaps
			// the region we're reading
			for (; i != m_jobs.rend(); i++)
			{
				// if *i should come before j, stop
				// and insert j before i
				if (*i < j) break;
				// if we come across a write operation that
				// overlaps the region we're reading, we need
				// to stop
				if (i->action == disk_io_job::write
					&& i->storage == j.storage
					&& i->piece == j.piece
					&& range_overlap(i->offset, i->buffer_size
						, j.offset, j.buffer_size))
					break;
			}
		}
		else if (j.action == disk_io_job::write)
		{
			for (; i != m_jobs.rend(); ++i)
			{
				if (*i < j)
				{
					if (i != m_jobs.rbegin()
						&& i.base()->storage.get() != j.storage.get())
						i = m_jobs.rbegin();
					break;
				}
			}
		}

		// if we are placed in front of all other jobs, put it on the back of
		// the queue, to sweep the disk in the same direction, and to avoid
		// starvation. The exception is if the priority is higher than the
		// job at the front of the queue
		if (i == m_jobs.rend() && (m_jobs.empty() || j.priority <= m_jobs.back().priority))
			i = m_jobs.rbegin();

		std::deque<disk_io_job>::iterator k = m_jobs.insert(i.base(), j);
		k->callback.swap(const_cast<boost::function<void(int, disk_io_job const&)>&>(f));
		if (j.action == disk_io_job::write)
			m_queue_buffer_size += j.buffer_size;
		TORRENT_ASSERT(j.storage.get());
		m_signal.notify_all();
	}

	char* disk_io_thread::allocate_buffer()
	{
		boost::mutex::scoped_lock l(m_mutex);
#ifdef TORRENT_STATS
		++m_allocations;
#endif
		return (char*)m_pool.ordered_malloc();
	}

	void disk_io_thread::free_buffer(char* buf)
	{
		boost::mutex::scoped_lock l(m_mutex);
#ifdef TORRENT_STATS
		--m_allocations;
#endif
		m_pool.ordered_free(buf);
	}

	void disk_io_thread::operator()()
	{
		for (;;)
		{
#ifdef TORRENT_DISK_STATS
			m_log << log_time() << " idle" << std::endl;
#endif
			boost::mutex::scoped_lock l(m_mutex);
#ifndef NDEBUG
			m_current.action = (disk_io_job::action_t)-1;
			m_current.piece = -1;
#endif
			while (m_jobs.empty() && !m_abort)
				m_signal.wait(l);
			if (m_abort && m_jobs.empty()) return;

			boost::function<void(int, disk_io_job const&)> handler;
			handler.swap(m_jobs.front().callback);
#ifndef NDEBUG
			m_current = m_jobs.front();
#endif
			disk_io_job j = m_jobs.front();
			m_jobs.pop_front();
			m_queue_buffer_size -= j.buffer_size;
			l.unlock();

			int ret = 0;

			bool free_buffer = true;
			try
			{
				TORRENT_ASSERT(j.storage);
#ifdef TORRENT_DISK_STATS
				ptime start = time_now();
#endif
//				std::cerr << "DISK THREAD: executing job: " << j.action << std::endl;
				switch (j.action)
				{
					case disk_io_job::read:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " read " << j.buffer_size << std::endl;
#endif
						free_buffer = false;
						if (j.buffer == 0)
						{
							l.lock();
							j.buffer = (char*)m_pool.ordered_malloc();
#ifdef TORRENT_STATS
							++m_allocations;
#endif
							l.unlock();
							TORRENT_ASSERT(j.buffer_size <= m_block_size);
							if (j.buffer == 0)
							{
								ret = -1;
								j.str = "out of memory";
								break;
							}
						}
						ret = j.storage->read_impl(j.buffer, j.piece, j.offset
							, j.buffer_size);

						// simulates slow drives
						// usleep(300);
						break;
					case disk_io_job::write:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " write " << j.buffer_size << std::endl;
#endif
						TORRENT_ASSERT(j.buffer);
						TORRENT_ASSERT(j.buffer_size <= m_block_size);
						j.storage->write_impl(j.buffer, j.piece, j.offset
							, j.buffer_size);

						// simulates a slow drive
						// usleep(300);
						break;
					case disk_io_job::hash:
						{
#ifdef TORRENT_DISK_STATS
							m_log << log_time() << " hash" << std::endl;
#endif
							sha1_hash h = j.storage->hash_for_piece_impl(j.piece);
							j.str.resize(20);
							std::memcpy(&j.str[0], &h[0], 20);
						}
						break;
					case disk_io_job::move_storage:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " move" << std::endl;
#endif
						ret = j.storage->move_storage_impl(j.str) ? 1 : 0;
						j.str = j.storage->save_path().string();
						break;
					case disk_io_job::release_files:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " release" << std::endl;
#endif
						j.storage->release_files_impl();
						break;
					case disk_io_job::delete_files:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " delete" << std::endl;
#endif
						j.storage->delete_files_impl();
						break;
				}
			}
			catch (std::exception& e)
			{
//				std::cerr << "DISK THREAD: exception: " << e.what() << std::endl;
				j.str = e.what();
				ret = -1;
			}

//			if (!handler) std::cerr << "DISK THREAD: no callback specified" << std::endl;
//			else std::cerr << "DISK THREAD: invoking callback" << std::endl;
			try { if (handler) handler(ret, j); }
			catch (std::exception&) {}

			if (j.buffer && free_buffer)
			{
				l.lock();
				m_pool.ordered_free(j.buffer);
#ifdef TORRENT_STATS
				--m_allocations;
#endif
			}
		}
	}
}

<commit_msg>fixed debug issue with disk io thread<commit_after>/*

Copyright (c) 2007, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/storage.hpp"
#include <deque>
#include "libtorrent/disk_io_thread.hpp"

#ifdef TORRENT_DISK_STATS

#include "libtorrent/time.hpp"

#endif

namespace libtorrent
{

	disk_io_thread::disk_io_thread(int block_size)
		: m_abort(false)
		, m_queue_buffer_size(0)
		, m_pool(block_size)
#ifndef NDEBUG
		, m_block_size(block_size)
#endif
		, m_disk_io_thread(boost::ref(*this))
	{
#ifdef TORRENT_STATS
		m_allocations = 0;
#endif
#ifdef TORRENT_DISK_STATS
		m_log.open("disk_io_thread.log", std::ios::trunc);
#endif
	}

	disk_io_thread::~disk_io_thread()
	{
		boost::mutex::scoped_lock l(m_mutex);
		m_abort = true;
		m_signal.notify_all();
		l.unlock();

		m_disk_io_thread.join();
	}

#ifndef NDEBUG
	disk_io_job disk_io_thread::find_job(boost::intrusive_ptr<piece_manager> s
		, int action, int piece) const
	{
		boost::mutex::scoped_lock l(m_mutex);
		for (std::deque<disk_io_job>::const_iterator i = m_jobs.begin();
			i != m_jobs.end(); ++i)
		{
			if (i->storage != s)
				continue;
			if ((i->action == action || action == -1) && i->piece == piece)
				return *i;
		}
		if ((m_current.action == action || action == -1)
			&& m_current.piece == piece)
			return m_current;

		disk_io_job ret;
		ret.action = (disk_io_job::action_t)-1;
		ret.piece = -1;
		return ret;
	}

#endif

	// aborts read operations
	void disk_io_thread::stop(boost::intrusive_ptr<piece_manager> s)
	{
		boost::mutex::scoped_lock l(m_mutex);
		// read jobs are aborted, write and move jobs are syncronized
		for (std::deque<disk_io_job>::iterator i = m_jobs.begin();
			i != m_jobs.end();)
		{
			if (i->storage != s)
			{
				++i;
				continue;
			}
			if (i->action == disk_io_job::read)
			{
				i->callback(-1, *i);
				m_jobs.erase(i++);
				continue;
			}
			++i;
		}
		m_signal.notify_all();
	}

	bool range_overlap(int start1, int length1, int start2, int length2)
	{
		return (start1 <= start2 && start1 + length1 > start2)
			|| (start2 <= start1 && start2 + length2 > start1);
	}

	namespace
	{
		// The semantic of this operator is:
		// shouls lhs come before rhs in the job queue
		bool operator<(disk_io_job const& lhs, disk_io_job const& rhs)
		{
			// NOTE: comparison inverted to make higher priority
			// skip _in_front_of_ lower priority
			if (lhs.priority > rhs.priority) return true;
			if (lhs.priority < rhs.priority) return false;

			if (lhs.storage.get() < rhs.storage.get()) return true;
			if (lhs.storage.get() > rhs.storage.get()) return false;
			if (lhs.piece < rhs.piece) return true;
			if (lhs.piece > rhs.piece) return false;
			if (lhs.offset < rhs.offset) return true;
//			if (lhs.offset > rhs.offset) return false;
			return false;
		}
	}

	void disk_io_thread::add_job(disk_io_job const& j
		, boost::function<void(int, disk_io_job const&)> const& f)
	{
		TORRENT_ASSERT(!j.callback);
		TORRENT_ASSERT(j.storage);
		boost::mutex::scoped_lock l(m_mutex);

		std::deque<disk_io_job>::reverse_iterator i = m_jobs.rbegin();
		if (j.action == disk_io_job::read)
		{
			// when we're reading, we may not skip
			// ahead of any write operation that overlaps
			// the region we're reading
			for (; i != m_jobs.rend(); i++)
			{
				// if *i should come before j, stop
				// and insert j before i
				if (*i < j) break;
				// if we come across a write operation that
				// overlaps the region we're reading, we need
				// to stop
				if (i->action == disk_io_job::write
					&& i->storage == j.storage
					&& i->piece == j.piece
					&& range_overlap(i->offset, i->buffer_size
						, j.offset, j.buffer_size))
					break;
			}
		}
		else if (j.action == disk_io_job::write)
		{
			for (; i != m_jobs.rend(); ++i)
			{
				if (*i < j)
				{
					if (i != m_jobs.rbegin()
						&& i.base()->storage.get() != j.storage.get())
						i = m_jobs.rbegin();
					break;
				}
			}
		}

		// if we are placed in front of all other jobs, put it on the back of
		// the queue, to sweep the disk in the same direction, and to avoid
		// starvation. The exception is if the priority is higher than the
		// job at the front of the queue
		if (i == m_jobs.rend() && (m_jobs.empty() || j.priority <= m_jobs.back().priority))
			i = m_jobs.rbegin();

		std::deque<disk_io_job>::iterator k = m_jobs.insert(i.base(), j);
		k->callback.swap(const_cast<boost::function<void(int, disk_io_job const&)>&>(f));
		if (j.action == disk_io_job::write)
			m_queue_buffer_size += j.buffer_size;
		TORRENT_ASSERT(j.storage.get());
		m_signal.notify_all();
	}

	char* disk_io_thread::allocate_buffer()
	{
		boost::mutex::scoped_lock l(m_mutex);
#ifdef TORRENT_STATS
		++m_allocations;
#endif
		return (char*)m_pool.ordered_malloc();
	}

	void disk_io_thread::free_buffer(char* buf)
	{
		boost::mutex::scoped_lock l(m_mutex);
#ifdef TORRENT_STATS
		--m_allocations;
#endif
		m_pool.ordered_free(buf);
	}

	void disk_io_thread::operator()()
	{
		for (;;)
		{
#ifdef TORRENT_DISK_STATS
			m_log << log_time() << " idle" << std::endl;
#endif
			boost::mutex::scoped_lock l(m_mutex);
#ifndef NDEBUG
			m_current.action = (disk_io_job::action_t)-1;
			m_current.piece = -1;
#endif
			while (m_jobs.empty() && !m_abort)
				m_signal.wait(l);
			if (m_abort && m_jobs.empty()) return;

			boost::function<void(int, disk_io_job const&)> handler;
			handler.swap(m_jobs.front().callback);
#ifndef NDEBUG
			m_current = m_jobs.front();
#endif
			disk_io_job j = m_jobs.front();
			m_jobs.pop_front();
			m_queue_buffer_size -= j.buffer_size;
			l.unlock();

			int ret = 0;

			bool free_buffer = true;
			try
			{
				TORRENT_ASSERT(j.storage);
#ifdef TORRENT_DISK_STATS
				ptime start = time_now();
#endif
//				std::cerr << "DISK THREAD: executing job: " << j.action << std::endl;
				switch (j.action)
				{
					case disk_io_job::read:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " read " << j.buffer_size << std::endl;
#endif
						free_buffer = false;
						if (j.buffer == 0)
						{
							l.lock();
							j.buffer = (char*)m_pool.ordered_malloc();
#ifdef TORRENT_STATS
							++m_allocations;
#endif
							l.unlock();
							TORRENT_ASSERT(j.buffer_size <= m_block_size);
							if (j.buffer == 0)
							{
								ret = -1;
								j.str = "out of memory";
								break;
							}
						}
						ret = j.storage->read_impl(j.buffer, j.piece, j.offset
							, j.buffer_size);

						// simulates slow drives
						// usleep(300);
						break;
					case disk_io_job::write:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " write " << j.buffer_size << std::endl;
#endif
						TORRENT_ASSERT(j.buffer);
						TORRENT_ASSERT(j.buffer_size <= m_block_size);
						j.storage->write_impl(j.buffer, j.piece, j.offset
							, j.buffer_size);

						// simulates a slow drive
						// usleep(300);
						break;
					case disk_io_job::hash:
						{
#ifdef TORRENT_DISK_STATS
							m_log << log_time() << " hash" << std::endl;
#endif
							sha1_hash h = j.storage->hash_for_piece_impl(j.piece);
							j.str.resize(20);
							std::memcpy(&j.str[0], &h[0], 20);
						}
						break;
					case disk_io_job::move_storage:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " move" << std::endl;
#endif
						ret = j.storage->move_storage_impl(j.str) ? 1 : 0;
						j.str = j.storage->save_path().string();
						break;
					case disk_io_job::release_files:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " release" << std::endl;
#endif
						j.storage->release_files_impl();
						break;
					case disk_io_job::delete_files:
#ifdef TORRENT_DISK_STATS
						m_log << log_time() << " delete" << std::endl;
#endif
						j.storage->delete_files_impl();
						break;
				}
			}
			catch (std::exception& e)
			{
//				std::cerr << "DISK THREAD: exception: " << e.what() << std::endl;
				j.str = e.what();
				ret = -1;
			}

//			if (!handler) std::cerr << "DISK THREAD: no callback specified" << std::endl;
//			else std::cerr << "DISK THREAD: invoking callback" << std::endl;
			try { if (handler) handler(ret, j); }
			catch (std::exception&) {}

#ifndef NDEBUG
			m_current.storage = 0;
#endif

			if (j.buffer && free_buffer)
			{
				l.lock();
				m_pool.ordered_free(j.buffer);
#ifdef TORRENT_STATS
				--m_allocations;
#endif
			}
		}
	}
}

<|endoftext|>


<commit_before>#include "shipmentschedule.h"

class mainwindow;

ShipmentSchedule::ShipmentSchedule()
{
    _railcarvector = QVector<RailCar*>();
}

ShipmentSchedule::~ShipmentSchedule()
{

}

void ShipmentSchedule::BuildDatabase()
{
    this->_railcarvector.push_back(new RailCar("AOK 494535"));
    this->_railcarvector.push_back(new RailCar("CSXT 493506"));
    this->_railcarvector.push_back(new RailCar("IHB 166590"));
    this->_railcarvector.push_back(new RailCar("NS 166154"));
    this->_railcarvector.push_back(new RailCar("NS 167155"));
}

/*** check if the RailCar is in the database
 * if yes, return railcar pointer
 * else return nullptr
 */
RailCar* ShipmentSchedule::GetCar(const QString id)
{
   for (int i = 0 ; i < _railcarvector.size(); i++)
   {
       if (_railcarvector[i]->getCarID() == id)
           return _railcarvector[i];
   }
   return nullptr;
}

QVector<RailCar*> ShipmentSchedule::getCars(){
    return _railcarvector;
}

<commit_msg>Add files via upload<commit_after>#include "shipmentschedule.h"
#include <QtSql>
class mainwindow;

ShipmentSchedule::ShipmentSchedule()
{
    _railcarvector = QVector<RailCar*>();
}

ShipmentSchedule::~ShipmentSchedule()
{

}

void ShipmentSchedule::BuildDatabase()
{
    /**this->_railcarvector.push_back(new RailCar("AOK 494535"));
    this->_railcarvector.push_back(new RailCar("CSXT 493506"));
    this->_railcarvector.push_back(new RailCar("IHB 166590"));
    this->_railcarvector.push_back(new RailCar("NS 166154"));
    this->_railcarvector.push_back(new RailCar("NS 167155"));
    */

    _db.setConnectOptions();
    QString dsn = QString("DRIVER={SQL Server}; SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(_servername).arg(_dbname);
    _db.setDatabaseName(dsn);

    QMap<QString,int>key_map;

    if (_db.open())
    {
            QSqlQuery qry;
            QString temp_str;

            if (qry.exec("SELECT * FROM [rcdata].dbo.Table_2"))
            {
                while(qry.next())
                {
                    temp_str = qry.value(0).toString();
                    key_map.insert(temp_str,1);
                    _map.insertMulti(temp_str, qry.value(1).toString());
                }
            }
            _db.close();
     }

     QMap<QString, int>::const_iterator i = key_map.constBegin();
     while (i != key_map.constEnd()) {
         this->_railcarvector.push_back(new RailCar(i.key(), _map));
         ++i;
    }
}

void ShipmentSchedule::UpdateDatabase(QString coilID, RailCar* RCID)
{
    QSqlQuery qry;
    if (_db.open())
    {
       // qry.first();
        QString execline = QString("UPDATE Table_2 SET Coils_Verification ='Yes' WHERE CoilsID ='%1'").arg(coilID);
        qry.exec(execline);

        _db.close();
    }

    // Update railcar status in the database
    if (_db.open())
    {
        if (RCID->isVerified())
        {
            QString execRC = QString("UPDATE Table_2 SET RC_Verification ='Yes' WHERE RailcarID ='%1'").arg(RCID->getCarID());
            qry.exec(execRC);

        }
        _db.close();
    }

}

/*** check if the RailCar is in the database
 * if yes, return railcar pointer
 * else return nullptr
 */
RailCar* ShipmentSchedule::GetCar(const QString id)
{
   for (int i = 0 ; i < _railcarvector.size(); i++)
   {
       if (_railcarvector[i]->getCarID() == id)
           return _railcarvector[i];
   }
   return nullptr;
}

QVector<RailCar*> ShipmentSchedule::getCars(){
    return _railcarvector;
}

<|endoftext|>