Exemplo n.º 1
0
	void* operator new[] (size_t size) throw (bad_alloc)
	{
		if (m_poolManager.init != B_OK) throw bad_alloc();
		void* ret = rtm_alloc(m_poolManager.pool, size);
		if (! ret) throw bad_alloc();
		return ret;
	}
Exemplo n.º 2
0
_LIBCPP_HIDDEN
__i_node*
__libcpp_db::__insert_iterator(void* __i)
{
    if (__isz_ + 1 > __iend_ - __ibeg_)
    {
        size_t nc = __next_prime(2*(__iend_ - __ibeg_) + 1);
        __i_node** ibeg = (__i_node**)calloc(nc, sizeof(void*));
        if (ibeg == nullptr)
            throw bad_alloc();
        for (__i_node** p = __ibeg_; p != __iend_; ++p)
        {
            __i_node* q = *p;
            while (q != nullptr)
            {
                size_t h = hash<void*>()(q->__i_) % nc;
                __i_node* r = q->__next_;
                q->__next_ = ibeg[h];
                ibeg[h] = q;
                q = r;
            }
        }
        free(__ibeg_);
        __ibeg_ = ibeg;
        __iend_ = __ibeg_ + nc;
    }
    size_t hi = hash<void*>()(__i) % (__iend_ - __ibeg_);
    __i_node* p = __ibeg_[hi];
    __i_node* r = __ibeg_[hi] = (__i_node*)malloc(sizeof(__i_node));
    if (r == nullptr)
        throw bad_alloc();
    ::new(r) __i_node(__i, p, nullptr);
    ++__isz_;
    return r;
}
Exemplo n.º 3
0
// returns the data of the uninstaller icon that should replace the installer icon data
// return values:
//   0  - Bad icon file
//   Anything else - Pointer to the uninstaller icon data
unsigned char* generate_uninstall_icon_data(char* filename)
{
  int i;

  FILE* f = fopen(filename, "rb");
  if (!f) return 0;

  IconGroupHeader igh;
  if (!fread(&igh, sizeof(IconGroupHeader), 1, f)) return 0;

  if (igh.wIsIcon != 1 && igh.wReserved != 0) return 0;

  int iNewIconSize = 0;
  FileIconGroupEntry ige;

  DWORD* offsets = (DWORD*)malloc(sizeof(DWORD)*igh.wCount);
  DWORD* rawSizes = (DWORD*)malloc(sizeof(DWORD)*igh.wCount);
  if (!offsets || !rawSizes) throw bad_alloc();

  for (i = 0; i < igh.wCount; i++) {
    if (!fread(&ige, sizeof(FileIconGroupEntry), 1, f)) return 0;
    offsets[i] = ige.dwImageOffset;
    rawSizes[i] = ige.dwRawSize;
    iNewIconSize += ige.dwRawSize;
  }

  // Before each icon come two DWORDs, one for size and the other for offset (set later)
  // The last size is 0, no offset
  iNewIconSize += sizeof(DWORD)*(1 + igh.wCount*2);

  BYTE* pbUninstIcon = (BYTE*)malloc(iNewIconSize);
  if (!pbUninstIcon) throw bad_alloc();

  BYTE* seeker = pbUninstIcon;

  for (i = 0; i < igh.wCount; i++) {
    *(DWORD*)seeker = rawSizes[i];
    seeker += sizeof(DWORD);
    *(DWORD*)seeker = 0;
    seeker += sizeof(DWORD);
    fseek(f, offsets[i], SEEK_SET);
    fread(seeker, 1, rawSizes[i], f);
    seeker += rawSizes[i];
  }

  // This is how we know there are no more icons (size = 0)
  *(DWORD*)seeker = 0;

  free(offsets);
  free(rawSizes);

  unicondata_size = iNewIconSize;

  return pbUninstIcon;
}
Exemplo n.º 4
0
void* single_alloc(size_t size)
{
    if ( size > SINGLE_ALLOC_THRESHOLD ) {
        return ::operator new(size);
    }
    sx_SingleAllocMutex.Lock();
    size_t pool_size = single_alloc_pool_size;
    char* pool;
    if ( size > pool_size ) {
        pool_size = SINGLE_ALLOC_POOL_SIZE;
        pool = (char*) malloc(pool_size);
        if ( !pool ) {
            sx_SingleAllocMutex.Unlock();
            throw bad_alloc();
        }
        single_alloc_pool = pool;
        single_alloc_pool_size = pool_size;
    }
    else {
        pool = single_alloc_pool;
    }
    single_alloc_pool      = pool      + size;
    single_alloc_pool_size = pool_size - size;
    sx_SingleAllocMutex.Unlock();
    return pool;
}
Exemplo n.º 5
0
void
__c_node::__add(__i_node* i)
{
    if (end_ == cap_)
    {
        size_t nc = 2*static_cast<size_t>(cap_ - beg_);
        if (nc == 0)
            nc = 1;
        __i_node** beg =
           static_cast<__i_node**>(malloc(nc * sizeof(__i_node*)));
        if (beg == nullptr)
#ifndef _LIBCPP_NO_EXCEPTIONS
            throw bad_alloc();
#else
            abort();
#endif
        if (nc > 1)
            memcpy(beg, beg_, nc/2*sizeof(__i_node*));
        free(beg_);
        beg_ = beg;
        end_ = beg_ + nc/2;
        cap_ = beg_ + nc;
    }
    *end_++ = i;
}
Exemplo n.º 6
0
/**
 * Print error and and throw an exception
 * @throw bad_alloc
 * 
 * @param [in] FMT - format of error
 * @param [in] HDF5MatrixName - HDF5 dataset name
 * @param [in] File  File of error
 * @param [in] Line  Line of error
 * 
 */
