File: php_cpp_allocator.h

package info (click to toggle)
wikidiff2 1.2%2Bgit03ea59f-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 276 kB
  • ctags: 240
  • sloc: cpp: 1,357; php: 172; makefile: 13
file content (39 lines) | stat: -rw-r--r-- 1,175 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef PHP_CPP_ALLOCATOR_H
#define PHP_CPP_ALLOCATOR_H

#include <memory>
#include "php.h"

/**
 * Allocation class which allows various C++ standard library functions
 * to allocate and free memory using PHP's emalloc/efree facilities.
 */
template <class T>
class PhpAllocator : public std::allocator<T> 
{
	public:
		// Make some typedefs to avoid having to use "typename" everywhere
		typedef typename std::allocator<T>::pointer pointer;
		typedef typename std::allocator<T>::size_type size_type;

		// The rebind member allows callers to get allocators for other types, 
		// given a specialised allocator
		template <class U> struct rebind { typedef PhpAllocator<U> other; };

		// Various constructors that do nothing
		PhpAllocator() throw() {}
		PhpAllocator(const PhpAllocator& other) throw() {}
		template <class U> PhpAllocator(const PhpAllocator<U>&) throw() {}

		// Allocate some memory from the PHP request pool
		pointer allocate(size_type size, typename std::allocator<void>::const_pointer hint = 0) {
			return (pointer)safe_emalloc(size, sizeof(T), 0);
		}

		// Free memory
		void deallocate(pointer p, size_type n) {
			return efree(p);
		}
};

#endif