summaryrefslogtreecommitdiff
path: root/src/CustomMemory.cpp
blob: c8102aef44674fe17303d660eec0404e5c2fa5f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "Internal.h"
#include <cstdlib>

#if defined(TARGET_PLATFORM_LINUX)
#include <malloc.h>
#endif
#include "InternalLogger.h"

#if (defined(TARGET_PLATFORM_MACOSX) || defined(TARGET_PLATFORM_ANDROID))
#define _THROW0()
#endif

#if defined(TARGET_PLATFORM_NIXLIKE)
#if defined(TARGET_PLATFORM_MACOSX)
namespace
{
    void* default_aligned_malloc(size_t size, size_t alignment)
    {
        void* aPtr;
        if (posix_memalign (&aPtr, alignment, size))
        {
            aPtr = NULL;
        }
        return aPtr;
    }
}
#else
namespace
{
    void* default_aligned_malloc(size_t size, size_t alignment)
    {
        return memalign(alignment, size);
    }
}
#endif
GFSDK_WAVEWORKS_ALIGNED_MALLOC			NVSDK_aligned_malloc    = default_aligned_malloc;
GFSDK_WAVEWORKS_ALIGNED_FREE			NVSDK_aligned_free		= free;
#else
GFSDK_WAVEWORKS_ALIGNED_MALLOC			NVSDK_aligned_malloc	= _aligned_malloc;
GFSDK_WAVEWORKS_ALIGNED_FREE			NVSDK_aligned_free		= _aligned_free;
#endif

#if defined(TARGET_PLATFORM_PS4)
namespace
{
	void* malloc_wrapper(size_t size)
	{
		return NVSDK_aligned_malloc(size, 8);
	}

	void free_wrapper(void* ptr)
	{
		NVSDK_aligned_free(ptr);
	}
}

GFSDK_WAVEWORKS_MALLOC					NVSDK_malloc			= malloc_wrapper; // never changes
GFSDK_WAVEWORKS_FREE					NVSDK_free				= free_wrapper; // never changes
GFSDK_WAVEWORKS_ALIGNED_MALLOC			NVSDK_garlic_malloc     = default_aligned_malloc;
GFSDK_WAVEWORKS_ALIGNED_FREE			NVSDK_garlic_free		= free;
#define DELETE_THROWSPEC() _THROW0()
#else
GFSDK_WAVEWORKS_MALLOC					NVSDK_malloc			= malloc;
GFSDK_WAVEWORKS_FREE					NVSDK_free				= free;
#define DELETE_THROWSPEC() _THROW0()
#endif

void* internalMalloc( size_t size )
{
    void* p = NVSDK_malloc( size );
	if (!p)
		NV_ERROR(TEXT("WaveWorks: MEMORY ALLOCATION ERROR. Check memory allocation callback pointer\n"));
//        diagnostic_message( TEXT("WaveWorks: MEMORY ALLOCATION ERROR. Check memory allocation callback pointer\n") );
    return p;
}

void internalFree( void* p )
{
    NVSDK_free( p );
}


void* __CRTDECL operator new(size_t size)
{
	return internalMalloc( size );
}
void __CRTDECL operator delete(void *p) DELETE_THROWSPEC()
{
	internalFree( p );
}

void * __CRTDECL operator new[](size_t size)
{
	return internalMalloc( size );
}

void __CRTDECL operator delete[](void *p) DELETE_THROWSPEC()
{
	internalFree( p );
}

void resetMemoryManagementCallbacksToDefaults()
{
#if defined(TARGET_PLATFORM_PS4)
	NVSDK_aligned_malloc	= default_aligned_malloc;
	NVSDK_aligned_free		= free;
	NVSDK_garlic_malloc		= default_aligned_malloc;
	NVSDK_garlic_free		= free;
#elif defined(TARGET_PLATFORM_NIXLIKE)
	NVSDK_malloc			= malloc;
	NVSDK_free				= free;
	NVSDK_aligned_malloc	= default_aligned_malloc;
	NVSDK_aligned_free		= free;
#else
	NVSDK_malloc			= malloc;
	NVSDK_free				= free;
	NVSDK_aligned_malloc	= _aligned_malloc;
	NVSDK_aligned_free		= _aligned_free;
#endif
}