Use the new assert that doesn't crash the program in a release build

This commit is contained in:
Sebastian Messmer 2015-07-22 13:42:07 +02:00
parent 37bdbd907a
commit fd93e4c199
13 changed files with 32 additions and 20 deletions

View File

@ -5,6 +5,7 @@
#include <algorithm> #include <algorithm>
#include <messmer/cpp-utils/pointer/cast.h> #include <messmer/cpp-utils/pointer/cast.h>
#include <messmer/cpp-utils/assert/assert.h>
using cpputils::dynamic_pointer_move; using cpputils::dynamic_pointer_move;
using cpputils::Data; using cpputils::Data;
@ -47,7 +48,7 @@ optional<unique_ref<Block>> CachingBlockStore::load(const Key &key) {
void CachingBlockStore::remove(cpputils::unique_ref<Block> block) { void CachingBlockStore::remove(cpputils::unique_ref<Block> block) {
auto cached_block = dynamic_pointer_move<CachedBlock>(block); auto cached_block = dynamic_pointer_move<CachedBlock>(block);
assert(cached_block != none); ASSERT(cached_block != none, "Passed block is not a CachedBlock");
auto baseBlock = (*cached_block)->releaseBlock(); auto baseBlock = (*cached_block)->releaseBlock();
auto baseNewBlock = dynamic_pointer_move<NewBlock>(baseBlock); auto baseNewBlock = dynamic_pointer_move<NewBlock>(baseBlock);
if (baseNewBlock != none) { if (baseNewBlock != none) {

View File

@ -1,5 +1,6 @@
#include "NewBlock.h" #include "NewBlock.h"
#include "CachingBlockStore.h" #include "CachingBlockStore.h"
#include <messmer/cpp-utils/assert/assert.h>
using cpputils::Data; using cpputils::Data;
using boost::none; using boost::none;
@ -24,7 +25,7 @@ const void *NewBlock::data() const {
} }
void NewBlock::write(const void *source, uint64_t offset, uint64_t size) { void NewBlock::write(const void *source, uint64_t offset, uint64_t size) {
assert(offset <= _data.size() && offset + size <= _data.size()); ASSERT(offset <= _data.size() && offset + size <= _data.size(), "Write outside of valid area");
std::memcpy((uint8_t*)_data.data()+offset, source, size); std::memcpy((uint8_t*)_data.data()+offset, source, size);
_dataChanged = true; _dataChanged = true;
} }
@ -34,7 +35,7 @@ void NewBlock::writeToBaseBlockIfChanged() {
if (_baseBlock == none) { if (_baseBlock == none) {
//TODO _data.copy() necessary? //TODO _data.copy() necessary?
auto newBase = _blockStore->tryCreateInBaseStore(key(), _data.copy()); auto newBase = _blockStore->tryCreateInBaseStore(key(), _data.copy());
assert(newBase != boost::none); //TODO What if tryCreate fails due to a duplicate key? We should ensure we don't use duplicate keys. ASSERT(newBase != boost::none, "Couldn't create base block"); //TODO What if tryCreate fails due to a duplicate key? We should ensure we don't use duplicate keys.
_baseBlock = std::move(*newBase); _baseBlock = std::move(*newBase);
} else { } else {
(*_baseBlock)->write(_data.data(), 0, _data.size()); (*_baseBlock)->write(_data.data(), 0, _data.size());
@ -52,7 +53,7 @@ void NewBlock::remove() {
void NewBlock::flush() { void NewBlock::flush() {
writeToBaseBlockIfChanged(); writeToBaseBlockIfChanged();
assert(_baseBlock != none); ASSERT(_baseBlock != none, "At this point, the base block should already have been created but wasn't");
(*_baseBlock)->flush(); (*_baseBlock)->flush();
} }

View File

@ -9,6 +9,7 @@
#include <mutex> #include <mutex>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <future> #include <future>
#include <messmer/cpp-utils/assert/assert.h>
namespace blockstore { namespace blockstore {
namespace caching { namespace caching {
@ -66,10 +67,10 @@ boost::optional<Value> Cache<Key, Value>::pop(const Key &key) {
template<class Key, class Value> template<class Key, class Value>
void Cache<Key, Value>::push(const Key &key, Value value) { void Cache<Key, Value>::push(const Key &key, Value value) {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
assert(_cachedBlocks.size() <= MAX_ENTRIES); ASSERT(_cachedBlocks.size() <= MAX_ENTRIES, "Cache too full");
if (_cachedBlocks.size() == MAX_ENTRIES) { if (_cachedBlocks.size() == MAX_ENTRIES) {
_cachedBlocks.pop(); _cachedBlocks.pop();
assert(_cachedBlocks.size() == MAX_ENTRIES-1); ASSERT(_cachedBlocks.size() == MAX_ENTRIES-1, "Removing entry from cache didn't work");
} }
_cachedBlocks.push(key, CacheEntry<Key, Value>(std::move(value))); _cachedBlocks.push(key, CacheEntry<Key, Value>(std::move(value)));
} }

View File

@ -11,6 +11,7 @@
#include <iostream> #include <iostream>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include "ciphers/Cipher.h" #include "ciphers/Cipher.h"
#include <messmer/cpp-utils/assert/assert.h>
namespace blockstore { namespace blockstore {
namespace encrypted { namespace encrypted {
@ -123,7 +124,7 @@ const void *EncryptedBlock<Cipher>::data() const {
template<class Cipher> template<class Cipher>
void EncryptedBlock<Cipher>::write(const void *source, uint64_t offset, uint64_t count) { void EncryptedBlock<Cipher>::write(const void *source, uint64_t offset, uint64_t count) {
assert(offset <= size() && offset + count <= size()); //Also check offset < size() because of possible overflow in the addition ASSERT(offset <= size() && offset + count <= size(), "Write outside of valid area"); //Also check offset < size() because of possible overflow in the addition
std::memcpy((uint8_t*)_plaintextWithHeader.data()+HEADER_LENGTH+offset, source, count); std::memcpy((uint8_t*)_plaintextWithHeader.data()+HEADER_LENGTH+offset, source, count);
_dataChanged = true; _dataChanged = true;
} }

View File

@ -69,7 +69,7 @@ boost::optional<cpputils::unique_ref<Block>> EncryptedBlockStore<Cipher>::load(c
template<class Cipher> template<class Cipher>
void EncryptedBlockStore<Cipher>::remove(cpputils::unique_ref<Block> block) { void EncryptedBlockStore<Cipher>::remove(cpputils::unique_ref<Block> block) {
auto encryptedBlock = cpputils::dynamic_pointer_move<EncryptedBlock<Cipher>>(block); auto encryptedBlock = cpputils::dynamic_pointer_move<EncryptedBlock<Cipher>>(block);
assert(encryptedBlock != boost::none); ASSERT(encryptedBlock != boost::none, "Block is not an EncryptedBlock");
auto baseBlock = (*encryptedBlock)->releaseBlock(); auto baseBlock = (*encryptedBlock)->releaseBlock();
return _baseBlockStore->remove(std::move(baseBlock)); return _baseBlockStore->remove(std::move(baseBlock));
} }

View File

@ -1,6 +1,7 @@
#include "InMemoryBlock.h" #include "InMemoryBlock.h"
#include "InMemoryBlockStore.h" #include "InMemoryBlockStore.h"
#include <cstring> #include <cstring>
#include <messmer/cpp-utils/assert/assert.h>
using std::make_shared; using std::make_shared;
using std::istream; using std::istream;
@ -29,7 +30,7 @@ const void *InMemoryBlock::data() const {
} }
void InMemoryBlock::write(const void *source, uint64_t offset, uint64_t size) { void InMemoryBlock::write(const void *source, uint64_t offset, uint64_t size) {
assert(offset <= _data->size() && offset + size <= _data->size()); //Also check offset < _data->size() because of possible overflow in the addition ASSERT(offset <= _data->size() && offset + size <= _data->size(), "Write outside of valid area"); //Also check offset < _data->size() because of possible overflow in the addition
std::memcpy((uint8_t*)_data->data()+offset, source, size); std::memcpy((uint8_t*)_data->data()+offset, source, size);
} }

View File

@ -1,6 +1,7 @@
#include "InMemoryBlock.h" #include "InMemoryBlock.h"
#include "InMemoryBlockStore.h" #include "InMemoryBlockStore.h"
#include <memory> #include <memory>
#include <messmer/cpp-utils/assert/assert.h>
using std::make_unique; using std::make_unique;
using std::string; using std::string;
@ -44,7 +45,7 @@ void InMemoryBlockStore::remove(unique_ref<Block> block) {
Key key = block->key(); Key key = block->key();
cpputils::destruct(std::move(block)); cpputils::destruct(std::move(block));
int numRemoved = _blocks.erase(key.ToString()); int numRemoved = _blocks.erase(key.ToString());
assert(1==numRemoved); ASSERT(1==numRemoved, "Didn't find block to remove");
} }
uint64_t InMemoryBlockStore::numBlocks() const { uint64_t InMemoryBlockStore::numBlocks() const {

View File

@ -4,6 +4,7 @@
#include "OnDiskBlock.h" #include "OnDiskBlock.h"
#include "OnDiskBlockStore.h" #include "OnDiskBlockStore.h"
#include "../../utils/FileDoesntExistException.h" #include "../../utils/FileDoesntExistException.h"
#include <messmer/cpp-utils/assert/assert.h>
using std::istream; using std::istream;
using std::ostream; using std::ostream;
@ -34,7 +35,7 @@ const void *OnDiskBlock::data() const {
} }
void OnDiskBlock::write(const void *source, uint64_t offset, uint64_t size) { void OnDiskBlock::write(const void *source, uint64_t offset, uint64_t size) {
assert(offset <= _data.size() && offset + size <= _data.size()); //Also check offset < _data->size() because of possible overflow in the addition ASSERT(offset <= _data.size() && offset + size <= _data.size(), "Write outside of valid area"); //Also check offset < _data->size() because of possible overflow in the addition
std::memcpy((uint8_t*)_data.data()+offset, source, size); std::memcpy((uint8_t*)_data.data()+offset, source, size);
_dataChanged = true; _dataChanged = true;
} }
@ -76,7 +77,7 @@ optional<unique_ref<OnDiskBlock>> OnDiskBlock::CreateOnDisk(const bf::path &root
void OnDiskBlock::RemoveFromDisk(const bf::path &rootdir, const Key &key) { void OnDiskBlock::RemoveFromDisk(const bf::path &rootdir, const Key &key) {
auto filepath = rootdir / key.ToString(); auto filepath = rootdir / key.ToString();
assert(bf::is_regular_file(filepath)); ASSERT(bf::is_regular_file(filepath), "Block not found on disk");
bf::remove(filepath); bf::remove(filepath);
} }

View File

@ -3,6 +3,7 @@
#include "ParallelAccessBlockStoreAdapter.h" #include "ParallelAccessBlockStoreAdapter.h"
#include <cassert> #include <cassert>
#include <messmer/cpp-utils/pointer/cast.h> #include <messmer/cpp-utils/pointer/cast.h>
#include <messmer/cpp-utils/assert/assert.h>
using std::string; using std::string;
using std::mutex; using std::mutex;
@ -48,7 +49,7 @@ optional<unique_ref<Block>> ParallelAccessBlockStore::load(const Key &key) {
void ParallelAccessBlockStore::remove(unique_ref<Block> block) { void ParallelAccessBlockStore::remove(unique_ref<Block> block) {
Key key = block->key(); Key key = block->key();
auto block_ref = dynamic_pointer_move<BlockRef>(block); auto block_ref = dynamic_pointer_move<BlockRef>(block);
assert(block_ref != none); ASSERT(block_ref != none, "Block is not a BlockRef");
return _parallelAccessStore.remove(key, std::move(*block_ref)); return _parallelAccessStore.remove(key, std::move(*block_ref));
} }

View File

@ -1,6 +1,7 @@
#include "FakeBlock.h" #include "FakeBlock.h"
#include "FakeBlockStore.h" #include "FakeBlockStore.h"
#include <cstring> #include <cstring>
#include <messmer/cpp-utils/assert/assert.h>
using std::shared_ptr; using std::shared_ptr;
using std::istream; using std::istream;
@ -27,7 +28,7 @@ const void *FakeBlock::data() const {
} }
void FakeBlock::write(const void *source, uint64_t offset, uint64_t size) { void FakeBlock::write(const void *source, uint64_t offset, uint64_t size) {
assert(offset <= _data->size() && offset + size <= _data->size()); //Also check offset < _data->size() because of possible overflow in the addition ASSERT(offset <= _data->size() && offset + size <= _data->size(), "Write outside of valid area"); //Also check offset < _data->size() because of possible overflow in the addition
std::memcpy((uint8_t*)_data->data()+offset, source, size); std::memcpy((uint8_t*)_data->data()+offset, source, size);
_dataChanged = true; _dataChanged = true;
} }

View File

@ -1,5 +1,6 @@
#include "FakeBlock.h" #include "FakeBlock.h"
#include "FakeBlockStore.h" #include "FakeBlockStore.h"
#include <messmer/cpp-utils/assert/assert.h>
using std::make_shared; using std::make_shared;
using std::string; using std::string;
@ -42,7 +43,7 @@ void FakeBlockStore::remove(unique_ref<Block> block) {
Key key = block->key(); Key key = block->key();
cpputils::destruct(std::move(block)); cpputils::destruct(std::move(block));
int numRemoved = _blocks.erase(key.ToString()); int numRemoved = _blocks.erase(key.ToString());
assert(numRemoved == 1); ASSERT(numRemoved == 1, "Block not found");
} }
unique_ref<Block> FakeBlockStore::makeFakeBlockFromData(const Key &key, const Data &data, bool dirty) { unique_ref<Block> FakeBlockStore::makeFakeBlockFromData(const Key &key, const Data &data, bool dirty) {
@ -55,11 +56,11 @@ void FakeBlockStore::updateData(const Key &key, const Data &data) {
auto found = _blocks.find(key.ToString()); auto found = _blocks.find(key.ToString());
if (found == _blocks.end()) { if (found == _blocks.end()) {
auto insertResult = _blocks.emplace(key.ToString(), data.copy()); auto insertResult = _blocks.emplace(key.ToString(), data.copy());
assert(true == insertResult.second); ASSERT(true == insertResult.second, "Inserting didn't work");
found = insertResult.first; found = insertResult.first;
} }
Data &stored_data = found->second; Data &stored_data = found->second;
assert(data.size() == stored_data.size()); ASSERT(data.size() == stored_data.size(), "Wrong data size in block");
std::memcpy(stored_data.data(), data.data(), data.size()); std::memcpy(stored_data.data(), data.data(), data.size());
} }

View File

@ -4,6 +4,7 @@
#include <messmer/cpp-utils/macros.h> #include <messmer/cpp-utils/macros.h>
#include <cassert> #include <cassert>
#include <messmer/cpp-utils/assert/assert.h>
// This is a not-default-constructible non-copyable but moveable Value type // This is a not-default-constructible non-copyable but moveable Value type
class MinimalValueType { class MinimalValueType {
@ -19,13 +20,13 @@ public:
} }
~MinimalValueType() { ~MinimalValueType() {
assert(!_isDestructed); ASSERT(!_isDestructed, "Object was already destructed before");
--instances; --instances;
_isDestructed = true; _isDestructed = true;
} }
int value() const { int value() const {
assert(!_isMoved && !_isDestructed); ASSERT(!_isMoved && !_isDestructed, "Object is invalid");
return _value; return _value;
} }

View File

@ -2,6 +2,7 @@
#include "BlockStoreUtils.h" #include "BlockStoreUtils.h"
#include <messmer/cpp-utils/data/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include <cassert> #include <cassert>
#include <messmer/cpp-utils/assert/assert.h>
using cpputils::Data; using cpputils::Data;
using cpputils::unique_ref; using cpputils::unique_ref;
@ -16,7 +17,7 @@ unique_ref<Block> copyToNewBlock(BlockStore *blockStore, const Block &block) {
} }
void copyTo(Block *target, const Block &source) { void copyTo(Block *target, const Block &source) {
assert(target->size() == source.size()); ASSERT(target->size() == source.size(), "Can't copy block data when blocks have different sizes");
target->write(source.data(), 0, source.size()); target->write(source.data(), 0, source.size());
} }