]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2088] Add initial MemorySegment and MemorySegmentLocal classes
authorMukund Sivaraman <muks@isc.org>
Fri, 6 Jul 2012 02:11:09 +0000 (07:41 +0530)
committerMukund Sivaraman <muks@isc.org>
Fri, 6 Jul 2012 02:11:12 +0000 (07:41 +0530)
These could very well not be the implementations we are looking
for. From the bug report, it's not clear how these methods will be used:

* Is deallocate() meant to free smaller blocks than what allocate()
  returns?

* Because these methods will be virtual, a MemorySegment object is
  necessary. Will this be a singleton?

* Does allMemoryDeallocated() need anything fancy like a set of all
  allocated buffers kept, or is just a counter of total size of memory
  allocated sufficient? (The counter is implemented.)

src/lib/util/Makefile.am
src/lib/util/memory_segment.h [new file with mode: 0644]
src/lib/util/memory_segment_local.cc [new file with mode: 0644]
src/lib/util/memory_segment_local.h [new file with mode: 0644]
src/lib/util/tests/Makefile.am
src/lib/util/tests/memory_segment_local_unittest.cc [new file with mode: 0644]

index fad24651d15dace783af0dd3d20a758fe2e90d91..b135ffc3fac427a55e2298cdf750eeab3f41ed9b 100644 (file)
@@ -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 (file)
index 0000000..6a06c3a
--- /dev/null
@@ -0,0 +1,33 @@
+// 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 {
+
+class MemorySegment {
+public:
+    virtual void* allocate(size_t size) = 0;
+    virtual void deallocate(void* ptr, size_t size) = 0;
+    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 (file)
index 0000000..f29c1c3
--- /dev/null
@@ -0,0 +1,43 @@
+// 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"
+
+namespace isc {
+namespace util {
+
+void*
+MemorySegmentLocal::allocate(size_t size) {
+    void *ptr = malloc(size);
+
+    if (ptr != NULL) {
+        allocated_size_ += size;
+    }
+
+    return (ptr);
+}
+
+void
+MemorySegmentLocal::deallocate(void* ptr, size_t 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 (file)
index 0000000..cc7929c
--- /dev/null
@@ -0,0 +1,39 @@
+// 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 {
+
+class MemorySegmentLocal : public MemorySegment {
+public:
+    MemorySegmentLocal() : allocated_size_(0) {
+    }
+
+    void* allocate(size_t size);
+    void deallocate(void* ptr, size_t size);
+    bool allMemoryDeallocated() const;
+
+private:
+    size_t allocated_size_;
+};
+
+} // namespace util
+} // namespace isc
+
+#endif // __MEMORY_SEGMENT_LOCAL_H__
index 837f887880fd802a65b030d3ab8b0bb83cab2559..97ee77d3d17348be8e5d22111f5313d1e1e30290 100644 (file)
@@ -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 (file)
index 0000000..f0da842
--- /dev/null
@@ -0,0 +1,56 @@
+// 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 <gtest/gtest.h>
+#include <memory>
+
+using namespace std;
+
+namespace isc {
+namespace util {
+
+TEST(MemorySegmentLocal, testDefault) {
+  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());
+}
+
+} // namespace util
+} // namespace isc