mirror of
https://github.com/ptitSeb/Serious-Engine
synced 2024-12-01 22:25:53 +01:00
24cb244d43
This was a _ton_ of changes, made 15 years ago, so there are probably some problems to work out still. Among others: Engine/Base/Stream.* was mostly abandoned and will need to be re-ported. Still, this is a pretty good start, and probably holds a world record for lines of changes or something. :)
182 lines
4.0 KiB
C++
182 lines
4.0 KiB
C++
/* Copyright (c) 2002-2012 Croteam Ltd. All rights reserved. */
|
|
|
|
#include "Engine/StdH.h"
|
|
|
|
#include <Engine/Base/Memory.h>
|
|
#include <Engine/Base/Translation.h>
|
|
|
|
#include <Engine/Base/ErrorReporting.h>
|
|
|
|
/* rcg10282005 "new.h" is deprecated in newer C++ standards... --ryan. */
|
|
#ifdef _MSC_VER
|
|
#include <new.h>
|
|
#else
|
|
#include <new>
|
|
#endif
|
|
|
|
FLOAT _bCheckAllAllocations = FALSE;
|
|
|
|
#ifdef PLATFORM_WIN32
|
|
/*
|
|
* Declarations for setting up the 'new_handler'.
|
|
*/
|
|
_CRTIMP int __cdecl _query_new_mode( void );
|
|
_CRTIMP int __cdecl _set_new_mode( int );
|
|
|
|
#ifndef _PNH_DEFINED
|
|
typedef int (__cdecl * _PNH)( size_t );
|
|
#define _PNH_DEFINED
|
|
#endif
|
|
|
|
/*_CRTIMP _PNH __cdecl _query_new_handler( void );
|
|
_CRTIMP _PNH __cdecl _set_new_handler( _PNH );*/
|
|
|
|
#ifndef NDEBUG
|
|
// include this for debug version of operator new
|
|
#pragma comment (lib, "msvcrtd.lib")
|
|
#endif
|
|
|
|
// include user32 library (because of message box)
|
|
#pragma comment (lib, "user32.lib")
|
|
#endif
|
|
|
|
|
|
// if not enough memory
|
|
#ifdef _MSC_VER
|
|
int NewHandler(size_t size)
|
|
{
|
|
// terminate program
|
|
FatalError(TRANS("Not enough memory (%d bytes needed)!"), size);
|
|
return 0;
|
|
}
|
|
#else
|
|
void NewHandler(void)
|
|
{
|
|
// terminate program
|
|
FatalError(TRANS("Not enough memory!"));
|
|
}
|
|
#define _CrtCheckMemory()
|
|
#endif
|
|
|
|
/* Static class used for initializing memory handlers. */
|
|
static class CMemHandlerInit {
|
|
public:
|
|
// constructor
|
|
CMemHandlerInit(void);
|
|
} MemHandlerInit;
|
|
|
|
CMemHandlerInit::CMemHandlerInit(void)
|
|
{
|
|
// set our not-enough-memory handler
|
|
_set_new_handler(NewHandler);
|
|
// make malloc use that handler
|
|
#ifdef _MSC_VER
|
|
_set_new_mode(1);
|
|
#endif
|
|
}
|
|
|
|
#undef AllocMemory
|
|
|
|
void *AllocMemory( SLONG memsize )
|
|
{
|
|
void *pmem;
|
|
ASSERTMSG(memsize>0, "AllocMemory: Block size is less or equal zero.");
|
|
if (_bCheckAllAllocations) {
|
|
_CrtCheckMemory();
|
|
}
|
|
pmem = malloc( memsize);
|
|
// memory handler asures no null results here?!
|
|
if (pmem==NULL) {
|
|
_CrtCheckMemory();
|
|
FatalError(TRANS("Not enough memory (%d bytes needed)!"), memsize);
|
|
}
|
|
return pmem;
|
|
}
|
|
|
|
#ifdef _MSC_VER
|
|
#ifndef NDEBUG
|
|
void *_debug_AllocMemory( SLONG memsize, int iType, const char *strFile, int iLine)
|
|
{
|
|
void *pmem;
|
|
ASSERTMSG(memsize>0, "AllocMemory: Block size is less or equal zero.");
|
|
|
|
if (_bCheckAllAllocations) {
|
|
_CrtCheckMemory();
|
|
}
|
|
pmem = _malloc_dbg( memsize, iType, strFile, iLine);
|
|
// memory handler asures no null results here?!
|
|
if (pmem==NULL) {
|
|
_CrtCheckMemory();
|
|
FatalError(TRANS("Not enough memory (%d bytes needed)!"), memsize);
|
|
}
|
|
return pmem;
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
void *AllocMemoryAligned( SLONG memsize, SLONG slAlignPow2)
|
|
{
|
|
ULONG ulMem = (ULONG)AllocMemory(memsize+slAlignPow2*2);
|
|
ULONG ulMemAligned = ((ulMem+slAlignPow2-1) & ~(slAlignPow2-1)) + slAlignPow2;
|
|
((ULONG *)ulMemAligned)[-1] = ulMem;
|
|
return (void*)ulMemAligned;
|
|
}
|
|
void FreeMemoryAligned( void *memory)
|
|
{
|
|
FreeMemory((void*) ( ( (ULONG*)memory )[-1] ) );
|
|
}
|
|
|
|
void FreeMemory( void *memory )
|
|
{
|
|
ASSERTMSG(memory!=NULL, "FreeMemory: NULL pointer input.");
|
|
free( (char *)memory);
|
|
}
|
|
|
|
void ResizeMemory( void **ppv, SLONG slSize )
|
|
{
|
|
if (_bCheckAllAllocations) {
|
|
_CrtCheckMemory();
|
|
}
|
|
void *pv = realloc(*ppv, slSize);
|
|
// memory handler asures no null results here?!
|
|
if (pv==NULL) {
|
|
_CrtCheckMemory();
|
|
FatalError(TRANS("Not enough memory (%d bytes needed)!"), slSize);
|
|
}
|
|
*ppv = pv;
|
|
}
|
|
|
|
void GrowMemory( void **ppv, SLONG newSize )
|
|
{
|
|
ResizeMemory(ppv, newSize);
|
|
}
|
|
|
|
void ShrinkMemory( void **ppv, SLONG newSize )
|
|
{
|
|
ResizeMemory(ppv, newSize);
|
|
}
|
|
|
|
|
|
/*
|
|
* Allocate a copy of a string. - fatal error if not enough memory.
|
|
*/
|
|
char *StringDuplicate(const char *strOriginal) {
|
|
// get the size
|
|
SLONG slSize = strlen(strOriginal)+1;
|
|
// allocate that much memory
|
|
char *strCopy = (char *)AllocMemory(slSize);
|
|
// copy it there
|
|
memcpy(strCopy, strOriginal, slSize);
|
|
// result is the pointer to the copied string
|
|
return strCopy;
|
|
}
|
|
|
|
|
|
|
|
// return position where we encounter zero byte or iBytes
|
|
INDEX FindZero( UBYTE *pubMemory, INDEX iBytes)
|
|
{
|
|
for( INDEX i=0; i<iBytes; i++) if( pubMemory[i]==0) return i;
|
|
return iBytes;
|
|
}
|