-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
681: Cleanse backend memory args r=Saviq a=ricab Fixes #677 On investigation, back-end tools use slightly different formats to represent digital info sizes (bytes). So, our previous assumption that a *bytes string* would be correctly interpreted everywhere is not actually true. IOW, neither plain strings nor plain numbers are adequate to convey memory sizes uniformly, so the earlier *shortcut implementation* falls short. This PR encapsulates memory sizes in a dedicated type: `MemorySize`. This type does the necessary arithmetic, but it is the responsibility of each (internal) back-end implementation to derive the string representation they need (unit and unit representation). Co-authored-by: Ricardo Abreu <[email protected]>
- Loading branch information
Showing
20 changed files
with
524 additions
and
273 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
include/multipass/exceptions/invalid_memory_size_exception.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2019 Canonical, Ltd. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#ifndef MULTIPASS_INVALID_MEMORY_SIZE_EXCEPTION_H | ||
#define MULTIPASS_INVALID_MEMORY_SIZE_EXCEPTION_H | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <stdexcept> | ||
#include <string> | ||
|
||
namespace multipass | ||
{ | ||
class InvalidMemorySizeException : public std::runtime_error | ||
{ | ||
public: | ||
InvalidMemorySizeException(const std::string& val) | ||
: runtime_error(fmt::format("{} is not a valid memory size", val)) | ||
{ | ||
} | ||
}; | ||
} // namespace multipass | ||
#endif // MULTIPASS_INVALID_MEMORY_SIZE_EXCEPTION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2019 Canonical, Ltd. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#ifndef MULTIPASS_MEMORY_SIZE_H | ||
#define MULTIPASS_MEMORY_SIZE_H | ||
|
||
#include <string> | ||
|
||
namespace multipass | ||
{ | ||
class MemorySize | ||
{ | ||
public: | ||
friend bool operator==(const MemorySize& a, const MemorySize& b); | ||
friend bool operator!=(const MemorySize& a, const MemorySize& b); | ||
friend bool operator<(const MemorySize& a, const MemorySize& b); | ||
friend bool operator>(const MemorySize& a, const MemorySize& b); | ||
friend bool operator<=(const MemorySize& a, const MemorySize& b); | ||
friend bool operator>=(const MemorySize& a, const MemorySize& b); | ||
|
||
MemorySize(); | ||
explicit MemorySize(const std::string& val); | ||
long long in_bytes() const noexcept; | ||
long long in_kilobytes() const noexcept; | ||
long long in_megabytes() const noexcept; | ||
long long in_gigabytes() const noexcept; | ||
|
||
private: | ||
long long bytes; | ||
}; | ||
|
||
bool operator==(const MemorySize& a, const MemorySize& b); | ||
bool operator!=(const MemorySize& a, const MemorySize& b); | ||
bool operator<(const MemorySize& a, const MemorySize& b); | ||
bool operator>(const MemorySize& a, const MemorySize& b); | ||
bool operator<=(const MemorySize& a, const MemorySize& b); | ||
bool operator>=(const MemorySize& a, const MemorySize& b); | ||
|
||
} // namespace multipass | ||
|
||
#endif // MULTIPASS_MEMORY_SIZE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.