BIND 10 master, updated. 9726cd1627ba4c074f65e57799e8b77ec158a491 Merge branch 'master' into trac2088
BIND 10 source code commits
bind10-changes at lists.isc.org
Sun Jul 15 17:40:21 UTC 2012
The branch, master has been updated
via 9726cd1627ba4c074f65e57799e8b77ec158a491 (commit)
via c9f90e89db5b8cae1a79896700cea84abe4a9e55 (commit)
via 59c5e16d4fae5a18681580ca6948999d60ee7e5d (commit)
via 25cc38bbd45cca618c0f073b743882865ebc1d64 (commit)
via 3e70efc345f40ae6b6e0310bae46dc4167fd4fbc (commit)
via c0af8f8c458f85e5771a7dd47e17ab8c45a17d07 (commit)
via aa584b6a81609f94620b538329977d1f62ad702a (commit)
via fb9bdb9bd4f34364475ca1d0472ce0975affec73 (commit)
via 96c432de789212c35a4089a46ba551e36547e797 (commit)
via 16f7c0656669ebe1e93ba9f88a35072311bb2f03 (commit)
via 093aab9cf5593c305e7cf6576ed339fec181979c (commit)
via 05a1e1a98e21ff9d5591af9c5debdbf74eb9cb77 (commit)
via 22690690bd3da8e82cdcaa38212317fc8d3d72fb (commit)
via a4296e2bbf266d31004f99569b8da028e84d8460 (commit)
via 00c9a91a3e5e83bc4255b8fbd31f15b6fcee09da (commit)
via 9eb79ba9af49234fc87a4806bbfdcfa8e43e7aca (commit)
from 7fca81716ad3a755bf5744e88c3adeef15b04450 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 9726cd1627ba4c074f65e57799e8b77ec158a491
Merge: c9f90e8 7fca817
Author: Mukund Sivaraman <muks at isc.org>
Date: Sun Jul 15 22:27:39 2012 +0530
Merge branch 'master' into trac2088
-----------------------------------------------------------------------
Summary of changes:
src/lib/util/Makefile.am | 2 +
src/lib/util/memory_segment.h | 69 +++++++++++++
...rocess_sync_null.cc => memory_segment_local.cc} | 41 +++++---
src/lib/util/memory_segment_local.h | 76 ++++++++++++++
src/lib/util/tests/Makefile.am | 1 +
.../util/tests/memory_segment_local_unittest.cc | 108 ++++++++++++++++++++
6 files changed, 283 insertions(+), 14 deletions(-)
create mode 100644 src/lib/util/memory_segment.h
copy src/lib/util/{interprocess_sync_null.cc => memory_segment_local.cc} (52%)
create mode 100644 src/lib/util/memory_segment_local.h
create mode 100644 src/lib/util/tests/memory_segment_local_unittest.cc
-----------------------------------------------------------------------
diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am
index fad2465..b135ffc 100644
--- a/src/lib/util/Makefile.am
+++ b/src/lib/util/Makefile.am
@@ -16,6 +16,8 @@ libutil_la_SOURCES += time_utilities.h time_utilities.cc
libutil_la_SOURCES += interprocess_sync.h
libutil_la_SOURCES += interprocess_sync_file.h interprocess_sync_file.cc
libutil_la_SOURCES += interprocess_sync_null.h interprocess_sync_null.cc
+libutil_la_SOURCES += memory_segment.h
+libutil_la_SOURCES += memory_segment_local.h memory_segment_local.cc
libutil_la_SOURCES += range_utilities.h
libutil_la_SOURCES += hash/sha1.h hash/sha1.cc
libutil_la_SOURCES += encode/base16_from_binary.h
diff --git a/src/lib/util/memory_segment.h b/src/lib/util/memory_segment.h
new file mode 100644
index 0000000..97d2e02
--- /dev/null
+++ b/src/lib/util/memory_segment.h
@@ -0,0 +1,69 @@
+// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
+//
+// 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 __MEMORY_SEGMENT_H__
+#define __MEMORY_SEGMENT_H__
+
+#include <stdlib.h>
+
+namespace isc {
+namespace util {
+
+/// \brief Memory Segment Class
+///
+/// This class specifies an interface for allocating memory
+/// segments. This is an abstract class and a real
+/// implementation such as MemorySegmentLocal should be used
+/// in code.
+class MemorySegment {
+public:
+ /// \brief Destructor
+ virtual ~MemorySegment() {}
+
+ /// \brief Allocate/acquire a segment of memory. The source of the
+ /// memory is dependent on the implementation used.
+ ///
+ /// Throws <code>std::bad_alloc</code> if the implementation cannot
+ /// allocate the requested storage.
+ ///
+ /// \param size The size of the memory requested in bytes.
+ /// \return Returns pointer to the memory allocated.
+ virtual void* allocate(size_t size) = 0;
+
+ /// \brief Free/release a segment of memory.
+ ///
+ /// This method may throw <code>isc::OutOfRange</code> if \c size is
+ /// not equal to the originally allocated size. \c size could be
+ /// used by some implementations such as a slice allocator, where
+ /// freeing memory also requires the size to be specified. We also
+ /// use this argument in some implementations to test if all allocated
+ /// memory was deallocated properly.
+ ///
+ /// \param ptr Pointer to the block of memory to free/release. This
+ /// should be equal to a value returned by <code>allocate()</code>.
+ /// \param size The size of the memory to be freed in bytes. This
+ /// should be equal to the number of bytes originally allocated.
+ virtual void deallocate(void* ptr, size_t size) = 0;
+
+ /// \brief Check if all allocated memory was deallocated.
+ ///
+ /// \return Returns <code>true</code> if all allocated memory was
+ /// deallocated, <code>false</code> otherwise.
+ virtual bool allMemoryDeallocated() const = 0;
+};
+
+} // namespace util
+} // namespace isc
+
+#endif // __MEMORY_SEGMENT_H__
diff --git a/src/lib/util/memory_segment_local.cc b/src/lib/util/memory_segment_local.cc
new file mode 100644
index 0000000..9c345c9
--- /dev/null
+++ b/src/lib/util/memory_segment_local.cc
@@ -0,0 +1,55 @@
+// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
+//
+// 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.
+
+#include "memory_segment_local.h"
+#include <exceptions/exceptions.h>
+
+namespace isc {
+namespace util {
+
+void*
+MemorySegmentLocal::allocate(size_t size) {
+ void* ptr = malloc(size);
+ if (ptr == NULL) {
+ throw std::bad_alloc();
+ }
+
+ allocated_size_ += size;
+ return (ptr);
+}
+
+void
+MemorySegmentLocal::deallocate(void* ptr, size_t size) {
+ if (ptr == NULL) {
+ // Return early if NULL is passed to be deallocated (without
+ // modifying allocated_size, or comparing against it).
+ return;
+ }
+
+ if (size > allocated_size_) {
+ isc_throw(OutOfRange, "Invalid size to deallocate: " << size
+ << "; currently allocated size: " << allocated_size_);
+ }
+
+ allocated_size_ -= size;
+ free(ptr);
+}
+
+bool
+MemorySegmentLocal::allMemoryDeallocated() const {
+ return (allocated_size_ == 0);
+}
+
+} // namespace util
+} // namespace isc
diff --git a/src/lib/util/memory_segment_local.h b/src/lib/util/memory_segment_local.h
new file mode 100644
index 0000000..5e43e53
--- /dev/null
+++ b/src/lib/util/memory_segment_local.h
@@ -0,0 +1,76 @@
+// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
+//
+// 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 __MEMORY_SEGMENT_LOCAL_H__
+#define __MEMORY_SEGMENT_LOCAL_H__
+
+#include <util/memory_segment.h>
+
+namespace isc {
+namespace util {
+
+/// \brief malloc/free based Memory Segment class
+///
+/// This class specifies a concrete implementation for a malloc/free
+/// based MemorySegment. Please see the MemorySegment class
+/// documentation for usage.
+class MemorySegmentLocal : public MemorySegment {
+public:
+ /// \brief Constructor
+ ///
+ /// Creates a local memory segment object
+ MemorySegmentLocal() : allocated_size_(0) {
+ }
+
+ /// \brief Destructor
+ virtual ~MemorySegmentLocal() {}
+
+ /// \brief Allocate/acquire a segment of memory. The source of the
+ /// memory is libc's malloc().
+ ///
+ /// Throws <code>std::bad_alloc</code> if the implementation cannot
+ /// allocate the requested storage.
+ ///
+ /// \param size The size of the memory requested in bytes.
+ /// \return Returns pointer to the memory allocated.
+ virtual void* allocate(size_t size);
+
+ /// \brief Free/release a segment of memory.
+ ///
+ /// This method may throw <code>isc::OutOfRange</code> if \c size is
+ /// not equal to the originally allocated size.
+ ///
+ /// \param ptr Pointer to the block of memory to free/release. This
+ /// should be equal to a value returned by <code>allocate()</code>.
+ /// \param size The size of the memory to be freed in bytes. This
+ /// should be equal to the number of bytes originally allocated.
+ virtual void deallocate(void* ptr, size_t size);
+
+ /// \brief Check if all allocated memory was deallocated.
+ ///
+ /// \return Returns <code>true</code> if all allocated memory was
+ /// deallocated, <code>false</code> otherwise.
+ virtual bool allMemoryDeallocated() const;
+
+private:
+ // allocated_size_ can underflow, wrap around to max size_t (which
+ // is unsigned). But because we only do a check against 0 and not a
+ // relation comparison, this is okay.
+ size_t allocated_size_;
+};
+
+} // namespace util
+} // namespace isc
+
+#endif // __MEMORY_SEGMENT_LOCAL_H__
diff --git a/src/lib/util/tests/Makefile.am b/src/lib/util/tests/Makefile.am
index 837f887..97ee77d 100644
--- a/src/lib/util/tests/Makefile.am
+++ b/src/lib/util/tests/Makefile.am
@@ -33,6 +33,7 @@ run_unittests_SOURCES += io_utilities_unittest.cc
run_unittests_SOURCES += lru_list_unittest.cc
run_unittests_SOURCES += interprocess_sync_file_unittest.cc
run_unittests_SOURCES += interprocess_sync_null_unittest.cc
+run_unittests_SOURCES += memory_segment_local_unittest.cc
run_unittests_SOURCES += qid_gen_unittest.cc
run_unittests_SOURCES += random_number_generator_unittest.cc
run_unittests_SOURCES += sha1_unittest.cc
diff --git a/src/lib/util/tests/memory_segment_local_unittest.cc b/src/lib/util/tests/memory_segment_local_unittest.cc
new file mode 100644
index 0000000..a18a9b2
--- /dev/null
+++ b/src/lib/util/tests/memory_segment_local_unittest.cc
@@ -0,0 +1,108 @@
+// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
+//
+// 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.
+
+#include "util/memory_segment_local.h"
+#include <exceptions/exceptions.h>
+#include <gtest/gtest.h>
+#include <memory>
+
+using namespace std;
+using namespace isc::util;
+
+namespace {
+
+TEST(MemorySegmentLocal, TestLocal) {
+ auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
+
+ // By default, nothing is allocated.
+ EXPECT_TRUE(segment->allMemoryDeallocated());
+
+ void* ptr = segment->allocate(1024);
+
+ // Now, we have an allocation:
+ EXPECT_FALSE(segment->allMemoryDeallocated());
+
+ void* ptr2 = segment->allocate(42);
+
+ // Still:
+ EXPECT_FALSE(segment->allMemoryDeallocated());
+
+ // These should not fail, because the buffers have been allocated.
+ EXPECT_NO_FATAL_FAILURE(memset(ptr, 0, 1024));
+ EXPECT_NO_FATAL_FAILURE(memset(ptr, 0, 42));
+
+ segment->deallocate(ptr, 1024);
+
+ // Still:
+ EXPECT_FALSE(segment->allMemoryDeallocated());
+
+ segment->deallocate(ptr2, 42);
+
+ // Now, we have an deallocated everything:
+ EXPECT_TRUE(segment->allMemoryDeallocated());
+}
+
+TEST(MemorySegmentLocal, TestTooMuchMemory) {
+ auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
+
+ EXPECT_THROW(segment->allocate(0x7fffffffffffffff), bad_alloc);
+}
+
+TEST(MemorySegmentLocal, TestBadDeallocate) {
+ auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
+
+ // By default, nothing is allocated.
+ EXPECT_TRUE(segment->allMemoryDeallocated());
+
+ void* ptr = segment->allocate(1024);
+
+ // Now, we have an allocation:
+ EXPECT_FALSE(segment->allMemoryDeallocated());
+
+ // This should not throw
+ EXPECT_NO_THROW(segment->deallocate(ptr, 1024));
+
+ // Now, we have an deallocated everything:
+ EXPECT_TRUE(segment->allMemoryDeallocated());
+
+ ptr = segment->allocate(1024);
+
+ // Now, we have another allocation:
+ EXPECT_FALSE(segment->allMemoryDeallocated());
+
+ // This should throw as the size passed to deallocate() is larger
+ // than what was allocated.
+ EXPECT_THROW(segment->deallocate(ptr, 2048), isc::OutOfRange);
+
+ // This should not throw
+ EXPECT_NO_THROW(segment->deallocate(ptr, 1024));
+
+ // Now, we have an deallocated everything:
+ EXPECT_TRUE(segment->allMemoryDeallocated());
+}
+
+TEST(MemorySegmentLocal, TestNullDeallocate) {
+ auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
+
+ // By default, nothing is allocated.
+ EXPECT_TRUE(segment->allMemoryDeallocated());
+
+ // NULL deallocation is a no-op.
+ EXPECT_NO_THROW(segment->deallocate(NULL, 1024));
+
+ // This should still return true.
+ EXPECT_TRUE(segment->allMemoryDeallocated());
+}
+
+} // anonymous namespace
More information about the bind10-changes
mailing list