Skip to content

Commit

Permalink
std::size_t → usize
Browse files Browse the repository at this point in the history
  • Loading branch information
extratype committed May 3, 2022
1 parent d074d69 commit 2c5ec6a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
20 changes: 10 additions & 10 deletions base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using std::bad_alloc;
namespace chunkdisk
{

static constexpr auto MAX_CHUNK_PARTS = size_t(16384);
static constexpr auto MAX_CHUNK_PARTS = usize(16384);

ChunkRange ChunkDiskBase::BlockChunkRange(u64 block_addr, u64 count) const
{
Expand Down Expand Up @@ -66,7 +66,7 @@ PageRange ChunkDiskBase::BlockPageRange(u64 chunk_idx, u64 start_off, u64 end_of
}

template <class F>
DWORD ChunkDiskBase::IterPart(const size_t part_idx, F&& func)
DWORD ChunkDiskBase::IterPart(const usize part_idx, F&& func)
{
try
{
Expand Down Expand Up @@ -126,7 +126,7 @@ DWORD ChunkDiskBase::Start()
// disallow writing on base until merged: persistent .lock
const auto flags_attrs = FILE_ATTRIBUTE_NORMAL | (read_only ? 0 : FILE_FLAG_DELETE_ON_CLOSE);

for (auto i = size_t(0); i < num_parts; ++i)
for (auto i = usize(0); i < num_parts; ++i)
{
auto path = part_dirname[i] + L"\\.lock";
auto h = FileHandle(CreateFileW(
Expand All @@ -145,9 +145,9 @@ DWORD ChunkDiskBase::Start()

// read parts to check chunks
auto part_current = std::vector<u64>(num_parts, 0);
auto chunk_parts = std::unordered_map<u64, size_t>();
auto chunk_parts = std::unordered_map<u64, usize>();

for (auto i = size_t(0); i < num_parts; ++i)
for (auto i = usize(0); i < num_parts; ++i)
{
auto err = IterPart(i, [this, i, &part_current, &chunk_parts](u64 idx) -> DWORD
{
Expand Down Expand Up @@ -187,7 +187,7 @@ DWORD ChunkDiskBase::Start()
return ERROR_SUCCESS;
}

DWORD ChunkDiskBase::ChunkPath(const u64 chunk_idx, const size_t part_idx, std::wstring& path) const
DWORD ChunkDiskBase::ChunkPath(const u64 chunk_idx, const usize part_idx, std::wstring& path) const
{
try
{
Expand All @@ -200,7 +200,7 @@ DWORD ChunkDiskBase::ChunkPath(const u64 chunk_idx, const size_t part_idx, std::
}
}

DWORD ChunkDiskBase::FindChunkPart(const u64 chunk_idx, size_t& part_idx, SRWLock& lk)
DWORD ChunkDiskBase::FindChunkPart(const u64 chunk_idx, usize& part_idx, SRWLock& lk)
{
if (!lk) lk = SRWLock(*mutex_parts_, false);
auto it = chunk_parts_.find(chunk_idx);
Expand All @@ -226,9 +226,9 @@ DWORD ChunkDiskBase::FindChunkPart(const u64 chunk_idx, size_t& part_idx, SRWLoc
auto err = [this, chunk_idx, &part_idx]() -> DWORD
{
const auto num_parts = part_dirname.size();
auto i = size_t(0);
auto i = usize(0);
auto err = DWORD(ERROR_SUCCESS);
auto idx = size_t(num_parts);
auto idx = usize(num_parts);

for (; i < num_parts; ++i)
{
Expand Down Expand Up @@ -542,7 +542,7 @@ DWORD ChunkDiskBase::CreateChunk(const u64 chunk_idx, FileHandle& handle_out,

void ChunkDiskBase::RemoveChunkLocked(const u64 chunk_idx, FileHandle handle)
{
auto num_parts = size_t(part_dirname.size());
auto num_parts = usize(part_dirname.size());
auto part_idx = num_parts;
auto lk = SRWLock(*mutex_parts_, true);
auto err = FindChunkPart(chunk_idx, part_idx, lk);
Expand Down
12 changes: 6 additions & 6 deletions base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ class ChunkDiskBase
std::unique_ptr<std::shared_mutex> mutex_parts_;
std::vector<u64> part_current_; // part index -> # of chunks
// maybe less than actual, refresh at max.
size_t part_current_new_ = 0; // part index for new chunks
usize part_current_new_ = 0; // part index for new chunks
// chunks are never removed so remember the last result
Map<u64, size_t> chunk_parts_; // cached: add to back, evict from front
Map<u64, usize> chunk_parts_; // cached: add to back, evict from front
// chunk index -> part index
// part_dirname.size() if not found

Expand Down Expand Up @@ -107,7 +107,7 @@ class ChunkDiskBase
bool IsWholePages(u64 start_off, u64 end_off, void* buffer = nullptr) const
{
return start_off == 0 && end_off == page_length &&
(buffer == nullptr || recast<size_t>(buffer) % PageBytes(1) == 0);
(buffer == nullptr || recast<usize>(buffer) % PageBytes(1) == 0);
}

// chunks
Expand Down Expand Up @@ -144,15 +144,15 @@ class ChunkDiskBase
// func(chunk_idx) for chunks in part
// stop when func() returns an error
template <class F>
DWORD IterPart(size_t part_idx, F&& func);
DWORD IterPart(usize part_idx, F&& func);

DWORD ChunkPath(u64 chunk_idx, size_t part_idx, std::wstring& path) const;
DWORD ChunkPath(u64 chunk_idx, usize part_idx, std::wstring& path) const;

// loop over parts or get cached result
// lk: empty or lock mutex_parts_
// part_idx == part_dirname.size() if not found
// holding mutex_parts_ if successful
DWORD FindChunkPart(u64 chunk_idx, size_t& part_idx, SRWLock& lk);
DWORD FindChunkPart(u64 chunk_idx, usize& part_idx, SRWLock& lk);

// update part_current_new_
// lock mutex_parts_ and call this
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ DWORD ReadChunkDiskFile(PCWSTR chunkdisk_file, const bool read_only, const bool
return ERROR_INVALID_PARAMETER;
}

auto buf = unique_ptr<u8[]>(new u8[size_t(size.LowPart)]);
auto buf = unique_ptr<u8[]>(new u8[usize(size.LowPart)]);
auto bytes_read = DWORD();
if (!ReadFile(h.get(), buf.get(), size.LowPart, &bytes_read, nullptr))
{
Expand Down
4 changes: 2 additions & 2 deletions service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,9 @@ DWORD ChunkDiskService::FlushPages()
return cached_pages_.empty() ? ERROR_SUCCESS : err;
}

size_t ChunkDiskService::FindChunk(const u64 chunk_idx)
usize ChunkDiskService::FindChunk(const u64 chunk_idx)
{
auto i = size_t(0);
auto i = usize(0);
const auto n = bases.size();
for (; i < n; ++i)
{
Expand Down
2 changes: 1 addition & 1 deletion service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ChunkDiskService
DWORD FlushPages();

// CheckChunk() from current to parents, return bases.size() if all false.
size_t FindChunk(u64 chunk_idx);
usize FindChunk(u64 chunk_idx);

// Open a chunk file handle for I/O. See ChunkDiskBase::CreateChunk().
// base[0] used if is_write
Expand Down
8 changes: 4 additions & 4 deletions types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef std::int64_t i64;
typedef std::uint8_t u8;
typedef std::uint32_t u32;
typedef std::uint64_t u64;
using std::size_t;
typedef std::size_t usize;

template <class T, class U>
static constexpr T recast(U arg)
Expand Down Expand Up @@ -129,7 +129,7 @@ class Map

bool empty() const noexcept { return map_.empty(); }

size_t size() const noexcept { return map_.size(); }
usize size() const noexcept { return map_.size(); }

void clear() noexcept
{
Expand Down Expand Up @@ -191,7 +191,7 @@ class Map
erase(find(*key_order_.back()));
}

size_t erase(const KT& key)
usize erase(const KT& key)
{
auto it = map_.find(key);
if (it == map_.end()) return 0;
Expand All @@ -216,7 +216,7 @@ class Map
return iterator(&map_, map_.find(key), key_order_.end());
}

void reserve(size_t count) { map_.reserve(count); }
void reserve(usize count) { map_.reserve(count); }
};

}
Expand Down
4 changes: 2 additions & 2 deletions utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class SRWLock : public SRWLockBase<SRWLock>
void on_unlock(bool is_exclusive) {}
};

template <class T, class Deleter, size_t defval = 0>
template <class T, class Deleter, usize defval = 0>
struct GenericDeleter
{
void operator()(T x) noexcept
Expand Down Expand Up @@ -238,7 +238,7 @@ struct HandleDeleter

using GenericHandle = std::unique_ptr<void, GenericDeleter<HANDLE, HandleDeleter>>;

using FileHandle = std::unique_ptr<void, GenericDeleter<HANDLE, HandleDeleter, size_t(-1)>>;
using FileHandle = std::unique_ptr<void, GenericDeleter<HANDLE, HandleDeleter, usize(-1)>>;

struct PagesDeleter
{
Expand Down

0 comments on commit 2c5ec6a

Please sign in to comment.