1 // Copyright 2020 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef COMMON_PLACEMENTALLOCATED_H_ 16 #define COMMON_PLACEMENTALLOCATED_H_ 17 18 #include <cstddef> 19 20 class PlacementAllocated { 21 public: 22 // Delete the default new operator so this can only be created with placement new. 23 void* operator new(size_t) = delete; 24 new(size_t size,void * ptr)25 void* operator new(size_t size, void* ptr) { 26 // Pass through the pointer of the allocation. This is essentially the default 27 // placement-new implementation, but we must define it if we delete the default 28 // new operator. 29 return ptr; 30 } 31 delete(void * ptr)32 void operator delete(void* ptr) { 33 // Object is placement-allocated. Don't free the memory. 34 } 35 delete(void *,void *)36 void operator delete(void*, void*) { 37 // This is added to match new(size_t size, void* ptr) 38 // Otherwise it triggers C4291 warning in MSVC 39 } 40 }; 41 42 #endif // COMMON_PLACEMENTALLOCATED_H_ 43