Exemple #1
0
ZHandle::Rep::Rep(size_t iSize)
	{
	#if ZCONFIG(OS, MacOS7) || ZCONFIG(OS, Carbon)
		// May need toolbox lock.
		fMacHandle = ::NewHandle(iSize);
		if (fMacHandle == nil)
			throw bad_alloc();
	#elif ZCONFIG(OS, Win32)
		if (iSize)
			{
			fData = reinterpret_cast<char*>(::GlobalAlloc(GMEM_FIXED, iSize));
			if (fData == nil)
				throw bad_alloc();
			}
		else
			{
			fData = nil;
			}
		fSize = iSize;
	#else
		if (iSize)
			{
			fData = reinterpret_cast<char*>(malloc(iSize));
			if (fData == nil)
				throw bad_alloc();
			}
		else
			{
			fData = nil;
			}
		fSize = iSize;
	#endif

	fLockCount.fValue = 0;
	}
Exemple #2
0
ArrayRCP<zscalar_t> makeWeights(
  const RCP<const Teuchos::Comm<int> > & comm,
  zlno_t len, weightTypes how, zscalar_t scale, int rank)
{
  zscalar_t *wgts = new zscalar_t [len];
  if (!wgts)
    throw bad_alloc();

  ArrayRCP<zscalar_t> weights(wgts, 0, len, true);

  if (how == upDown){
    zscalar_t val = scale + rank%2;
    for (zlno_t i=0; i < len; i++)
      wgts[i] = val;
  }
  else if (how == roundRobin){
    for (int i=0; i < 10; i++){
      zscalar_t val = (i + 10)*scale;
      for (zlno_t j=i; j < len; j += 10)
         weights[j] = val;
    }
  }
  else if (how == increasing){
    zscalar_t val = scale + rank;
    for (zlno_t i=0; i < len; i++)
      wgts[i] = val;
  }

  return weights;
}
ParserInterfaceScannerMembers::ParserInterfaceScannerMembers(
    const char* const query
) :
    scanner_(),
    bufferState_(nullptr)
{
    if (0 != sql_lex_init(&scanner_))
    {
        throw bad_alloc();
    }
    bufferState_ = sql__scan_string(query, scanner_);
    if (nullptr == bufferState_)
    {
        sql_lex_destroy(scanner_);
        throw bad_alloc();
    }
}
Exemple #4
0
/**
 * Resizes the FIFO's buffer so that it is at least newSize bytes long.
 *
 * @param newSize The minimum new size of the FIFO buffer.
 */
void FIFO::ResizeBuffer(size_t newSize)
{
    if (m_AllocSize >= newSize)
        return;

    newSize = (newSize / FIFO::BlockSize + 1) * FIFO::BlockSize;

    char *newBuffer = static_cast<char *>(realloc(m_Buffer, newSize));

    if (newBuffer == NULL)
        throw_exception(bad_alloc());

    m_Buffer = newBuffer;

    m_AllocSize = newSize;
}
Exemple #5
0
/**
 * If you want to upload more than one file, you can pass the form name and a 
 * vector of filenames.
 */
void curl_form::add(const curl_pair<CURLformoption,string> &form_name, const vector<string> &files) {
    const size_t size = files.size();
    struct curl_forms *new_files;
    this->is_null(new_files = (struct curl_forms *)calloc(size,sizeof(struct curl_forms)));
    if (new_files == nullptr) {
        throw bad_alloc();
    }
    for (size_t i = 0; i < size; ++i) {
        new_files[i].option = CURLFORM_FILE;
        new_files[i].value = files[i].c_str();
    }
    if (curl_formadd(&this->form_post,&this->last_ptr,
                    form_name.first(),form_name.second(),
                    CURLFORM_ARRAY,new_files,
                    CURLFORM_END) != 0) {
        delete []new_files;
        throw curl_exception("Error while adding the form",__FUNCTION__);
    } 
    delete []new_files;
}
Exemple #6
0
void ZHandle::Touch()
	{
	if (fRep->GetRefCount() == 1)
		return;

	#if ZCONFIG(OS, MacOS7) || ZCONFIG(OS, Carbon)
		ZRef<Rep> newRep = new Rep;
		if (fRep->fMacHandle)
			{
			// May need toolbox lock.
			newRep->fMacHandle = fRep->fMacHandle;
			::HandToHand(&newRep->fMacHandle);
			if (newRep->fMacHandle == nil)
				throw bad_alloc();
			}
		fRep = newRep;
	#else
		Rep* newRep = new Rep(this->GetSize());
		ZBlockCopy(fRep->fData, newRep->fData, fRep->fSize);
		fRep = newRep;
	#endif
	}
