Line data Source code
1 : /* Copyright (C) 2025 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 : #include <cstddef> 26 : #include <new> 27 : 28 : namespace AtlasMessage 29 : { 30 : 31 : // Shared pointers need to be allocated and freed from the same heap. 32 : // So, both sides of the Shareable interface should set these function pointers 33 : // to point to the same function. (The game will have to dynamically load them 34 : // from the DLL.) 35 : extern void* (*ShareableMallocFptr) (size_t n); 36 : extern void (*ShareableFreeFptr) (void* p); 37 : 38 : // Implement shared new/delete on top of those 39 : template<typename T> T* ShareableNew() 40 : { 41 : T* p = (T*)ShareableMallocFptr(sizeof(T)); 42 : new (p) T; 43 : return p; 44 : } 45 0 : template<typename T> void ShareableDelete(T* p) 46 : { 47 0 : p->~T(); 48 0 : ShareableFreeFptr(p); 49 0 : } 50 : // Or maybe we want to use a non-default constructor 51 : #define SHAREABLE_NEW(T, data) (new ( (T*)AtlasMessage::ShareableMallocFptr(sizeof(T)) ) T data) 52 : 53 : } 54 : 55 : #endif // INCLUDED_SHAREDMEMORY