Line data Source code
1 : /* Copyright (C) 2009 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #ifndef INCLUDED_SHAREDMEMORY
19 : #define INCLUDED_SHAREDMEMORY
20 :
21 : // we want to use placement new without grief
22 : // (Duplicated in Shareable.h)
23 : #undef new
24 :
25 : namespace AtlasMessage
26 : {
27 :
28 : // Shared pointers need to be allocated and freed from the same heap.
29 : // So, both sides of the Shareable interface should set these function pointers
30 : // to point to the same function. (The game will have to dynamically load them
31 : // from the DLL.)
32 : extern void* (*ShareableMallocFptr) (size_t n);
33 : extern void (*ShareableFreeFptr) (void* p);
34 :
35 : // Implement shared new/delete on top of those
36 : template<typename T> T* ShareableNew()
37 : {
38 : T* p = (T*)ShareableMallocFptr(sizeof(T));
39 : new (p) T;
40 : return p;
41 : }
42 0 : template<typename T> void ShareableDelete(T* p)
43 : {
44 0 : p->~T();
45 0 : ShareableFreeFptr(p);
46 0 : }
47 : // Or maybe we want to use a non-default constructor
48 : #define SHAREABLE_NEW(T, data) (new ( (T*)AtlasMessage::ShareableMallocFptr(sizeof(T)) ) T data)
49 :
50 : }
51 :
52 : #endif // INCLUDED_SHAREDMEMORY
|