void TMatrixContainer::PrintErrorAndThrowException(const char * FMT, const string HDF5MatrixName,           
                                          const char * File, const int Line){
    
     fprintf(stderr,FMT, HDF5MatrixName.c_str(), File, Line);                                
     throw bad_alloc();
    
}// end of PrintErrorAndAbort
Exemplo n.º 7
0
/* Construct a matching table for specified hashtable, original and new files. */
JMatchTable::JMatchTable (JHashPos const * const cpHsh,  JFile  * const apFilOrg, JFile  * const apFilNew, const bool abCmpAll)
: mpHsh(cpHsh), mpFilOrg(apFilOrg), mpFilNew(apFilNew), mbCmpAll(abCmpAll)
{
    // allocate table
	// TODO only allocate max number of elements
    msMch = (rMch *) malloc(sizeof(rMch) * MCH_MAX) ;
#ifndef __MINGW32__
    if ( msMch == null ) {
        throw bad_alloc() ;
    }
#endif

	// initialize linked list of free nodes
    for (int liIdx=0; liIdx < MCH_MAX - 1; liIdx++) {
        msMch[liIdx].ipNxt = &msMch[liIdx + 1];
    }
    msMch[MCH_MAX - 1].ipNxt = null ;
    mpMchFre = msMch ;

    memset(mpMch, 0, MCH_PME * sizeof(tMch *));

    // initialize other values
    mpMchGld = null;
    mzGldDlt = 0 ;
}
Exemplo n.º 8
0
__c_node*
__libcpp_db::__insert_c(void* __c)
{
#ifndef _LIBCPP_HAS_NO_THREADS
    WLock _(mut());
#endif
    if (__csz_ + 1 > static_cast<size_t>(__cend_ - __cbeg_))
    {
        size_t nc = __next_prime(2*static_cast<size_t>(__cend_ - __cbeg_) + 1);
        __c_node** cbeg = static_cast<__c_node**>(calloc(nc, sizeof(void*)));
        if (cbeg == nullptr)
#ifndef _LIBCPP_NO_EXCEPTIONS
            throw bad_alloc();
#else
            abort();
#endif
        for (__c_node** p = __cbeg_; p != __cend_; ++p)
        {
            __c_node* q = *p;
            while (q != nullptr)
            {
                size_t h = hash<void*>()(q->__c_) % nc;
                __c_node* r = q->__next_;
                q->__next_ = cbeg[h];
                cbeg[h] = q;
                q = r;
            }
        }
        free(__cbeg_);
        __cbeg_ = cbeg;
        __cend_ = __cbeg_ + nc;
    }
    size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
    __c_node* p = __cbeg_[hc];
    __c_node* r = __cbeg_[hc] =
      static_cast<__c_node*>(malloc(sizeof(__c_node)));
    if (__cbeg_[hc] == nullptr)
#ifndef _LIBCPP_NO_EXCEPTIONS
        throw bad_alloc();
#else
        abort();
#endif
    r->__c_ = __c;
    r->__next_ = p;
    ++__csz_;
    return r;
}
Exemplo n.º 9
0
	void EncryptionModeLRW::SetKey (const ConstBufferPtr &key)
	{
		if (key.Size() != 16)
			throw ParameterIncorrect (SRC_POS);

		if (!KeySet)
			GfContext.Allocate (sizeof (GfCtx));

		if (!Gf64TabInit ((unsigned char *) key.Get(), (GfCtx *) (GfContext.Ptr())))
			throw bad_alloc();

		if (!Gf128Tab64Init ((unsigned char *) key.Get(), (GfCtx *) (GfContext.Ptr())))
			throw bad_alloc();

		Key.CopyFrom (key);
		KeySet = true;
	}