Exemple #7
0
_GLIBCXX_WEAK_DEFINITION void *
operator new (std::size_t sz) throw (std::bad_alloc)
{
  void *p;

  /* malloc (0) is unpredictable; avoid it.  */
  if (sz == 0)
    sz = 1;
  p = (void *) malloc (sz);
  while (p == 0)
    {
      new_handler handler = __new_handler;
      if (! handler)
#ifdef __EXCEPTIONS
	throw bad_alloc();
#else
        std::abort();
#endif
      handler ();
      p = (void *) malloc (sz);
    }

  return p;
}
Exemple #8
0
void ZHandle::SetSize(size_t iSize)
	{
// We shouldn't be trying to resize when we're locked
	ZAssertStop(2, fRep->fLockCount.fValue == 0);
	#if ZCONFIG(OS, MacOS7) || ZCONFIG(OS, Carbon)
		if (iSize == 0)
			{
			if (fRep->fMacHandle == nil)
				return;
			fRep = new Rep;
			}
		else
			{
			if (fRep->fMacHandle == nil)
				{
				fRep = new Rep(iSize);
				}
			else
				{
				// May need toolbox lock.
				size_t currentSize = ::GetHandleSize(fRep->fMacHandle);
				if (currentSize != iSize)
					{
					if (fRep->GetRefCount() == 1)
						{
						::SetHandleSize(fRep->fMacHandle, iSize);
						if (::MemError() != noErr)
							throw bad_alloc();
						}
					else
						{
						Rep* newRep = new Rep(iSize);
						ZBlockCopy(fRep->fMacHandle[0], newRep->fMacHandle[0],
							min(currentSize, iSize));
						fRep = newRep;
						}
					}
				}
			}
	#else // ZCONFIG(OS, MacOS7) || ZCONFIG(OS, Carbon)
		if (iSize == 0)
			{
			if (fRep->fData == nil)
				return;
			fRep = new Rep;
			}
		else
			{
			if (fRep->fData == nil)
				{
				fRep = new Rep(iSize);
				}
			else
				{
				if (iSize != fRep->fSize)
					{
					if (fRep->GetRefCount() == 1)
						{
						#if ZCONFIG(OS, Win32)
							char* newData = reinterpret_cast<char*>(
								::GlobalReAlloc(
									reinterpret_cast<HGLOBAL>(fRep->fData),
									iSize, GMEM_MOVEABLE));
						#else
							char* newData = reinterpret_cast<char*>(
								::realloc(fRep->fData, iSize));
						#endif
	
						if (newData == nil)
							throw bad_alloc();
						fRep->fData = newData;
						fRep->fSize = iSize;
						}
					else
						{
						Rep* newRep = new Rep(iSize);
						ZBlockCopy(fRep->fData, newRep->fData, min(fRep->fSize, newRep->fSize));
						fRep = newRep;
						}
					}
				}
			}
	#endif // ZCONFIG(OS, MacOS7) || ZCONFIG(OS, Carbon)
	}
/* Create a mesh of approximately the desired size.
 *
 *  We want 3 dimensions close to equal in length.
 */
