Browse Source

[2088] Add initial MemorySegment and MemorySegmentLocal classes

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.)
Mukund Sivaraman 12 years ago
parent
commit
00c9a91a3e

+ 2 - 0
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

+ 33 - 0
src/lib/util/memory_segment.h

@@ -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__

+ 43 - 0
src/lib/util/memory_segment_local.cc

@@ -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

+ 39 - 0
src/lib/util/memory_segment_local.h

@@ -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__

+ 1 - 0
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

+ 56 - 0
src/lib/util/tests/memory_segment_local_unittest.cc

@@ -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