From e25984db80c73093c3e6ecf7cf4034f27e9b026c Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Tue, 2 Jun 2015 13:48:30 +0200 Subject: [PATCH 1/1] [mc] C++ify RegionType, StorageType enums Namespaced and strongly-typed. --- src/mc/RegionSnapshot.cpp | 6 ++-- src/mc/RegionSnapshot.hpp | 75 +++++++++++++++++++++------------------ src/mc/mc_checkpoint.cpp | 21 +++++------ src/mc/mc_compare.cpp | 10 +++--- src/mc/mc_diff.cpp | 2 +- src/mc/mc_snapshot.cpp | 12 +++---- src/mc/mc_snapshot.h | 16 ++++----- 7 files changed, 75 insertions(+), 67 deletions(-) diff --git a/src/mc/RegionSnapshot.cpp b/src/mc/RegionSnapshot.cpp index 3d1daa4dbc..23853c8341 100644 --- a/src/mc/RegionSnapshot.cpp +++ b/src/mc/RegionSnapshot.cpp @@ -19,7 +19,7 @@ namespace simgrid { namespace mc { RegionSnapshot dense_region( - mc_region_type_t region_type, + RegionType region_type, void *start_addr, void* permanent_addr, size_t size) { std::vector data(size); @@ -44,7 +44,7 @@ RegionSnapshot dense_region( * @param size Size of the data* */ RegionSnapshot region( - mc_region_type_t type, void *start_addr, void* permanent_addr, size_t size) + RegionType type, void *start_addr, void* permanent_addr, size_t size) { if (_sg_mc_sparse_checkpoint) { return sparse_region(type, start_addr, permanent_addr, size); @@ -53,7 +53,7 @@ RegionSnapshot region( } } -RegionSnapshot sparse_region(mc_region_type_t region_type, +RegionSnapshot sparse_region(RegionType region_type, void *start_addr, void* permanent_addr, size_t size) { mc_process_t process = &mc_model_checker->process(); diff --git a/src/mc/RegionSnapshot.hpp b/src/mc/RegionSnapshot.hpp index 0c85f22eb3..a5f1946586 100644 --- a/src/mc/RegionSnapshot.hpp +++ b/src/mc/RegionSnapshot.hpp @@ -13,20 +13,6 @@ #ifndef SIMGRID_MC_REGION_SNAPSHOT_HPP #define SIMGRID_MC_REGION_SNAPSHOT_HPP -typedef enum e_mc_region_type_t { - MC_REGION_TYPE_UNKNOWN = 0, - MC_REGION_TYPE_HEAP = 1, - MC_REGION_TYPE_DATA = 2 -} mc_region_type_t; - -// TODO, use OO instead of this -typedef enum e_mc_region_storage_type_t { - MC_REGION_STORAGE_TYPE_NONE = 0, - MC_REGION_STORAGE_TYPE_FLAT = 1, - MC_REGION_STORAGE_TYPE_CHUNKED = 2, - MC_REGION_STORAGE_TYPE_PRIVATIZED = 3 -} mc_region_storage_type_t; - namespace simgrid { namespace mc { @@ -97,6 +83,20 @@ public: remote_ptr addr, std::size_t page_count); }; +enum class RegionType { + Unknown = 0, + Heap = 1, + Data = 2 +}; + +// TODO, use Boost.Variant instead of this +enum class StorageType { + NoData = 0, + Flat = 1, + Chunked = 2, + Privatized = 3 +}; + /** @brief Copy/snapshot of a given memory region * * Different types of region snapshot storage types exist: @@ -114,9 +114,16 @@ public: * each type. */ class RegionSnapshot { + static const RegionType UnknownRegion = RegionType::Unknown; + static const RegionType HeapRegion = RegionType::Heap; + static const RegionType DataRegion = RegionType::Data; + static const StorageType NoData = StorageType::NoData; + static const StorageType FlatData = StorageType::Flat; + static const StorageType ChunkedData = StorageType::Chunked; + static const StorageType PrivatizedData = StorageType::Privatized; private: - mc_region_type_t region_type_; - mc_region_storage_type_t storage_type_; + RegionType region_type_; + StorageType storage_type_; mc_object_info_t object_info_; /** @brief Virtual address of the region in the simulated process */ @@ -141,16 +148,16 @@ private: std::vector privatized_regions_; public: RegionSnapshot() : - region_type_(MC_REGION_TYPE_UNKNOWN), - storage_type_(MC_REGION_STORAGE_TYPE_NONE), + region_type_(UnknownRegion), + storage_type_(NoData), object_info_(nullptr), start_addr_(nullptr), size_(0), permanent_addr_(nullptr) {} - RegionSnapshot(mc_region_type_t type, void *start_addr, void* permanent_addr, size_t size) : + RegionSnapshot(RegionType type, void *start_addr, void* permanent_addr, size_t size) : region_type_(type), - storage_type_(MC_REGION_STORAGE_TYPE_NONE), + storage_type_(NoData), object_info_(nullptr), start_addr_(start_addr), size_(size), @@ -191,8 +198,8 @@ public: void clear() { - region_type_ = MC_REGION_TYPE_UNKNOWN; - storage_type_ = MC_REGION_STORAGE_TYPE_NONE; + region_type_ = UnknownRegion; + storage_type_ = NoData; privatized_regions_.clear(); page_numbers_.clear(); flat_data_.clear(); @@ -204,7 +211,7 @@ public: void clear_data() { - storage_type_ = MC_REGION_STORAGE_TYPE_NONE; + storage_type_ = NoData; flat_data_.clear(); page_numbers_.clear(); privatized_regions_.clear(); @@ -212,7 +219,7 @@ public: void flat_data(std::vector data) { - storage_type_ = MC_REGION_STORAGE_TYPE_FLAT; + storage_type_ = FlatData; flat_data_ = std::move(data); page_numbers_.clear(); privatized_regions_.clear(); @@ -221,7 +228,7 @@ public: void page_data(PerPageCopy page_data) { - storage_type_ = MC_REGION_STORAGE_TYPE_CHUNKED; + storage_type_ = ChunkedData; flat_data_.clear(); page_numbers_ = std::move(page_data); privatized_regions_.clear(); @@ -230,7 +237,7 @@ public: void privatized_data(std::vector data) { - storage_type_ = MC_REGION_STORAGE_TYPE_PRIVATIZED; + storage_type_ = PrivatizedData; flat_data_.clear(); page_numbers_.clear(); privatized_regions_ = std::move(data); @@ -253,8 +260,8 @@ public: remote_ptr end() const { return remote((char*)start_addr_ + size_); } remote_ptr permanent_address() const { return remote(permanent_addr_); } std::size_t size() const { return size_; } - mc_region_storage_type_t storage_type() const { return storage_type_; } - mc_region_type_t region_type() const { return region_type_; } + StorageType storage_type() const { return storage_type_; } + RegionType region_type() const { return region_type_; } bool contain(remote_ptr p) const { @@ -262,14 +269,14 @@ public: } }; -simgrid::mc::RegionSnapshot privatized_region( - mc_region_type_t type, void *start_addr, void* data_addr, size_t size); -simgrid::mc::RegionSnapshot dense_region( - mc_region_type_t type, void *start_addr, void* data_addr, size_t size); +RegionSnapshot privatized_region( + RegionType type, void *start_addr, void* data_addr, size_t size); +RegionSnapshot dense_region( + RegionType type, void *start_addr, void* data_addr, size_t size); simgrid::mc::RegionSnapshot sparse_region( - mc_region_type_t type, void *start_addr, void* data_addr, size_t size); + RegionType type, void *start_addr, void* data_addr, size_t size); simgrid::mc::RegionSnapshot region( - mc_region_type_t type, void *start_addr, void* data_addr, size_t size); + RegionType type, void *start_addr, void* data_addr, size_t size); } } diff --git a/src/mc/mc_checkpoint.cpp b/src/mc/mc_checkpoint.cpp index f813c74688..77bbfd6be1 100644 --- a/src/mc/mc_checkpoint.cpp +++ b/src/mc/mc_checkpoint.cpp @@ -84,21 +84,21 @@ extern "C" { static void MC_region_restore(mc_mem_region_t region) { switch(region->storage_type()) { - case MC_REGION_STORAGE_TYPE_NONE: + case simgrid::mc::StorageType::NoData: default: xbt_die("Storage type not supported"); break; - case MC_REGION_STORAGE_TYPE_FLAT: + case simgrid::mc::StorageType::Flat: mc_model_checker->process().write_bytes(region->flat_data().data(), region->size(), region->permanent_address()); break; - case MC_REGION_STORAGE_TYPE_CHUNKED: + case simgrid::mc::StorageType::Chunked: mc_region_restore_sparse(&mc_model_checker->process(), region); break; - case MC_REGION_STORAGE_TYPE_PRIVATIZED: + case simgrid::mc::StorageType::Privatized: for (auto& p : region->privatized_data()) MC_region_restore(&p); break; @@ -111,7 +111,7 @@ namespace simgrid { namespace mc { simgrid::mc::RegionSnapshot privatized_region( - mc_region_type_t region_type, void *start_addr, void* permanent_addr, size_t size + RegionType region_type, void *start_addr, void* permanent_addr, size_t size ) { size_t process_count = MC_smpi_process_count(); @@ -145,13 +145,14 @@ simgrid::mc::RegionSnapshot privatized_region( extern "C" { -static void MC_snapshot_add_region(int index, mc_snapshot_t snapshot, mc_region_type_t type, +static void MC_snapshot_add_region(int index, mc_snapshot_t snapshot, + simgrid::mc::RegionType type, mc_object_info_t object_info, void *start_addr, void* permanent_addr, size_t size) { - if (type == MC_REGION_TYPE_DATA) + if (type == simgrid::mc::RegionType::Data) xbt_assert(object_info, "Missing object info for object."); - else if (type == MC_REGION_TYPE_HEAP) + else if (type == simgrid::mc::RegionType::Heap) xbt_assert(!object_info, "Unexpected object info for heap region."); const bool privatization_aware = MC_object_info_is_privatized(object_info); @@ -176,7 +177,7 @@ static void MC_get_memory_regions(mc_process_t process, mc_snapshot_t snapshot) for (size_t i = 0; i!=n; ++i) { mc_object_info_t object_info = process->object_infos[i]; - MC_snapshot_add_region(i, snapshot, MC_REGION_TYPE_DATA, object_info, + MC_snapshot_add_region(i, snapshot, simgrid::mc::RegionType::Data, object_info, object_info->start_rw, object_info->start_rw, object_info->end_rw - object_info->start_rw); } @@ -185,7 +186,7 @@ static void MC_get_memory_regions(mc_process_t process, mc_snapshot_t snapshot) void *start_heap = heap->base; void *end_heap = heap->breakval; - MC_snapshot_add_region(n, snapshot, MC_REGION_TYPE_HEAP, NULL, + MC_snapshot_add_region(n, snapshot, simgrid::mc::RegionType::Heap, NULL, start_heap, start_heap, (char *) end_heap - (char *) start_heap); snapshot->heap_bytes_used = mmalloc_get_bytes_used_remote( diff --git a/src/mc/mc_compare.cpp b/src/mc/mc_compare.cpp index d9af408dfa..174b889644 100644 --- a/src/mc/mc_compare.cpp +++ b/src/mc/mc_compare.cpp @@ -268,9 +268,9 @@ static int compare_global_variables(mc_object_info_t object_info, xbt_assert(r1 && r2, "Missing region."); #ifdef HAVE_SMPI - if (r1->storage_type() == MC_REGION_STORAGE_TYPE_PRIVATIZED) { + if (r1->storage_type() == simgrid::mc::StorageType::Privatized) { xbt_assert(process_index >= 0); - if (r2->storage_type() != MC_REGION_STORAGE_TYPE_PRIVATIZED) { + if (r2->storage_type() != simgrid::mc::StorageType::Privatized) { return 1; } @@ -289,9 +289,9 @@ static int compare_global_variables(mc_object_info_t object_info, return 0; } #else - xbt_assert(r1->storage_type() != MC_REGION_STORAGE_TYPE_PRIVATIZED); + xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized); #endif - xbt_assert(r2->storage_type() != MC_REGION_STORAGE_TYPE_PRIVATIZED); + xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized); struct mc_compare_state state; @@ -589,7 +589,7 @@ int snapshot_compare(void *state1, void *state2) mc_mem_region_t region2 = s2->snapshot_regions[k]; // Preconditions: - if (region1->region_type() != MC_REGION_TYPE_DATA) + if (region1->region_type() != simgrid::mc::RegionType::Data) continue; xbt_assert(region1->region_type() == region2->region_type()); diff --git a/src/mc/mc_diff.cpp b/src/mc/mc_diff.cpp index e64d3a95a9..a037f849c7 100644 --- a/src/mc/mc_diff.cpp +++ b/src/mc/mc_diff.cpp @@ -421,7 +421,7 @@ mc_mem_region_t MC_get_heap_region(mc_snapshot_t snapshot) size_t n = snapshot->snapshot_regions_count; for (size_t i=0; i!=n; ++i) { mc_mem_region_t region = snapshot->snapshot_regions[i]; - if (region->region_type() == MC_REGION_TYPE_HEAP) + if (region->region_type() == simgrid::mc::RegionType::Heap) return region; } xbt_die("No heap region"); diff --git a/src/mc/mc_snapshot.cpp b/src/mc/mc_snapshot.cpp index 746453687c..89122a387e 100644 --- a/src/mc/mc_snapshot.cpp +++ b/src/mc/mc_snapshot.cpp @@ -32,7 +32,7 @@ mc_mem_region_t mc_get_snapshot_region( if (!(region && mc_region_contain(region, addr))) continue; - if (region->storage_type() == MC_REGION_STORAGE_TYPE_PRIVATIZED) { + if (region->storage_type() == simgrid::mc::StorageType::Privatized) { #ifdef HAVE_SMPI // Use the current process index of the snapshot: if (process_index == simgrid::mc::ProcessIndexDisabled) { @@ -114,8 +114,8 @@ int MC_snapshot_region_memcmp( // Using alloca() for large allocations may trigger stack overflow: // use malloc if the buffer is too big. bool stack_alloc = size < 64; - const bool region1_need_buffer = region1==NULL || region1->storage_type()==MC_REGION_STORAGE_TYPE_FLAT; - const bool region2_need_buffer = region2==NULL || region2->storage_type()==MC_REGION_STORAGE_TYPE_FLAT; + const bool region1_need_buffer = region1==NULL || region1->storage_type()==simgrid::mc::StorageType::Flat; + const bool region2_need_buffer = region2==NULL || region2->storage_type()==simgrid::mc::StorageType::Flat; void* buffer1a = region1_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size); void* buffer2a = region2_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size); const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size); @@ -257,12 +257,12 @@ static void test_snapshot(bool sparse_checkpoint) { // Init memory and take snapshots: init_memory(source, byte_size); simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region( - MC_REGION_TYPE_UNKNOWN, source, source, byte_size); + simgrid::mc::RegionType::Unknown, source, source, byte_size); for(int i=0; istorage_type()) { - case MC_REGION_STORAGE_TYPE_NONE: + case simgrid::mc::StorageType::NoData: default: xbt_die("Storage type not supported"); - case MC_REGION_STORAGE_TYPE_FLAT: + case simgrid::mc::StorageType::Flat: { uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address(); return (void *) ((uintptr_t) region->flat_data().data() + offset); } - case MC_REGION_STORAGE_TYPE_CHUNKED: + case simgrid::mc::StorageType::Chunked: return mc_translate_address_region_chunked(addr, region); - case MC_REGION_STORAGE_TYPE_PRIVATIZED: + case simgrid::mc::StorageType::Privatized: { xbt_assert(process_index >=0, "Missing process index for privatized region"); @@ -217,14 +217,14 @@ const void* MC_region_read(mc_mem_region_t region, void* target, const void* add "Trying to read out of the region boundary."); switch (region->storage_type()) { - case MC_REGION_STORAGE_TYPE_NONE: + case simgrid::mc::StorageType::NoData: default: xbt_die("Storage type not supported"); - case MC_REGION_STORAGE_TYPE_FLAT: + case simgrid::mc::StorageType::Flat: return (char*) region->flat_data().data() + offset; - case MC_REGION_STORAGE_TYPE_CHUNKED: + case simgrid::mc::StorageType::Chunked: { // Last byte of the region: void* end = (char*) addr + size - 1; @@ -239,7 +239,7 @@ const void* MC_region_read(mc_mem_region_t region, void* target, const void* add // We currently do not pass the process_index to this function so we assume // that the privatized region has been resolved in the callers: - case MC_REGION_STORAGE_TYPE_PRIVATIZED: + case simgrid::mc::StorageType::Privatized: xbt_die("Storage type not supported"); } } -- 2.20.1