summaryrefslogtreecommitdiff
path: root/XMPCommon/source/MemoryAllocatorWrapperImpl.cpp
blob: 8ffc33c3c48a292ce133fa759b903ef3a2e52220 (plain)
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
// =================================================================================================
// Copyright Adobe
// Copyright 2015 Adobe
// All Rights Reserved
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it. If you have received this file from a source other 
// than Adobe, then your use, modification, or distribution of it requires the prior written permission
// of Adobe.
// =================================================================================================

#define IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED 1
	#include "XMPCommon/ImplHeaders/MemoryAllocatorWrapperImpl.h"
#undef IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED

#include <cstdlib>

namespace XMP_COMPONENT_INT_NAMESPACE {

	using AdobeXMPCommon::sizet;

	static const size_t kSIZE_OF_POINTER = sizeof( void * );
	static const size_t kOFFSET = ( ( kSIZE_OF_POINTER + sizeof( sizet ) - 1 ) / sizeof( sizet ) )
		* sizeof( sizet );

	MemoryAllocatorWrapperImpl::MemoryAllocatorWrapperImpl()
		: mpMemoryAllocator( NULL ) { }

	AdobeXMPCommon::pIMemoryAllocator_base MemoryAllocatorWrapperImpl::SetMemoryAllocator( pIMemoryAllocator_base memoryAllocator ) {
		auto retValue = mpMemoryAllocator;
		mpMemoryAllocator = memoryAllocator;
		return retValue;
	}

	void * APICALL MemoryAllocatorWrapperImpl::allocate( sizet size ) __NOTHROW__ {
		size_t actualSize = size + kOFFSET;
		void * memPtr = NULL;
		if ( mpMemoryAllocator )
			memPtr = mpMemoryAllocator->allocate( actualSize );
		else
			memPtr = malloc( actualSize );

		if ( memPtr ) {
			IMemoryAllocator ** address = ( IMemoryAllocator ** ) memPtr;
			*address = mpMemoryAllocator;
			return ( XMP_Uns8 * ) memPtr + kOFFSET;
		}
		return NULL;

	}

	void APICALL MemoryAllocatorWrapperImpl::deallocate( void * ptr ) __NOTHROW__ {
		void * actualMemPtr = ( XMP_Uns8 * ) ptr - kOFFSET;
		pIMemoryAllocator allocator = *( ( IMemoryAllocator ** ) actualMemPtr );
		if ( allocator )
			allocator->deallocate( actualMemPtr );
		else
			free( actualMemPtr );
	}

	void * APICALL MemoryAllocatorWrapperImpl::reallocate( void * ptr, sizet size ) __NOTHROW__ {
		size_t actualSize = size + kOFFSET;
		void * actualMemPtr = ( XMP_Uns8 * ) ptr - kOFFSET;
		void * memPtr( NULL );
		if ( mpMemoryAllocator )
			memPtr = mpMemoryAllocator->reallocate( actualMemPtr, actualSize );
		else
			memPtr = realloc( actualMemPtr, actualSize );

		if ( memPtr ) {
			IMemoryAllocator ** address = ( IMemoryAllocator ** ) memPtr;
			*address = mpMemoryAllocator;
			return ( XMP_Uns8 * ) memPtr + kOFFSET;
		}
		return NULL;
	}

}