Branch data Line data Source code
1 : : // -*- C++ -*-
2 : : // Copyright (C) 2008 Red Hat Inc.
3 : : //
4 : : // This file is part of systemtap, and is free software. You can
5 : : // redistribute it and/or modify it under the terms of the GNU General
6 : : // Public License (GPL); either version 2, or (at your option) any
7 : : // later version.
8 : :
9 : : #ifndef AUTO_FREE_H
10 : : #define AUTO_FREE_H 1
11 : : #include <cstdlib>
12 : :
13 : : // Very simple auto_ptr-like class for protecting storage allocated
14 : : // with free().
15 : : class auto_free
16 : : {
17 : : public:
18 : 0 : auto_free(void* ptr) : _ptr(ptr) {}
19 : 0 : ~auto_free()
20 : : {
21 [ # # ]: 0 : if (_ptr)
22 : 0 : std::free(_ptr);
23 : 0 : }
24 : : void release()
25 : : {
26 : : _ptr = 0;
27 : : }
28 : : private:
29 : : // No copying allowed.
30 : : auto_free(const auto_free& af);
31 : : // No assignment either
32 : : auto_free& operator=(const auto_free& rhs);
33 : : void* _ptr;
34 : : };
35 : :
36 : : // Use this to free a pointer whose value may change after the initial
37 : : // allocation e.g., be realloced.
38 : : template <typename T>
39 : : class auto_free_ref
40 : : {
41 : : public:
42 : : typedef T pointer_type;
43 : 277032 : auto_free_ref(pointer_type& ptr) : _ptr(ptr)
44 : : {
45 : : }
46 : 277032 : ~auto_free_ref()
47 : : {
48 [ + + ]: 277032 : if (_ptr)
49 : 277032 : std::free(_ptr);
50 : : }
51 : : private:
52 : : // No copying allowed.
53 : : auto_free_ref(const auto_free_ref& af);
54 : : // No assignment either
55 : : auto_free_ref& operator=(const auto_free_ref& rhs);
56 : : pointer_type& _ptr;
57 : : };
58 : : #endif
59 : :
60 : : /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
|