Пример #1
0
T heap<T>::extract()
	{
	if (0 == currSize)
		{
        throw std::runtime_error("removing from empty heap"); 
		}
	T data = treeArr[0];
	swap(treeArr, 0, currSize-1);
	g_delete(treeArr[--currSize]);
	reheap(0);
	return data;
	}
Пример #2
0
bool heap<T>::remove(T data, bool (*eqCb)(const T&, const T&))
	{
	bool dataRemoved = false;
	
	signed i = treeArr.indexOf(data, eqCb);
		
	if (i > 0) // assert that i < currSize
		{
		dataRemoved = true;
		swap(treeArr, i, currSize);
		g_delete(treeArr[--currSize]);
		reheap(i);
		}
	
	return dataRemoved;
	}
Пример #3
0
/*
 * No need to touch anything here.
 */
int main (int argc, char **argv)
{
  Graph *graph;
  Vertex *start;
  size_t ii;
  
  if (argc < 3)
    errx (EXIT_FAILURE, "Please specify a file name and a city name.");
  graph = g_new ();
  g_load (graph, argv[1]);
  start = g_find (graph, argv[2]);
  if (NULL == start)
    errx (EXIT_FAILURE, "No vertex called %s in file %s", argv[2], argv[1]);
  
  for (ii = 0; ii < graph->size; ++ii)
    graph->vertex[ii]->value = -1;
  bfs (start);
  
  g_delete (graph);
  return 0;
}
Пример #4
0
/*
 * Thread reads from or writes to local component and also handles DELETE and
 * FLUSH requests.
 */
static void *
disk_thread(void *arg)
{
	struct hast_resource *res = arg;
	struct hio *hio;
	ssize_t ret;
	bool clear_activemap, logerror;

	clear_activemap = true;

	for (;;) {
		pjdlog_debug(2, "disk: Taking request.");
		QUEUE_TAKE(disk, hio);
		while (clear_activemap) {
			unsigned char *map;
			size_t mapsize;

			/*
			 * When first request is received, it means that primary
			 * already received our activemap, merged it and stored
			 * locally. We can now safely clear our activemap.
			 */
			mapsize =
			    activemap_calc_ondisk_size(res->hr_local_mediasize -
			    METADATA_SIZE, res->hr_extentsize,
			    res->hr_local_sectorsize);
			map = calloc(1, mapsize);
			if (map == NULL) {
				pjdlog_warning("Unable to allocate memory to clear local activemap.");
				break;
			}
			if (pwrite(res->hr_localfd, map, mapsize,
			    METADATA_SIZE) != (ssize_t)mapsize) {
				pjdlog_errno(LOG_WARNING,
				    "Unable to store cleared activemap");
				free(map);
				break;
			}
			free(map);
			clear_activemap = false;
			pjdlog_debug(1, "Local activemap cleared.");
			break;
		}
		reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio);
		logerror = true;
		/* Handle the actual request. */
		switch (hio->hio_cmd) {
		case HIO_READ:
			ret = pread(res->hr_localfd, hio->hio_data,
			    hio->hio_length,
			    hio->hio_offset + res->hr_localoff);
			if (ret < 0)
				hio->hio_error = errno;
			else if (ret != (int64_t)hio->hio_length)
				hio->hio_error = EIO;
			else
				hio->hio_error = 0;
			break;
		case HIO_WRITE:
			ret = pwrite(res->hr_localfd, hio->hio_data,
			    hio->hio_length,
			    hio->hio_offset + res->hr_localoff);
			if (ret < 0)
				hio->hio_error = errno;
			else if (ret != (int64_t)hio->hio_length)
				hio->hio_error = EIO;
			else
				hio->hio_error = 0;
			break;
		case HIO_DELETE:
			ret = g_delete(res->hr_localfd,
			    hio->hio_offset + res->hr_localoff,
			    hio->hio_length);
			if (ret < 0)
				hio->hio_error = errno;
			else
				hio->hio_error = 0;
			break;
		case HIO_FLUSH:
			if (!res->hr_localflush) {
				ret = -1;
				hio->hio_error = EOPNOTSUPP;
				logerror = false;
				break;
			}
			ret = g_flush(res->hr_localfd);
			if (ret < 0) {
				if (errno == EOPNOTSUPP)
					res->hr_localflush = false;
				hio->hio_error = errno;
			} else {
				hio->hio_error = 0;
			}
			break;
		default:
			PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
			    hio->hio_cmd);
		}
		if (logerror && hio->hio_error != 0) {
			reqlog(LOG_ERR, 0, hio->hio_error, hio,
			    "Request failed: ");
		}
		pjdlog_debug(2, "disk: (%p) Moving request to the send queue.",
		    hio);
		QUEUE_INSERT(send, hio);
	}
	/* NOTREACHED */
	return (NULL);
}
Пример #5
0
int nand_erase(struct cmd_param *params)
{
	struct chip_param_io chip_params;
	char *dev;
	int fd = -1, ret = 0;
	off_t pos, count;
	off_t start, nblocks, i;
	int block_size, mult;

	if (!(dev = param_get_string(params, "dev"))) {
		fprintf(stderr, "Please supply valid 'dev' parameter.\n");
		return (1);
	}

	if (param_has_value(params, "count"))
		count = param_get_intx(params, "count");
	else
		count = 1;

	if ((fd = g_open(dev, 1)) < 0) {
		perrorf("Cannot open %s", dev);
		return (1);
	}

	if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) {
		perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
		ret = 1;
		goto out;
	}

	block_size = chip_params.page_size * chip_params.pages_per_block;

	if (param_has_value(params, "page")) {
		pos = chip_params.page_size * param_get_intx(params, "page");
		mult = chip_params.page_size;
	} else if (param_has_value(params, "block")) {
		pos = block_size * param_get_intx(params, "block");
		mult = block_size;
	} else if (param_has_value(params, "pos")) {
		pos = param_get_intx(params, "pos");
		mult = 1;
	} else {
		/* Erase whole chip */
		if (ioctl(fd, DIOCGMEDIASIZE, &count) == -1) {
			ret = 1;
			goto out;
		}

		pos = 0;
		mult = 1;
	}

	if (pos % block_size) {
		fprintf(stderr, "Position must be block-size aligned!\n");
		ret = 1;
		goto out;
	}

	count *= mult;
	start = pos / block_size;
	nblocks = count / block_size;

	for (i = 0; i < nblocks; i++) {
		if (g_delete(fd, (start + i) * block_size, block_size) == -1) {
			perrorf("Cannot erase block %d - probably a bad block",
			    start + i);
			ret = 1;
		}
	}

out:
	g_close(fd);

	return (ret);
}