PB_DS_CLASS_T_DEC
PB_DS_CLASS_C_DEC::
PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
#ifdef _GLIBCXX_DEBUG
  debug_base(other),
#endif 
  hash_eq_fn_base(other),
  resize_base(other),
  ranged_probe_fn_base(other),
  m_num_e(other.m_num_e),
  m_num_used_e(other.m_num_used_e),
  m_entries(s_entry_allocator.allocate(m_num_e))
{
  for (size_type i = 0; i < m_num_e; ++i)
    m_entries[i].m_stat = (entry_status)empty_entry_status;

  __try
    {
      for (size_type i = 0; i < m_num_e; ++i)
        {
	  m_entries[i].m_stat = other.m_entries[i].m_stat;
	  if (m_entries[i].m_stat == valid_entry_status)
	    new (m_entries + i) entry(other.m_entries[i]);
        }
    }
  __catch(...)
    {
      deallocate_all();
      __throw_exception_again;
    }
PB_ASSOC_CLASS_T_DEC
PB_ASSOC_CLASS_C_DEC::
PB_ASSOC_CLASS_NAME(const PB_ASSOC_CLASS_C_DEC& r_other) :
  my_hash_eq_fn_base(r_other),
  my_resize_base(r_other),
  my_ranged_probe_fn_base(r_other),
  m_a_entries(s_entry_allocator.allocate(
					 r_other.m_num_e)),
  m_num_e(r_other.m_num_e),
  m_num_used_e(r_other.m_num_used_e)
{
  initialize();

  try
    {
      for (size_type i = 0; i < m_num_e; ++i)
	{
	  entry_pointer p_e =& r_other.m_a_entries[i];
	  if (p_e->m_stat == VALID_ENTRY_STATUS)
	    constructor_insert_new_imp((const_mapped_reference)p_e->m_value,
				       i,
				       my_hash_traits_base::s_store_hash_indicator);
	}
    }
  catch(...)
    {
      deallocate_all();

      throw;
    }

  PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
    }
PB_DS_CLASS_T_DEC
PB_DS_CLASS_C_DEC::
PB_DS_LU_NAME(const PB_DS_CLASS_C_DEC& other) :
m_p_l(0)
{
  __try
    {
      for (const_iterator it = other.begin(); it != other.end(); ++it)
	{
	  entry_pointer p_l = allocate_new_entry(*it,
				      traits_base::m_no_throw_copies_indicator);

	  p_l->m_p_next = m_p_l;
	  m_p_l = p_l;
	}
    }
  __catch(...)
    {
      deallocate_all();
      __throw_exception_again;
    }
PB_ASSOC_CLASS_T_DEC
PB_ASSOC_CLASS_C_DEC::
~PB_ASSOC_CLASS_NAME()
{
  deallocate_all();
}
示例#5
0
文件: xloader.c 项目: OPSF/uClinux
int load_image (char *filename)
{
	FILE *image;
	struct stat statbuf;
	int filesize, i, j, n;
	int bytes_read = 0;
	int links_per_car, links_over;
	int percent;
	int ferror_image;
	struct MD5Context  md5c;
	unsigned char      digest[16];

	if (opt_stdin) {
		image = stdin;
	} else {
		/* open image file: */
		image = fopen (filename, "r");
		if (image == NULL) {
			perror ("Error opening image file");
			return (errno);
		}
	}


	/* Check for the presence of a uCimage file header: */
	/* read header: */
	fread (&header, sizeof(header), 1, image);
	check_uCimage (&header, image);

	if (header_present) {
		filesize = header.data_size;
		MD5Init(&md5c); /* Initialize MD5 module */

	} else {
		/* no uCimage header present: */
		if (opt_stdin) {
			/* allocate the maximum size, since we do not know
			 * how big the image will be: */
			filesize = MAX_IMAGE_SIZE;
		} else {
	/* stat input file */
	if (stat (filename, &statbuf)) {
		perror ("Error stat()ing image file");
		return (errno);
	}
	/* otherwise, all is still OK: */
	filesize = statbuf.st_size;	
		}
	}


	/* build buffer chain: */
	links = (int) ((filesize + BUFFERSIZE -1) / BUFFERSIZE);
	/* chain = (void *)malloc (links * sizeof (void *)); */

	/* build link train: */
	links_per_car = CARSIZE / BUFFERSIZE;
	cars = 1 + links / links_per_car;

	/* Can we fit the chain into the last car? */
	/* How many links in the last car? */
	links_over = links - (cars - 1) * links_per_car;
	if ((CARSIZE - links_over*BUFFERSIZE) <
		links * sizeof (void *)) {
		/* then the chain can not be placed in the last car;
		 * allocate one more.*/
		cars++;
		links_over = links - (cars - 1) * links_per_car;
		if (links_over < 0) links_over = 0;
	}

	/* allocate the array of cars: */
	/* note: this array can be discarded once the chain of links
	 * has been mapped onto the actual buffers */
	train = (void *)malloc(cars * sizeof (void *));
	if ( train == NULL) {
		if (!opt_quiet)
		fprintf (stderr, "Error allocating train\n");
		return (errno);
	} else
		memset (train, 0, cars * sizeof (void *));

	/* allocate the cars: */
	for (i=0;i<cars;i++) {
		train[i] = (void *)malloc(CARSIZE);
		if (train[i] == NULL) {
			if (!opt_quiet)
			fprintf (stderr, "Error allocating car %d\n", i);
			/* before we return, free all allocated memories */
			deallocate_all();
			return (errno);
		}
		DBG("train[%d] = %p\n", i, train[i]);
	}

	/* sort the cars */
	sort_pointers (train, cars);

	/* map the chain into the last car: */
	chain = (void *)(train[cars-1]) + (links_over * BUFFERSIZE);

	/* allocate links into the cars: */
	for (i=0;i<cars;i++,j++) {
		DBG("\ntrain[%d] = %p cars:\n", i, train[i]);
		for (j=0;j<links_per_car;j++) {
			if (i*links_per_car+j >= links)
				break;
			chain[i*links_per_car+j] = train[i] + (BUFFERSIZE * j);
			DBG("  0x%08x", (unsigned int)chain[i*links_per_car+j]);
		}
	}

	DBG("\nfilesize = %d, links = %d\n", filesize, links);
	DBG("cars = %d, links_per_car = %d, links_over = %d\n", cars, links_per_car, links_over);
	DBG("car[%d] = %p, chain = %p\n", cars-1, train[cars-1], chain);



	/* populate chain with image file: */
	for (i=0;i<cars;i++) {
		if ((i+1)*links_per_car <= links)
			j = links_per_car;
		else
			j = links_over;

		n = fread (train[i], 1, BUFFERSIZE * j, image);

		if (opt_debug)
			fprintf(stderr, "fread %d bytes to car[%d] = %p\n",
					n, i, train[i]);
		else {
			percent = (((i*links_per_car+j)+1) * 100)/links;
			/* if (percent%10 == 0) */
			if (!opt_quiet)
			fprintf (stderr, "\r%d%%", percent);
		}

		/* Compute MD5 as we read the file: */
		if (header_present)
			MD5Update (&md5c, (char *)(train[i]), n);


		bytes_read += n;
			
		if (n < BUFFERSIZE * j) {
			if (opt_stdin && !header_present) {
				/* assume the transmission has finished */
				break;
			} else {
				if (bytes_read != filesize) {
					ferror_image = ferror (image);
					if (!opt_quiet)
			fprintf (stderr, "Error #%d reading from image file\n",
							 ferror_image);
			fclose (image);
					return ferror_image;
				}
			}
		}
	}

	if (!opt_debug && !opt_quiet)
		fprintf (stderr, "\n");

	if (opt_stdin && !feof(image)) {
		if (bytes_read != filesize) {
			if (!opt_quiet)
				fprintf (stderr, "Image too big, max %d\n", filesize);
 	fclose (image);
			deallocate_all();
			return -1;
		}
	}

 	fclose (image);
	/* free(train); */

	if (opt_stdin) {
		filesize = bytes_read;
		DBG("size of received file = %d bytes\n", filesize);
	}

	/* NOTE THAT we should have some kind of header and / or footer to verify
	 * that our image is error free. For starters, make sure it's not empty.
	 */

	if (filesize == 0) {
		if (!opt_quiet)
			fprintf(stderr, "Image is empty.\n");
		deallocate_all();
		return -1;
	}

	if (opt_watchdog) {
		fputc('c', watchdog);
		fflush (watchdog);
	}


	if (header_present) {
		char buf[42], ascii_md5[34];

		/* save MD5: */
		MD5Final (&(digest[0]), &md5c);

		if (!opt_quiet)
			printf ("Checking MD5 signature... ");

		/* print the signature to a string: */
		for (i=0;i<16;i++)
			sprintf (&(ascii_md5[i*2]), "%02x", digest[i]);

		/* Compare with given digest: */
		for (i=0;i<16;i++) {
			if (digest[i] != header.md5sum[i]) {
				printf ("ERROR: MD5 digest mismatch\n");
				printf ("MD5 digest calculated as:  %s\n", ascii_md5);
				printf ("failed to match given MD5: ");
				for (i=0;i<16;i++)
					printf ("%02x", header.md5sum[i]);
				printf ("\n");
				return (1);
			}
		}

		if (!opt_quiet)
			printf ("verified\n");

		if (opt_watchdog) {
			fputc('c', watchdog);
			fflush (watchdog);
		}

		if (opt_flashloader) {
			/* write uCbootstrap envars for the image: */
			if (opt_partition) {
				sprintf (buf, "p_%s_SIZE=%d", opt_partition, header.data_size);
				setbenv(buf);

				sprintf (buf, "p_%s_MD5=%s", opt_partition, ascii_md5);
				setbenv(buf);
			}
		}
		
	} /* endif header_present */



	/* set uCbootloader arguments: */
	m.len = filesize;
	m.offset = (void *)chain;

	return (0);
}
示例#6
0
文件: xloader.c 项目: OPSF/uClinux
int main (int argc, char *argv[])
{

	if (parse_args (argc, argv))
		if (!opt_quiet)
		usage();

	if (opt_watchdog) {
		fprintf (stderr, "Opening /dev/watchdog.\n");
		watchdog = fopen ("/dev/watchdog", "w");
		if (watchdog == NULL) {
			perror ("Error opening watchdog device file");
			exit(-1);
		}
		fputc('c', watchdog);
		fflush (watchdog);
	}

	if (!opt_4k) {
		if (load_image (opt_filename))
			exit (-1);
	} else {
		if (load_image_4k (opt_filename))
			exit (-1);
	}

	if (opt_watchdog) {
		fputc('c', watchdog);
		fflush (watchdog);
	}

	flags = (opt_debug?(PGM_DEBUG):0);

	if (opt_noop) {
		deallocate_all();
		exit (1);
	}

	fflush (stdout);
	fflush (stderr);
	sleep (1);

	if (opt_ramloader)
		ramload(&m, flags | PGM_EXEC_AFTER);
	else if (opt_flashloader) {
		if (opt_program_all)
			program(&m, flags | PGM_ERASE_FIRST | PGM_RESET_AFTER);
		else {
			if (opt_partition)
				program2(&m, flags | PGM_ERASE_FIRST | PGM_RESET_AFTER, opt_partition);
		else
				program2(&m, flags | PGM_ERASE_FIRST | PGM_RESET_AFTER, "0");
		}
	}

	/* not reached:
	 * PGM_EXEC_AFTER starts the new kernel,
	 * PGM_RESET_AFTER resets the board.
	 */
	exit (-1);

}
 ~default_pool_alloc() {
   deallocate_all();
 }