- Notifications
You must be signed in to change notification settings - Fork0
A C++11 utility for versatile memory size representation. Inspired by std::chrono, it offers types for memory sizes in both Base 10 and Base 2 systems, supporting compile-time literal operators and also both integer and floating-point backed types.
License
Nandite/Memory-Units
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This project provides a type named memory_size to represent the size of objects in memory.It has been inspired by thestd::chrono library,more specifically bystd::chrono::duration.
Template Parameters:
- Rep: Represents the type to store the size value (e.g., int, long, double).
- Factor: A std::ratio type representing the size unit (e.g., bytes, kilobytes, megabytes).It's a compile-time rational fraction defining the ratio of the size's unit to a byte.
Usage:
The mu::memory_size object can represent memory sizes with different granularities,like bytes, kilobytes, megabytes, etc., through its template parameters. Common predefinedtypes are available, which are specializations of the mu::memory_size template:
mu::bytesb(1024);// 1024 bytesmu::kilobyteskb(5);// 5 kilobytesmu::megabytesmb(75);// 75 megabytesmu::gigabytesgb(89);// 89 gigabytesmu::terabytestb(4);// 4 terabytesmu::petabytespb(977);// 977 petabytesmu::exabyteseb(7);// 7 exabytes
It supports various arithmetic operations like addition, subtraction, multiplication, and division:
auto total_bytes = b + kb + mb + gb + tb + pb + eb;// Adding all memory sizes togetherauto remaining_gb = gb - mb - kb - b;// Subtracting smaller units from gigabytesauto double_gb = gb *2;// Doubling the gigabytesauto five_times_mb = mb *5;// Multiplying megabytes by 5auto ratio_gb_to_mb = gb / mb;// How many times megabytes fit into gigabytesauto half_tb = tb /2;// Halving the terabytes
You can compare durations using relational operators:
mu::gigabytescompute_filesystem_size(...);mu::megabytescompute_available_cache_size(...);mu::megabytescompute_used_memory_in_cache(...);mu::bytesfree_memory(mu::bytes how_much_to_free, ....);// --------------------------------------auto size {computeFileSystemSize()};if(size >mu::gigabytes(20))// ... auto available_size{compute_available_cache_size()};if(available_size >mu::megabytes(20))// There is at least 20MB of non used memory in the cache auto used{compute_used_memory_in_cache(...)};auto freed{free_memory(used, ...)};if(freed == used)// All memory has been reclaimed
Type conversion between different units (e.g., megabytes to bytes) is also supported and automatically handled by the type'simplicit conversion mechanism.
std::vector<...>find_all_files_with_size_gt(mu::bytes threshold);voiddelete_file_if_size_gt(... file, mu::bytes threshold);// ...// --------------------------------------std::gigabytes max_size{10};// 10GBstd::kilobytes delete_threshold{512};auto files {find_all_files_with_size_gt(max_size)};for(constauto & f : files)delete_file_if_size_gt(f, delete_threshold);
A method for casting from one memory unit type to another is provided within thenamespace. This functionality enables explicit conversion between different units,such as converting from bytes to kilobytes or gigabytes to megabytes, ensuring easyunit conversion:
mu::byteslarge_number_of_bytes(5000000);// 5,000,000 bytes//...// Casting bytes to megabytesauto size_in_mb{mu::memory_size_cast<mu::megabytes>(large_number_of_bytes)};std::cout <<"5,000,000 bytes is" << size_in_mb.count() <<" megabytes." << std::endl;
The megabytes, gigabytes (etc.) are representation of size in the decimal system or Base 10:
using kb = std::ratio<1000 * b::num>;using mb = std::ratio<1000 * kb::num>;using gb = std::ratio<1000 * mb::num>;using tb = std::ratio<1000 * gb::num>;using pb = std::ratio<1000 * tb::num>;using eb = std::ratio<1000 * pb::num>;using kilobytes = memory_size<..., details::kb>;using megabytes = memory_size<..., details::mb>;using gigabytes = memory_size<..., details::gb>;using terabytes = memory_size<..., details::tb>;using petabytes = memory_size<..., details::pb>;using exabytes = memory_size<..., details::eb>;
In Base 10 (or decimal system), which is one of the main standards for measuring memory-related quantities, every unit is relatedby a factor of 1000
However, an alternative system is the binary system, or Base 2. In this system, units are related by a factorof 1024. This reflects the binary nature of computing, where data is processed in powers of 2. The namespaceprovides types for both these measurement systems:
using kib = std::ratio<1024 * b::num>;using mib = std::ratio<1024 * kib::num>;using gib = std::ratio<1024 * mib::num>;using tib = std::ratio<1024 * gib::num>;using pib = std::ratio<1024 * tib::num>;using eib = std::ratio<1024 * pib::num>;using kibibytes = memory_size<..., details::kib>;using mebibytes = memory_size<..., details::mib>;using gibibytes = memory_size<..., details::gib>;using tebibytes = memory_size<..., details::tib>;using pebibytes = memory_size<..., details::pib>;using exbibytes = memory_size<..., details::eib>;
The floating-point backed memory_size types are as usable as the Base 10 types in the provided usage examples.
Floating-point representation types for the memory_size are also supported in the implementation. These areparticularly useful in scenarios where precise memory amounts are needed, without the rounding that occurswith integer types. Floating-point backed memory_size types ensure accuracy across conversions between differentunits, maintaining the exactness of memory quantities even in fractional values:
using f_kilobytes = memory_size</* floating type rep*/, details::kb>;using f_megabytes = memory_size</* floating type rep*/, details::mb>;using f_gigabytes = memory_size</* floating typerep*/, details::gb>;using f_terrabytes = memory_size</* floating type rep*/, details::tb>;using f_petabytes = memory_size</* floating type rep*/, details::pb>;using f_exabytes = memory_size</* floating type rep*/, details::eb>;using f_kibibytes = memory_size</* floating type rep*/, details::kib>;using f_mebibytes = memory_size</* floating type rep*/, details::mib>;using f_gibibytes = memory_size</* floating type rep*/, details::gib>;using f_tebibytes = memory_size</* floating type rep*/, details::tib>;using f_pebibytes = memory_size</* floating type rep*/, details::pib>;using f_exbibytes = memory_size</* floating type rep*/, details::eib>;
Literal operators are available for all types from both Base 10 and Base 2 systems, enabling the creationof memory size constants at compile-time. These operators are part of the mu::literals namespace. Users canemploy these operators to specify memory sizes in their preferred unit:
// ...usingnamespacemu::literals;// ...auto size_kb = 256_kB;// 256 kilobytesauto size_mb = 128_MB;// 128 megabytesauto size_gb = 2_GB;// 2 gigabytesauto size_tb = 1_TB;// 1 terabyteauto size_pb = 3_PB;// 3 petabytesauto size_eb = 5_EB;// 5 exabytesauto size_kib = 256_kiB;// 256 kibibytesauto size_mib = 128_MiB;// 128 mebibytesauto size_gib = 2_GiB;// 2 gibibytesauto size_tib = 1_TiB;// 1 tebibyteauto size_pib = 3_PiB;// 3 petbibytesauto size_eib = 5_EiB;// 5 exbibytesif (size_gb < 5_GB)// ...if (2_TiB >= size_kib)// ...//...
The implementation is self-contained and needs only standard language support through a compiler supporting atleastC++11.
To run the test suite,Google Test (GTest) is required.
Just copy theheader file or its content into your project.
Don't hesitate if you have any suggestions for improving this project, or if you find any error. I will be glad tohear from you. Contributions are welcomed :)
Distributed under the MIT Software License (X11 license).See the accompanyingLICENSE file.
About
A C++11 utility for versatile memory size representation. Inspired by std::chrono, it offers types for memory sizes in both Base 10 and Base 2 systems, supporting compile-time literal operators and also both integer and floating-point backed types.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.