Exemplo n.º 10
0
void
__throw_bad_alloc()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    throw bad_alloc();
#else
    _VSTD::abort();
#endif
}
Exemplo n.º 11
0
_LIBCPP_HIDDEN
__i_node*
__libcpp_db::__insert_iterator(void* __i)
{
    if (__isz_ + 1 > static_cast<size_t>(__iend_ - __ibeg_))
    {
        size_t nc = __next_prime(2*static_cast<size_t>(__iend_ - __ibeg_) + 1);
        __i_node** ibeg = static_cast<__i_node**>(calloc(nc, sizeof(void*)));
        if (ibeg == nullptr)
#ifndef _LIBCPP_NO_EXCEPTIONS
            throw bad_alloc();
#else
            abort();
#endif
        for (__i_node** p = __ibeg_; p != __iend_; ++p)
        {
            __i_node* q = *p;
            while (q != nullptr)
            {
                size_t h = hash<void*>()(q->__i_) % nc;
                __i_node* r = q->__next_;
                q->__next_ = ibeg[h];
                ibeg[h] = q;
                q = r;
            }
        }
        free(__ibeg_);
        __ibeg_ = ibeg;
        __iend_ = __ibeg_ + nc;
    }
    size_t hi = hash<void*>()(__i) % static_cast<size_t>(__iend_ - __ibeg_);
    __i_node* p = __ibeg_[hi];
    __i_node* r = __ibeg_[hi] =
      static_cast<__i_node*>(malloc(sizeof(__i_node)));
    if (r == nullptr)
#ifndef _LIBCPP_NO_EXCEPTIONS
        throw bad_alloc();
#else
        abort();
#endif
    ::new(r) __i_node(__i, p, nullptr);
    ++__isz_;
    return r;
}
Exemplo n.º 12
0
void	GUI_MemoryHog::our_new_handler()
{
	for(set<GUI_MemoryHog *>::iterator h = sHogs.begin(); h != sHogs.end(); ++h)
	{
		if ((*h)->ReleaseMemory())
			return;
	}
	if (sOldHandler)	sOldHandler();
	else				throw bad_alloc();
}
Exemplo n.º 13
0
  cn_context::cn_context() {
#if !defined(__APPLE__)
    data = mmap(nullptr, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
#else
    data = mmap(nullptr, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
#endif
    if (data == MAP_FAILED) {
      throw bad_alloc();
    }
    mlock(data, MAP_SIZE);
  }
Exemplo n.º 14
0
	void *Memory::Allocate (std::size_t size)
	{
		if (size < 1)
			throw ParameterIncorrect (SRC_POS);

		void *bufPtr = malloc (size);
		if (!bufPtr)
			throw bad_alloc();

		return bufPtr;
	}
Exemplo n.º 15
0
/**
 * Memory allocation based on the total number of elements. \n
 * Memory is aligned by the SSE_ALIGNMENT and all elements are zeroed.
 */
void TBaseLongMatrix::AllocateMemory(){
    /* No memory allocated before this function*/

    pMatrixData = (long *) memalign(SSE_ALIGNMENT,pTotalAllocatedElementCount * sizeof (long));
    
    if (!pMatrixData) {
        fprintf(stderr,Matrix_ERR_FMT_NotEnoughMemory, "TBaseLongMatrix");
        throw bad_alloc();   
    }    

    ZeroMatrix();
    
}// end of AllocateMemory
Exemplo n.º 16
0
__c_node*
__libcpp_db::__insert_c(void* __c)
{
    WLock _(mut());
    if (__csz_ + 1 > __cend_ - __cbeg_)
    {
        size_t nc = __next_prime(2*(__cend_ - __cbeg_) + 1);
        __c_node** cbeg = (__c_node**)calloc(nc, sizeof(void*));
        if (cbeg == nullptr)
            throw bad_alloc();
        for (__c_node** p = __cbeg_; p != __cend_; ++p)
        {
            __c_node* q = *p;
            while (q != nullptr)
            {
                size_t h = hash<void*>()(q->__c_) % nc;
                __c_node* r = q->__next_;
                q->__next_ = cbeg[h];
                cbeg[h] = q;
                q = r;
            }
        }
        free(__cbeg_);
        __cbeg_ = cbeg;
        __cend_ = __cbeg_ + nc;
    }
    size_t hc = hash<void*>()(__c) % (__cend_ - __cbeg_);
    __c_node* p = __cbeg_[hc];
    __c_node* r = __cbeg_[hc] = (__c_node*)malloc(sizeof(__c_node));
    if (__cbeg_[hc] == nullptr)
        throw bad_alloc();
    r->__c_ = __c;
    r->__next_ = p;
    ++__csz_;
    return r;
}
Exemplo n.º 17
0
void *FreeListNode::getBlock()
{
	ChunkHeader *chunk;
	void *block;

	// если chunk в m_freeList берем его оттуда и кладем в m_usedList
	if (m_freeList)
	{
		//1. берем chunk из m_freeList
		chunk = m_freeList;
		m_freeList = chunk->next;
		if (m_freeList)
			m_freeList->last = NULL;

//		кладем в m_usedList
		chunk->next = m_usedList;
		if (m_usedList) {
			m_usedList->last = chunk;
		}
		m_usedList = chunk;
		return (void *)((char *)m_usedList + sizeof(ChunkHeader));
	}
	// иначе создаем новой chunk и кледем его в m_usedList
	else
	{
		size_t chunkSize = sizeof(ChunkHeader) + m_blockSize;
		block = malloc(chunkSize);

		if (!block)
			throw bad_alloc();
		ChunkHeader *chunk = (ChunkHeader *)block;
		chunk->size = m_blockSize;

		if (m_usedList)
		{
			m_usedList->last = chunk;
			chunk->next = m_usedList;
			chunk->last = NULL;
			m_usedList = chunk;
		}
		else
		{
			chunk->last = chunk->next = (ChunkHeader*) NULL;
			m_usedList = chunk;
		}
	}
	return (void *)((char*)block + sizeof(ChunkHeader));
}
Exemplo n.º 18
0
/// \brief Reallocates internal block to hold at least \p newSize bytes.
///
/// Additional memory may be allocated, but for efficiency it is a very
/// good idea to call reserve before doing byte-by-byte edit operations.
/// The block size as returned by size() is not altered. reserve will not
/// reduce allocated memory. If you think you are wasting space, call
/// deallocate and start over. To avoid wasting space, use the block for
/// only one purpose, and try to get that purpose to use similar amounts
/// of memory on each iteration.
///
void memblock::reserve (size_type newSize, bool bExact)
{
    if ((newSize += minimumFreeCapacity()) <= m_Capacity)
	return;
    pointer oldBlock (is_linked() ? NULL : data());
    const size_t alignedSize (Align (newSize, 64));
    if (!bExact)
	newSize = alignedSize;
    pointer newBlock = (pointer) realloc (oldBlock, newSize);
    if (!newBlock)
	USTL_THROW(bad_alloc (newSize));
    if (!oldBlock & (cdata() != NULL))
	copy_n (cdata(), min (size() + 1, newSize), newBlock);
    link (newBlock, size());
    m_Capacity = newSize;
}
Exemplo n.º 19
0
float *getFFT(const void *matData, const Size &imSz, const fftwf_plan &plan) {
	float *fft = (float *)fftwf_malloc_thr(sizeof(float) * getFFTLen(imSz));
	if (!fft) {
		throw bad_alloc();
	}

	int inI = 0, outI = 0;
	for (int r = 0; r < imSz.height; r++) {
		for (int c = 0; c < imSz.width; c++) {
			fft[outI++] = ((const float *)matData)[inI++];
			if (((const float *)matData)[inI - 1] != ((const float *)matData)[inI - 1]) {
				//printf(" UH UH %d %d\n", r, c);
			}
		}
		fft[outI++] = 0;	// zero-pad twice
		fft[outI++] = 0;
	}

	fftwf_execute_dft_r2c(plan, fft, (fftwf_complex *)fft);

	// testing
//	static mutex mut;
//	mut.lock();
//	Mat im(imSz, CV_32FC1, (void *)matData);
//
//	double minVal, maxVal;
//	Point minLoc, maxLoc;
//
//	minMaxLoc(im, &minVal, &maxVal, &minLoc, &maxLoc);
//	printf("min: %lf, (%d, %d)\n", minVal, minLoc.x, minLoc.y);
//	printf("max: %lf, (%d, %d)\n", maxVal, maxLoc.x, maxLoc.y);
//	imshow("hn", im);
//	waitKey(0);
//	mut.unlock();
//
//	Mat matDatam(imSz.height, imSz.width+2, CV_32F, (float *)fft);
//	minMaxLoc(matDatam, &minVal, &maxVal, &minLoc, &maxLoc);
//	printf("min: %lf, (%d, %d)\n", minVal, minLoc.x, minLoc.y);
//	printf("max: %lf, (%d, %d)\n", maxVal, maxLoc.x, maxLoc.y);
//
//	imshow("immmmm", matDatam);
//	waitKey(0);
	//

	return fft;
}
Exemplo n.º 20
0
	AutoMatrix& AutoMatrix::operator= ( const AutoMatrix& original ) {
		if ( this != &original ) {
			const size_t
				rows = original.countRows(),
				cols = original.countColumns();
			const size_t used = calculateSize( rows, cols );
			double* newArray;
			if ( used <= size ) {
				newArray = matrix.data;
			} else {
				newArray = static_cast<double*>( malloc( used ) );
				if ( NULL == newArray ) {
					throw bad_alloc();
				}
				free( matrix.data );
				matrix.data = newArray;
				size = used;
			}
			gslInit(
				rows,
				cols,
				newArray,
				calculateSize( rows, original.matrix.tda ) <= size ? original.matrix.tda : cols
				// try to copy tda
			);
			// the following copy is like in the copy constructor
			if ( cols == original.matrix.tda ) {
				// copy one big lump
				memcpy( matrix.data, original.matrix.data, used );
			} else {
				// copy row by row
				const size_t blockSize = cols * sizeof( double );
				const double *source = original.matrix.data;
				double *target = matrix.data;
				for ( size_t row = 0; row < rows; ++row ) {
					memcpy( target, source, blockSize );
					source += original.matrix.tda;
					target += matrix.tda;
				}
			}
		}
		return *this;
	}
Exemplo n.º 21
0
/// \brief Reallocates internal block to hold at least \p newSize bytes.
///
/// Additional memory may be allocated, but for efficiency it is a very
/// good idea to call reserve before doing byte-by-byte edit operations.
/// The block size as returned by size() is not altered. reserve will not
/// reduce allocated memory. If you think you are wasting space, call
/// deallocate and start over. To avoid wasting space, use the block for
/// only one purpose, and try to get that purpose to use similar amounts
/// of memory on each iteration.
///
void memblock::reserve (size_type newSize, bool bExact)
{
    if ((newSize += minimumFreeCapacity()) <= m_Capacity)
	return;
    void* oldBlock (is_linked() ? NULL : data());
    if (!bExact)
	newSize = Align (newSize, c_PageSize);
    pointer newBlock = (pointer) realloc (oldBlock, newSize);
    if (!newBlock)
#if PLATFORM_ANDROID
        printf("bad_alloc\n");
#else
	throw bad_alloc (newSize);
#endif
    if (!oldBlock && cdata())
	copy_n (cdata(), min (size() + 1, newSize), newBlock);
    link (newBlock, size());
    m_Capacity = newSize;
}
 _CONCRTIMP void* NFS_Allocate( size_t n, size_t element_size, void*)
 {
     size_t m = NFS_LineSize;
     _CONCRT_ASSERT( m<=NFS_MaxLineSize ); // illegal value for NFS_LineSize
     _CONCRT_ASSERT( (m & m-1)==0 ); // must be power of two
     size_t bytes = n*element_size;
     unsigned char* base;
     if( bytes<n || bytes+m<bytes || !(base=(unsigned char*)(malloc(m+bytes))) )
     {
         // Overflow
         throw bad_alloc();
     }
     // Round up to next line
     unsigned char* result = (unsigned char*)((size_t)(base+m)&-m);
     // Record where block actually starts.
     ((size_t*)result)[-1] = size_t(base);
     _CONCRT_ASSERT( ((size_t)result&(m-1)) == 0 );  // The address returned isn't aligned to cache line size
     return result;
 }
Exemplo n.º 23
0
MP_Var_Array_c<TYPE>& MP_Var_Array_c<TYPE>::operator=(const MP_Var_Array_c<TYPE>& cSource){
    TYPE* tmp;
    // check for self-assignment
    if (this == &cSource)
        return *this;
	// First we need to copy the elements
	nElem = cSource.nElem;
	maxNElem = cSource.maxNElem;
	blockSize = cSource.blockSize;
	// Second we need to allocate memory for our copy
	tmp = (TYPE*) malloc((maxNElem+blockSize)*sizeof(TYPE));
	if(tmp == NULL)
		throw bad_alloc();
    // Third we need to deallocate any value that elem is holding!
    free(elem);
    // allocate memory for our copy
	elem = tmp;
	// Copy the parameter the newly allocated memory
	memcpy(elem, cSource.elem, nElem*sizeof(TYPE));
	return *this;
}
Exemplo n.º 24
0
_LIBCPP_HIDDEN
void
__c_node::__add(__i_node* i)
{
    if (end_ == cap_)
    {
        size_t nc = 2*(cap_ - beg_);
        if (nc == 0)
            nc = 1;
        __i_node** beg = (__i_node**)malloc(nc * sizeof(__i_node*));
        if (beg == nullptr)
            throw bad_alloc();
        if (nc > 1)
            memcpy(beg, beg_, nc/2*sizeof(__i_node*));
        free(beg_);
        beg_ = beg;
        end_ = beg_ + nc/2;
        cap_ = beg_ + nc;
    }
    *end_++ = i;
}
Exemplo n.º 25
0
void *operator new[](size_t i)
{
	i += sizeof(MemoryRecord);

	BufferID id;
	void *p = NULL;

	if(pBufferSuite != NULL)
	{
		SPErr iErr = pBufferSuite->allocateProc(i, &id);

		if(!iErr)
		{
			p = pBufferSuite->lockProc(id, 0);
		}
	}
	else
	{
		p = malloc(i);
	}

	if(p == NULL)
	{
#if defined(DEBUG)
		printf("Allocation failure of %i bytes, current allocation %i, peak %i\n", i, g_iTotal, g_iPeak);
#endif
		throw bad_alloc();
	}

	MemoryRecord *pRec = (MemoryRecord *) p;
	pRec->id = id;
#if defined(DEBUG)
	pRec->iSize = i;
	AdjustMemory(pRec, true);
#endif

	return pRec + 1;
}
Exemplo n.º 26
0
void Stack::push(int a){
	if(height == 0){
		tab = new int[1];
		tab[0] = a;
		height++;		
		}
	else
		{
		int* temp = new int[height +1];
		if (temp == NULL)
			throw bad_alloc();

		memcpy(temp + 1, tab, height*sizeof(int));

		temp[0] = a;
		delete[] tab;
		tab = temp;
		height++;
		}



	}
Exemplo n.º 27
0
void Stack::pop(){
	if(height == 0)
		{
		return;
		}
	if(height == 1)
		{
		height--;
		delete[] tab;
		tab = NULL;
		}
	else
		{
		int* temp = new int[height - 1];
		if(temp == NULL)
			throw bad_alloc();

		memcpy(temp, tab + 1 , (height-1) * sizeof(int));
		delete[] tab;
		tab = temp;
		height--;
		}
	
	}
Exemplo n.º 28
0
void __throw_bad_alloc() { throw bad_alloc(); }
Exemplo n.º 29
0
void *operator new(size_t size) {
  void *p = malloc(size);
  if (!p)
    throw bad_alloc();
  return p;
}
Exemplo n.º 30
0
// Returns 0 if everything is OK
// Returns -1 if can't find the file
// Returns -2 if the file is an invalid bitmap
// Returns -3 if the size doesn't match
// Returns -4 if the bpp doesn't match
int update_bitmap(CResourceEditor* re, WORD id, char* filename, int width/*=0*/, int height/*=0*/, int maxbpp/*=0*/) {
  FILE *f = fopen(filename, "rb");
  if (!f) return -1;

  if (fgetc(f) != 'B' || fgetc(f) != 'M') {
    fclose(f);
    return -2;
  }

  if (width != 0) {
    LONG biWidth;
    fseek(f, 18, SEEK_SET); // Seek to the width member of the header
    fread(&biWidth, sizeof(LONG), 1, f);
    if (width != biWidth) {
      fclose(f);
      return -3;
    }
  }

  if (height != 0) {
    LONG biHeight;
    fseek(f, 22, SEEK_SET); // Seek to the height member of the header
    fread(&biHeight, sizeof(LONG), 1, f);
    // Bitmap height can be negative too...
    if (height != abs(biHeight)) {
      fclose(f);
      return -3;
    }
  }

  if (maxbpp != 0) {
    WORD biBitCount;
    fseek(f, 28, SEEK_SET); // Seek to the height member of the header
    fread(&biBitCount, sizeof(WORD), 1, f);
    if (biBitCount > maxbpp) {
      fclose(f);
      return -4;
    }
  }

  DWORD dwSize;
  fseek(f, 2, SEEK_SET);
  fread(&dwSize, sizeof(DWORD), 1, f);
  dwSize -= 14;

  unsigned char* bitmap = (unsigned char*)malloc(dwSize);
  if (!bitmap) throw bad_alloc();

  fseek(f, 14, SEEK_SET);
  if (fread(bitmap, 1, dwSize, f) != dwSize) {
    fclose(f);
    return -2;
  }
  fclose(f);

  re->UpdateResource(RT_BITMAP, MAKEINTRESOURCE(id), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), bitmap, dwSize);

  free(bitmap);

  return 0;
}