tMVector_t* makeMeshCoordinates(
    const RCP<const Teuchos::Comm<int> > & comm,
    gno_t numGlobalCoords)
{
  int rank = comm->getRank();
  int nprocs = comm->getSize();

  double k = log(numGlobalCoords) / 3;
  double xdimf = exp(k) + 0.5;
  gno_t xdim = static_cast<gno_t>(floor(xdimf));
  gno_t ydim = xdim;
  gno_t zdim = numGlobalCoords / (xdim*ydim);
  gno_t num=xdim*ydim*zdim;
  gno_t diff = numGlobalCoords - num;
  gno_t newdiff = 0;

  while (diff > 0){
    if (zdim > xdim && zdim > ydim){
      zdim++;
      newdiff = diff - (xdim*ydim);
      if (newdiff < 0)
        if (diff < -newdiff)
          zdim--;
    }
    else if (ydim > xdim && ydim > zdim){
      ydim++;
      newdiff = diff - (xdim*zdim);
      if (newdiff < 0)
        if (diff < -newdiff)
          ydim--;
    }
    else{
      xdim++;
      newdiff = diff - (ydim*zdim);
      if (newdiff < 0)
        if (diff < -newdiff)
          xdim--;
    }

    diff = newdiff;
  }

  num=xdim*ydim*zdim;
  diff = numGlobalCoords - num;
  if (diff < 0)
    diff /= -numGlobalCoords;
  else
    diff /= numGlobalCoords;

  if (rank == 0){
    if (diff > .01)
      cout << "Warning: Difference " << diff*100 << " percent" << endl;
    cout << "Mesh size: " << xdim << "x" << ydim << "x" <<
      zdim << ", " << num << " vertices." << endl;
  }

  // Divide coordinates.

  gno_t numLocalCoords = num / nprocs;
  gno_t leftOver = num % nprocs;
  gno_t gid0 = 0;

  if (rank <= leftOver)
    gid0 = gno_t(rank) * (numLocalCoords+1);
  else
    gid0 = (leftOver * (numLocalCoords+1)) +
           ((gno_t(rank) - leftOver) * numLocalCoords);

  if (rank < leftOver)
    numLocalCoords++;

  gno_t gid1 = gid0 + numLocalCoords;

  gno_t *ids = new gno_t[numLocalCoords];
  if (!ids)
    throw bad_alloc();
  ArrayView<gno_t> idArray(ids, numLocalCoords);

  for (gno_t i=gid0, *idptr=ids; i < gid1; i++)
    *idptr++ = i;

  RCP<const tMap_t> idMap = rcp(new tMap_t(num, idArray, 0, comm));

  delete [] ids;

  // Create a Tpetra::MultiVector of coordinates.

  scalar_t *x = new scalar_t [numLocalCoords*3];
  if (!x) throw bad_alloc();

  scalar_t *y = x + numLocalCoords;
  scalar_t *z = y + numLocalCoords;

  gno_t xStart = 0;
  gno_t yStart = 0;
  gno_t xyPlane = xdim*ydim;
  gno_t zStart = gid0 / xyPlane;
  gno_t rem = gid0 % xyPlane;
  if (rem > 0){
    yStart = rem / xdim;
    xStart = rem % xdim;
  }

  lno_t next = 0;
  for (scalar_t zval=zStart; next < numLocalCoords && zval < zdim; zval+=1.){
    for (scalar_t yval=yStart; next < numLocalCoords && yval < ydim; yval+=1.){
      for (scalar_t xval=xStart; next < numLocalCoords && xval < xdim;xval+=1.){
        x[next] = xval;
        y[next] = yval;
        z[next] = zval;
        next++;
      }
      xStart = 0;
    }
    yStart = 0;
  }

  ArrayView<const scalar_t> xArray(x, numLocalCoords*3);
  tMVector_t *dots = new tMVector_t(idMap, xArray, numLocalCoords, 3);

  delete [] x;
  return dots;
}
Exemple #10
0
/*! \brief Create a mesh of approximately the desired size.
 *
 *  We want 3 dimensions close to equal in length.
 */
