segment_object_holder.h 3.78 KB
Newer Older
1
// Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#ifndef DATASRC_MEMORY_SEGMENT_OBJECT_HOLDER_H
#define DATASRC_MEMORY_SEGMENT_OBJECT_HOLDER_H 1

#include <util/memory_segment.h>
19
#include <string>
20
#include <cassert>
21
22
23
24
25
26

namespace isc {
namespace datasrc {
namespace memory {
namespace detail {

27
28
29
// Internal function to get next yet unused name of segment holder.
// We need the names of holders to be unique per segment at any given
// momemnt. This just keeps incrementing number after a prefix with
30
31
32
33
// each call, it should be enough (we assert it does not wrap around,
// but 64bits should be enough).
//
// Also, it is not thread safe.
34
35
36
std::string
getNextHolderName();

37
38
39
40
41
42
43
44
45
// A simple holder to create and use some objects in this implementation
// in an exception safe manner.   It works like std::auto_ptr but much
// more simplified.
// template parameter T is the type of object allocated by mem_sgmt.
// template parameter ARG_T is the type that will be passed to destroy()
// (deleter functor, etc).  It must be copyable.
template <typename T, typename ARG_T>
class SegmentObjectHolder {
public:
46
    SegmentObjectHolder(util::MemorySegment& mem_sgmt, ARG_T arg) :
47
48
49
        mem_sgmt_(mem_sgmt), arg_(arg),
        holder_name_(getNextHolderName()), holding_(true)
    {
50
        if (mem_sgmt_.setNamedAddress(holder_name_.c_str(), NULL)) {
51
52
53
54
            // OK. We've grown. The caller might need to be informed, so
            // we throw. But then, we don't get our destructor, so we
            // release the memory right away.
            mem_sgmt_.clearNamedAddress(holder_name_.c_str());
55
56
57
            isc_throw(isc::util::MemorySegmentGrown,
                      "Segment grown when allocating holder");
        }
58
    }
59
    ~SegmentObjectHolder() {
60
61
62
        if (holding_) {
            // Use release, as it removes the stored address from segment
            T* obj = release();
63
64
65
            if (obj) { // May be NULL if set wasn't called
                T::destroy(mem_sgmt_, obj, arg_);
            }
66
67
        }
    }
68
69
70
71
72
73
    void set(T* obj) {
        const bool grown = mem_sgmt_.setNamedAddress(holder_name_.c_str(),
                                                     obj);
        // We reserve the space in the constructor, should not grow now
        assert(!grown);
    }
74
75
    T* get() {
        if (holding_) {
76
77
78
79
            const util::MemorySegment::NamedAddressResult result =
                mem_sgmt_.getNamedAddress(holder_name_.c_str());
            assert(result.first);
            return (static_cast<T*>(result.second));
80
81
        } else {
            return (NULL);
82
83
84
        }
    }
    T* release() {
85
86
87
88
89
90
91
92
        if (holding_) {
            T* obj = get();
            mem_sgmt_.clearNamedAddress(holder_name_.c_str());
            holding_ = false;
            return (obj);
        } else {
            return (NULL);
        }
93
94
95
96
    }
private:
    util::MemorySegment& mem_sgmt_;
    ARG_T arg_;
97
98
    const std::string holder_name_;
    bool holding_;
99
100
101
102
103
104
105
106
107
108
109
110
};

} // detail
} // namespace memory
} // namespace datasrc
} // namespace isc

#endif // DATASRC_MEMORY_SEGMENT_OBJECT_HOLDER_H

// Local Variables:
// mode: c++
// End: