diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /public/vallocator.h | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'public/vallocator.h')
| -rw-r--r-- | public/vallocator.h | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/public/vallocator.h b/public/vallocator.h new file mode 100644 index 0000000..ea26dbe --- /dev/null +++ b/public/vallocator.h @@ -0,0 +1,84 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +// +//=============================================================================// + +// These classes let you write your own allocators to be used with new and delete. +// If you have an allocator: VAllocator *pAlloc, you can call new and delete like this: +// +// ptr = VNew(pAlloc) ClassName; +// VDelete(pAlloc, ptr); +// +// Note: allocating and freeing arrays of objects will not work using VAllocators. + + + +#ifndef VALLOCATOR_H +#define VALLOCATOR_H + + +class VAllocator +{ +public: + virtual void* Alloc(unsigned long size)=0; + virtual void Free(void *ptr)=0; +}; + + +// This allocator just uses malloc and free. +class VStdAllocator : public VAllocator +{ +public: + virtual void* Alloc(unsigned long size); + virtual void Free(void *ptr); +}; +extern VStdAllocator g_StdAllocator; + + + +// Use these to allocate classes through VAllocator. +// Allocating arrays of classes is not supported. +#define VNew(pAlloc) new +#define VDelete(pAlloc, ptr) delete ptr + +// Used internally.. just makes sure we call the right operator new. +class DummyAllocatorHelper +{ +public: + int x; +}; + +inline void* operator new(size_t size, void *ptr, DummyAllocatorHelper *asdf) +{ + (void)asdf; // Suppress unused-variable compiler warnings. + (void)size; + return ptr; +} + +inline void operator delete(void *ptrToDelete, void *ptr, DummyAllocatorHelper *asdf) +{ + (void)asdf; // Suppress unused-variable compiler warnings. + (void)ptr; + (void)ptrToDelete; +} + +// Use these to manually construct and destruct lists of objects. +template<class T> +inline void VAllocator_CallConstructors(T *pObjects, int count=1) +{ + for(int i=0; i < count; i++) + new(&pObjects[i], (DummyAllocatorHelper*)0) T; +} + +template<class T> +inline void VAllocator_CallDestructors(T *pObjects, int count) +{ + for(int i=0; i < count; i++) + pObjects[i].~T(); +} + +#endif + |