diff options
| author | Joe Ludwig <[email protected]> | 2013-06-26 15:22:04 -0700 |
|---|---|---|
| committer | Joe Ludwig <[email protected]> | 2013-06-26 15:22:04 -0700 |
| commit | 39ed87570bdb2f86969d4be821c94b722dc71179 (patch) | |
| tree | abc53757f75f40c80278e87650ea92808274aa59 /sp/src/public/vallocator.h | |
| download | source-sdk-2013-39ed87570bdb2f86969d4be821c94b722dc71179.tar.xz source-sdk-2013-39ed87570bdb2f86969d4be821c94b722dc71179.zip | |
First version of the SOurce SDK 2013
Diffstat (limited to 'sp/src/public/vallocator.h')
| -rw-r--r-- | sp/src/public/vallocator.h | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/sp/src/public/vallocator.h b/sp/src/public/vallocator.h new file mode 100644 index 00000000..598e69f6 --- /dev/null +++ b/sp/src/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)
+{
+ asdf=asdf; // compiler warning.
+ size=size;
+ return ptr;
+}
+
+inline void operator delete(void *ptrToDelete, void *ptr, DummyAllocatorHelper *asdf)
+{
+ asdf=asdf; // compiler warning.
+ ptr=ptr;
+ ptrToDelete=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
+
|