-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoryFunctions.h
122 lines (103 loc) · 3.28 KB
/
memoryFunctions.h
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
//-----------------------------------------------------------------------------
// Application Core Library
// Copyright (c) 2009-2012 DuJardin Consulting, LLC.
// Portions Copyright (c) 2009 GarageGames, Inc.
//-----------------------------------------------------------------------------
#ifndef _CORE_MEMORYFUNCTIONS_H_
#define _CORE_MEMORYFUNCTIONS_H_
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <new>
#include "core/types.h"
#if defined( ACL_OS_WIN32 ) || defined( ACL_OS_XBOX ) || defined( ACL_OS_XENON )
#include <malloc.h>
#endif
// Always use aligned memory allocation
#if defined( ACL_OS_WIN32 ) || defined( ACL_OS_XBOX ) || defined( ACL_OS_XENON )
#define _MALLOC(size) _aligned_malloc(size, 16)
#define _REALLOC(ptr,size) _aligned_realloc(ptr,size, 16)
#define _FREE(ptr) _aligned_free(ptr)
#elif defined( ACL_OS_MAC )
// Mac OS X always aligns malloc'd memory on 16-byte boundaries, so just use normal malloc routines
// <http://developer.apple.com/technotes/tn2005/tn2130.html#TNTAG6>
#define _MALLOC(size) malloc(size)
#define _REALLOC(ptr,size) realloc(ptr,size)
#define _FREE(ptr) free(ptr)
#elif defined( ACL_OS_LINUX )
inline void* posix_aligned_malloc(size_t size)
{
void *ptr;
posix_memalign(&ptr,16,size);
return ptr;
}
#define _MALLOC(size) posix_aligned_malloc(size)
#define _REALLOC(ptr,size) realloc(ptr,size)
#define _FREE(ptr) free(ptr)
#else
#error "Check aligned memory allocation on your platform"
#endif
namespace ACLib
{
inline void* dMalloc(dsize_t in_size)
{
return _MALLOC( in_size );
}
inline void* dRealloc(void* in_pResize, dsize_t in_size)
{
return _REALLOC( in_pResize, in_size );
}
inline void dFree(void* in_pFree)
{
_FREE( in_pFree );
}
inline void* dMemcpy(void *dst, const void *src, dsize_t size)
{
return memcpy(dst,src,size);
}
inline void* dMemmove(void *dst, const void *src, dsize_t size)
{
return memmove(dst,src,size);
}
inline void* dMemset(void *dst, int c, dsize_t size)
{
return memset(dst,c,size);
}
inline int dMemcmp(const void *ptr1, const void *ptr2, dsize_t len)
{
return(memcmp(ptr1, ptr2, len));
}
///@defgroup ObjTrickery Object Management Trickery
///
/// These functions are to construct and destruct objects in memory
/// without causing a free or malloc call to occur. This is so that
/// we don't have to worry about allocating, say, space for a hundred
/// NetAddresses with a single malloc call, calling delete on a single
/// NetAdress, and having it try to free memory out from under us.
///
/// @{
/// Constructs an object that already has memory allocated for it.
template <class T>
inline T* constructInPlace(T* p)
{
return new(p) T;
}
/// Copy constructs an object that already has memory allocated for it.
template <class T>
inline T* constructInPlace(T* p, const T& copy)
{
return new(p) T(copy);
}
template <class T, class T2> inline T* constructInPlace(T* ptr, const T2& t2)
{
return new(ptr) T(t2);
}
/// Destructs an object without freeing the memory associated with it.
template <class T>
inline void destructInPlace(T* p)
{
p->~T();
}
};
/// @}
#endif