コード例 #1
0
ファイル: smalheap.c プロジェクト: mysticTot/learn_c
void* __cdecl realloc(
    void* pblock,
    size_t newsize
) {
    void* retp;

    /* if pblock is NULL, call malloc */
    if (pblock == (void*) NULL) {
        return malloc(newsize);
    }

    /* if pblock is !NULL and size is 0, call free and return NULL */
    if (newsize == 0) {
        free(pblock);
        return NULL;
    }

    for (;;) {
        retp = HeapReAlloc(_crtheap, 0, pblock, newsize);

        if (retp || _newmode == 0) {
            return retp;
        }

        /* call installed new handler */
        if (!_callnewh(newsize)) {
            return NULL;
        }

        /* new handler was successful -- try to allocate again */
    }
}
コード例 #2
0
ファイル: smalheap.c プロジェクト: mysticTot/learn_c
void* __cdecl _nh_malloc(
    size_t size,
    int nhFlag
) {
    void* retp;

    for (;;) {
        retp = HeapAlloc(_crtheap, 0, size);

        /*
         * if successful allocation, return pointer to memory
         * if new handling turned off altogether, return NULL
         */

        if (retp || nhFlag == 0) {
            return retp;
        }

        /* call installed new handler */
        if (!_callnewh(size)) {
            return NULL;
        }

        /* new handler was successful -- try to allocate again */
    }
}
コード例 #3
0
void * operator new( size_t cb )
{
    void *res;

    for (;;) {

        //  allocate memory block
        res = malloc(cb);

        //  if successful allocation, return pointer to memory

        if (res)
            break;

        //  call installed new handler
        if (!_callnewh(cb))
            break;

        //  new handler was successful -- try to allocate again
    }

    RTCCALLBACK(_RTC_Allocate_hook, (res, cb, 0));

    return res;
}
コード例 #4
0
ファイル: debug.c プロジェクト: AmesianX/RosWine
/*********************************************************************
 *		??2@YAPAXIHPBDH@Z (MSVCRTD.@)
 */
void * CDECL MSVCRTD_operator_new_dbg(MSVCRT_size_t nSize, int nBlockUse,
                                      const char *szFileName, int nLine)
{
    void *retval = NULL;

    TRACE("(%lu, %d, '%s', %d)\n", nSize, nBlockUse, szFileName, nLine);

    switch(_BLOCK_TYPE(nBlockUse))
    {
    case _NORMAL_BLOCK:
        break;
    case _CLIENT_BLOCK:
        FIXME("Unimplemented case for nBlockUse = _CLIENT_BLOCK\n");
        return NULL;
    case _FREE_BLOCK:
        FIXME("Native code throws an exception here\n");
    case _CRT_BLOCK:
    case _IGNORE_BLOCK:
        ERR("Not allowed nBlockUse value: %d\n", _BLOCK_TYPE(nBlockUse));
        return NULL;
    default:
        ERR("Unknown nBlockUse value: %d\n", _BLOCK_TYPE(nBlockUse));
        return NULL;
    }

    retval = HeapAlloc(GetProcessHeap(), 0, nSize);

    if (!retval)
        _callnewh(nSize);

    return retval;
}
コード例 #5
0
ファイル: newop.cpp プロジェクト: miaowangtw/Old-Codes
void *__CRTDECL operator new(size_t count) _THROW1(_STD bad_alloc)
	{	// try to allocate size bytes
	void *p;
	while ((p = malloc(count)) == 0)
		if (_callnewh(count) == 0)
			{	// report no memory
			_STD _Xbad_alloc();
			}
	return (p);
	}
コード例 #6
0
ファイル: new.cpp プロジェクト: jetlive/skiaming
void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
        {       // try to allocate size bytes
        void *p;
        while ((p = malloc(size)) == 0)
                if (_callnewh(size) == 0)
                {       // report no memory
                static const std::bad_alloc nomem;
                _RAISE(nomem);
                }

        return (p);
        }
コード例 #7
0
void *operator new(size_t size, const std::nothrow_t&) _THROW0()
	{	// try to allocate size bytes
	void *p;
	while ((p = malloc(size)) == 0)
		{	// buy more memory or return null pointer
		_TRY_BEGIN
			if (_callnewh(size) == 0)
				break;
		_CATCH(std::bad_alloc)
			return (0);
		_CATCH_END
		}
	return (p);
	}
コード例 #8
0
void * __cdecl _calloc_base (size_t num, size_t size)
{
    size_t  size_sbh;
    void *  pvReturn;

    size_sbh = size = size * num;


    /* round up to the nearest paragraph */
    if (size <= _HEAP_MAXREQ)
    {
        if (size == 0)
            size = 1;
        size = (size + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1);
    }

    for (;;)
    {
        pvReturn = NULL;

        if (size <= _HEAP_MAXREQ)
        {
            if (size_sbh <= __sbh_threshold)
            {
                //  Allocate the block from the small-block heap and
                //  initialize it with zeros.
                _mlock(_HEAP_LOCK);
                pvReturn = __sbh_alloc_block(size_sbh);
                _munlock(_HEAP_LOCK);

                if (pvReturn != NULL)
                    memset(pvReturn, 0, size_sbh);
            }

            if (pvReturn == NULL)
                pvReturn = HeapAlloc(_crtheap, HEAP_ZERO_MEMORY, size);
        }

        if (pvReturn || _newmode == 0)
            return pvReturn;

        /* call installed new handler */
        if (!_callnewh(size))
            return NULL;

        /* new handler was successful -- try to allocate again */
    }

}
コード例 #9
0
ファイル: smalheap.c プロジェクト: mysticTot/learn_c
void* __cdecl calloc(
    size_t num,
    size_t size
) {
    void* retp;
    size *= num;

    for (;;) {
        retp = HeapAlloc(_crtheap, HEAP_ZERO_MEMORY, size);

        if (retp || _newmode == 0) {
            return retp;
        }

        /* call installed new handler */
        if (!_callnewh(size)) {
            return NULL;
        }

        /* new handler was successful -- try to allocate again */
    }
}
コード例 #10
0
// This function implements the logic of realloc().  It is called directly by
// the realloc() and _recalloc() functions in the Release CRT and is called by
// the debug heap in the Debug CRT.
extern "C" _CRTRESTRICT void* __cdecl _realloc_base(
    void*  const block,
    size_t const size
    )
{
    // If the block is a nullptr, just call malloc:
    if (block == nullptr)
        return _malloc_base(size);

    // If the new size is 0, just call free and return nullptr:
    if (size == 0)
    {
        _free_base(block);
        return nullptr;
    }

    // Ensure that the requested size is not too large:
    _VALIDATE_RETURN_NOEXC(_HEAP_MAXREQ >= size, ENOMEM, nullptr);

    for (;;)
    {
        void* const new_block = HeapReAlloc(__acrt_heap, 0, block, size);
        if (new_block)
            return new_block;

        // Otherwise, see if we need to call the new handler, and if so call it.
        // If the new handler fails, just return nullptr:
        if (_query_new_mode() == 0 || !_callnewh(size))
        {
            errno = ENOMEM;
            return nullptr;
        }

        // The new handler was successful; try to allocate again...
    }
}