const RCP<tMVector_t> getMeshCoordinates(
    const RCP<const Teuchos::Comm<int> > & comm,
    zgno_t numGlobalCoords)
{
  int rank = comm->getRank();
  int nprocs = comm->getSize();

  double k = log(numGlobalCoords) / 3;
  double xdimf = exp(k) + 0.5;
  ssize_t xdim = static_cast<ssize_t>(floor(xdimf));
  ssize_t ydim = xdim;
  ssize_t zdim = numGlobalCoords / (xdim*ydim);
  ssize_t num=xdim*ydim*zdim;
  ssize_t diff = numGlobalCoords - num;
  ssize_t newdiff = 0;

  while (diff > 0){
    if (zdim > xdim && zdim > ydim){
      zdim++;
      newdiff = diff - (xdim*ydim);
      if (newdiff < 0)
        if (diff < -newdiff)
          zdim--;
    }
    else if (ydim > xdim && ydim > zdim){
      ydim++;
      newdiff = diff - (xdim*zdim);
      if (newdiff < 0)
        if (diff < -newdiff)
          ydim--;
    }
    else{
      xdim++;
      newdiff = diff - (ydim*zdim);
      if (newdiff < 0)
        if (diff < -newdiff)
          xdim--;
    }

    diff = newdiff;
  }

  num=xdim*ydim*zdim;
  diff = numGlobalCoords - num;
  if (diff < 0)
    diff /= -numGlobalCoords;
  else
    diff /= numGlobalCoords;

  if (rank == 0){
    if (diff > .01)
      cout << "Warning: Difference " << diff*100 << " percent" << endl;
    cout << "Mesh size: " << xdim << "x" << ydim << "x" <<
      zdim << ", " << num << " vertices." << endl;
  }

  // Divide coordinates.

  ssize_t numLocalCoords = num / nprocs;
  ssize_t leftOver = num % nprocs;
  ssize_t gid0 = 0;

  if (rank <= leftOver)
    gid0 = zgno_t(rank) * (numLocalCoords+1);
  else
    gid0 = (leftOver * (numLocalCoords+1)) + 
           ((zgno_t(rank) - leftOver) * numLocalCoords);

  if (rank < leftOver)
    numLocalCoords++;

  ssize_t gid1 = gid0 + numLocalCoords;

  zgno_t *ids = new zgno_t [numLocalCoords];
  if (!ids)
    throw bad_alloc();
  ArrayRCP<zgno_t> idArray(ids, 0, numLocalCoords, true);

  for (ssize_t i=gid0; i < gid1; i++)
    *ids++ = zgno_t(i);   

  RCP<const tMap_t> idMap = rcp(
    new tMap_t(num, idArray.view(0, numLocalCoords), 0, comm));

  // Create a Tpetra::MultiVector of coordinates.

  zscalar_t *x = new zscalar_t [numLocalCoords*3]; 
  if (!x)
    throw bad_alloc();
  ArrayRCP<zscalar_t> coordArray(x, 0, numLocalCoords*3, true);

  zscalar_t *y = x + numLocalCoords;
  zscalar_t *z = y + numLocalCoords;

  zgno_t xStart = 0;
  zgno_t yStart = 0;
  zgno_t xyPlane = xdim*ydim;
  zgno_t zStart = gid0 / xyPlane;
  zgno_t rem = gid0 % xyPlane;
  if (rem > 0){
    yStart = rem / xdim;
    xStart = rem % xdim;
  }

  zlno_t next = 0;
  for (zscalar_t zval=zStart; next < numLocalCoords && zval < zdim; zval++){
    for (zscalar_t yval=yStart; next < numLocalCoords && yval < ydim; yval++){
      for (zscalar_t xval=xStart; next < numLocalCoords && xval < xdim; xval++){
        x[next] = xval;
        y[next] = yval;
        z[next] = zval;
        next++;
      }
      xStart = 0;
    }
    yStart = 0;
  }

  ArrayView<const zscalar_t> xArray(x, numLocalCoords);
  ArrayView<const zscalar_t> yArray(y, numLocalCoords);
  ArrayView<const zscalar_t> zArray(z, numLocalCoords);
  ArrayRCP<ArrayView<const zscalar_t> > coordinates =
    arcp(new ArrayView<const zscalar_t> [3], 0, 3);
  coordinates[0] = xArray;
  coordinates[1] = yArray;
  coordinates[2] = zArray;

  ArrayRCP<const ArrayView<const zscalar_t> > constCoords =
   coordinates.getConst();

  RCP<tMVector_t> meshCoords = rcp(new tMVector_t(
    idMap, constCoords.view(0,3), 3));

  return meshCoords;
}