From: Mukund Sivaraman Date: Fri, 6 Jul 2012 02:11:09 +0000 (+0530) Subject: [2088] Add initial MemorySegment and MemorySegmentLocal classes X-Git-Tag: trac2351_base~188^2^2~14 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=00c9a91a3e5e83bc4255b8fbd31f15b6fcee09da;p=thirdparty%2Fkea.git [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.) --- diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am index fad24651d1..b135ffc3fa 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 0000000000..6a06c3abf2 --- /dev/null +++ b/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 + +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 index 0000000000..f29c1c3c47 --- /dev/null +++ b/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 diff --git a/src/lib/util/memory_segment_local.h b/src/lib/util/memory_segment_local.h new file mode 100644 index 0000000000..cc7929cd5c --- /dev/null +++ b/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 + +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__ diff --git a/src/lib/util/tests/Makefile.am b/src/lib/util/tests/Makefile.am index 837f887880..97ee77d3d1 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 0000000000..f0da84201b --- /dev/null +++ b/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 +#include + +using namespace std; + +namespace isc { +namespace util { + +TEST(MemorySegmentLocal, testDefault) { + auto_ptr 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