summaryrefslogtreecommitdiff
path: root/zend/delayedfree.h
blob: 598d4f53bf991428f5a497cb08c9a85f1702526b (plain)
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
40
41
42
43
44
45
46
47
48
49
50
/**
 *  DelayedFree.h
 *
 *  Sometimes a piece of data must be freed when a function gets out of
 *  scope. In stead of putting the efree() call right before every possible
 *  function end point (exceptions, returns, zend_errors()), we can use
 *  this simple class instead
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2015 Copernica BV
 */

/**
 *  Set up namespace
 */
namespace Php {

/**
 *  Class definition
 */
class DelayedFree
{
private:
    /**
     *  The data that has to be free'd when the object falls out of scope
     *  @var void*
     */
    void *_data;

public:
    /**
     *  Constructor
     *  @param  data        Data that will be freed on destruction
     */
    DelayedFree(void *data) : _data(data) {}
    
    /**
     *  Destructor
     */
    virtual ~DelayedFree()
    {
        // free the data
        efree(_data);
    }
};

/**
 *  End of namespace
 */
}