Line data Source code
1 : #ifndef PHP_CPP_ALLOCATOR_H
2 : #define PHP_CPP_ALLOCATOR_H
3 :
4 : #include <memory>
5 : #include "php.h"
6 :
7 : namespace wikidiff2 {
8 :
9 : /**
10 : * Allocation class which allows various C++ standard library functions
11 : * to allocate and free memory using PHP's emalloc/efree facilities.
12 : */
13 : template <class T>
14 : class PhpAllocator : public std::allocator<T>
15 : {
16 : public:
17 : // Make some typedefs to avoid having to use "typename" everywhere
18 : typedef typename std::allocator<T>::pointer pointer;
19 : typedef typename std::allocator<T>::size_type size_type;
20 :
21 : // The rebind member allows callers to get allocators for other types,
22 : // given a specialised allocator
23 : template <class U> struct rebind { typedef PhpAllocator<U> other; };
24 :
25 : // Various constructors that do nothing
26 23429 : PhpAllocator() throw() {}
27 55569 : PhpAllocator(const PhpAllocator& other) throw() {}
28 1302 : template <class U> PhpAllocator(const PhpAllocator<U>&) throw() {}
29 :
30 : // Allocate some memory from the PHP request pool
31 55608 : pointer allocate(size_type size, typename std::allocator<void>::const_pointer hint = 0) {
32 55608 : return (pointer)safe_emalloc(size, sizeof(T), 0);
33 : }
34 1753 :
35 1753 : // Free memory
36 0 : void deallocate(pointer p, size_type n) {
37 0 : return efree(p);
38 0 : }
39 : };
40 2077 :
41 2077 : } // namespace wikidiff2
42 0 :
43 6572 : #endif
|