]>
Commit | Line | Data |
---|---|---|
2e67a43b TM |
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 | auto_free(void* ptr) : _ptr(ptr) {} | |
19 | ~auto_free() | |
20 | { | |
21 | if (_ptr) | |
22 | std::free(_ptr); | |
23 | } | |
24 | void release() | |
25 | { | |
26 | _ptr = 0; | |
27 | } | |
28 | private: | |
29 | // No copying allowed. | |
558982c5 TM |
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 | auto_free_ref(pointer_type& ptr) : _ptr(ptr) | |
2e67a43b TM |
44 | { |
45 | } | |
558982c5 | 46 | ~auto_free_ref() |
2e67a43b | 47 | { |
558982c5 TM |
48 | if (_ptr) |
49 | std::free(_ptr); | |
2e67a43b | 50 | } |
558982c5 TM |
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; | |
2e67a43b TM |
57 | }; |
58 | #endif | |
73267b89 JS |
59 | |
60 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |