Memory Routines << Back



Without using CRT, you might have to re-define a few CRT routines on your own. The collection of code is from these web sites


extern "C"
{
	_CRT_INSECURE_DEPRECATE_MEMORY(memcpy_s)
		_Post_equal_to_(_Dst)
		_At_buffer_(
			(unsigned char*)_Dst,
			_Iter_,
			_Size,
			_Post_satisfies_(((unsigned char*)_Dst)[_Iter_] == ((unsigned char*)_Src)[_Iter_])
		)
	void* __cdecl memcpy(_Out_writes_bytes_all_(_Size) void* _Dst, _In_reads_bytes_(_Size) const void* _Src, _In_ size_t _Size)
	{
		char** pDst = reinterpret_cast(&_Dst); // Get around VS 2022 Warning of using uninitialized variable
		char* dest8 = *pDst;
		const char* src8 = reinterpret_cast(_Src);
		while (_Size--)
			*dest8++ = *src8++;
		return _Dst;
	}
	
    #pragma function(memset)
    _Post_equal_to_(_Dst)
        _At_buffer_(
            (unsigned char*)_Dst,
            _Iter_,
            _Size,
            _Post_satisfies_(((unsigned char*)_Dst)[_Iter_] == _Val)
        )
    void* __cdecl memset(_Out_writes_bytes_all_(_Size) void* _Dst, _In_ int _Val, _In_ size_t _Size)
    {
        char** pDst = reinterpret_cast(&_Dst); // Get around VS 2022 Warning of using uninitialized variable
        char* bytes = *pDst;
        while (_Size--)
        {
            *bytes++ = (char)_Val;
        }
        return _Dst;
    }
}

void* __cdecl operator new(unsigned int cb) { return HeapAlloc(GetProcessHeap(), 0, cb); }
void __cdecl operator delete(void* pv, unsigned int cb) { if (pv) HeapFree(GetProcessHeap(), 0, pv); }

#ifdef FillMemory
#undef FillMemory
#endif
#ifdef ZeroMemory
#undef ZeroMemory
#endif
#ifdef CopyMemory
#undef CopyMemory
#endif
#ifdef MoveMemory
#undef MoveMemory
#endif

__forceinline
void CopyMemory(void* dest, const void* src, unsigned __int32 count)
{
	unsigned __int32	size32 = count >> 2;
	unsigned __int32* dest32 = (unsigned __int32*)dest;
	unsigned __int32* src32 = (unsigned __int32*)src;

	switch ((count - (size32 << 2)))
	{
	case 3:	((unsigned __int8*)dest)[count - 3] =
		((unsigned __int8*)src)[count - 3];
	case 2:	((unsigned __int8*)dest)[count - 2] =
		((unsigned __int8*)src)[count - 2];
	case 1:	((unsigned __int8*)dest)[count - 1] =
		((unsigned __int8*)src)[count - 1];
	}

	while (size32-- > 0)
		*(dest32++) = *(src32++);

}  /* CopyMemory */

__forceinline
void FillMemory(void *dest,unsigned __int32 count,unsigned __int8 c)
{
	unsigned __int32	size32=count>>2;
	unsigned __int32	fill=(c<<24|c<<16|c<<8|c);
	unsigned __int32	*dest32=(unsigned __int32*)dest;

	switch( (count-(size32<<2)) )
	{
	case 3: ((unsigned __int8*)dest)[count - 3] = c;
	case 2: ((unsigned __int8*)dest)[count - 2] = c;
	case 1: ((unsigned __int8*)dest)[count - 1] = c;
	}

	while( size32-- > 0 )
		*(dest32++) = fill;

}  /* FillMemory */

#define ZeroMemory(dest,count) FillMemory(dest,count,0)

__forceinline
void MoveMemory(void* dest, const void* src, unsigned __int32 count)
{
	unsigned __int32	size32 = count >> 2, i;
	unsigned __int32* dest32 = (unsigned __int32*)dest;
	unsigned __int32* src32 = (unsigned __int32*)src;

	if (dest > src)
	{
		switch ((count - (size32 << 2)))
		{
		case 3:	((unsigned __int8*)dest)[count - 1] =
			((unsigned __int8*)src)[count - 1];
			((unsigned __int8*)dest)[count - 2] =
				((unsigned __int8*)src)[count - 2];
			((unsigned __int8*)dest)[count - 3] =
				((unsigned __int8*)src)[count - 3];
			break;
		case 2:	((unsigned __int8*)dest)[count - 1] =
			((unsigned __int8*)src)[count - 1];
			((unsigned __int8*)dest)[count - 2] =
				((unsigned __int8*)src)[count - 2];
			break;
		case 1:	((unsigned __int8*)dest)[count - 1] =
			((unsigned __int8*)src)[count - 1];
			break;
		}

		while (size32-- > 0)
			dest32[size32] = src32[size32];
	}
	else
	{
		for (i = 0; i < size32; i++)
			*(dest32++) = *(src32++);

		switch ((count - (size32 << 2)))
		{
		case 3:	*(dest32++) = *(src32++);
		case 2:	*(dest32++) = *(src32++);
		case 1:	*(dest32++) = *(src32++);
		}
	}
}




Free Web Hosting