{"id":15047391,"url":"https://github.com/nandite/memory-units","last_synced_at":"2026-02-02T18:07:55.707Z","repository":{"id":217054323,"uuid":"743002238","full_name":"Nandite/Memory-Units","owner":"Nandite","description":"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. ","archived":false,"fork":false,"pushed_at":"2024-01-14T15:03:53.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-25T18:09:44.034Z","etag":null,"topics":["cpp","cpp11","memory","metaprogram","metaprogramming","size","size-calculation","units","units-converter","units-measures-converter","units-of-measure","units-of-measurement","unitsofmeasurement"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Nandite.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-14T02:14:54.000Z","updated_at":"2024-01-14T02:32:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"fd6c384f-ad71-43ad-a620-f5a7e2829c8c","html_url":"https://github.com/Nandite/Memory-Units","commit_stats":null,"previous_names":["nandite/memory-units"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Nandite/Memory-Units","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nandite%2FMemory-Units","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nandite%2FMemory-Units/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nandite%2FMemory-Units/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nandite%2FMemory-Units/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Nandite","download_url":"https://codeload.github.com/Nandite/Memory-Units/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nandite%2FMemory-Units/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261927585,"owners_count":23231379,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp11","memory","metaprogram","metaprogramming","size","size-calculation","units","units-converter","units-measures-converter","units-of-measure","units-of-measurement","unitsofmeasurement"],"created_at":"2024-09-24T20:57:30.678Z","updated_at":"2026-02-02T18:07:55.653Z","avatar_url":"https://github.com/Nandite.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"Memory-Units\n===============\n\n![license](https://img.shields.io/badge/license-MIT-green.svg?style=flat-square)\n![platform-image](https://img.shields.io/badge/platorms-linux64%20%7C%20osx%20%7C%20windows-lightgrey?style=flat-square)\n![language](https://img.shields.io/badge/language-c++-blue.svg?style=flat-square)\n![c++](https://img.shields.io/badge/std-c++11-blue.svg?style=flat-square)\n\n\nThis project provides a type named memory_size to represent the size of objects in memory. \nIt has been inspired by the [std::chrono](https://en.cppreference.com/w/cpp/chrono) library, \nmore specifically by [std::chrono::duration](https://en.cppreference.com/w/cpp/chrono/duration).\n\n\n\nTemplate Parameters:\n* Rep: Represents the type to store the size value (e.g., int, long, double).\n* Factor: A std::ratio type representing the size unit (e.g., bytes, kilobytes, megabytes). \nIt's a compile-time rational fraction defining the ratio of the size's unit to a byte.\n\nUsage:\n\nThe mu::memory_size object can represent memory sizes with different granularities, \nlike bytes, kilobytes, megabytes, etc., through its template parameters. Common predefined \ntypes are available, which are specializations of the mu::memory_size template:\n\n```c++\nmu::bytes b(1024); // 1024 bytes\nmu::kilobytes kb(5); // 5 kilobytes\nmu::megabytes mb(75); // 75 megabytes\nmu::gigabytes gb(89); // 89 gigabytes\nmu::terabytes tb(4); // 4 terabytes\nmu::petabytes pb(977); // 977 petabytes\nmu::exabytes eb(7); // 7 exabytes\n```\nIt supports various arithmetic operations like addition, subtraction, multiplication, and division:\n```c++\nauto total_bytes = b + kb + mb + gb + tb + pb + eb; // Adding all memory sizes together\nauto remaining_gb = gb - mb - kb - b; // Subtracting smaller units from gigabytes\nauto double_gb = gb * 2; // Doubling the gigabytes\nauto five_times_mb = mb * 5; // Multiplying megabytes by 5\nauto ratio_gb_to_mb = gb / mb; // How many times megabytes fit into gigabytes\nauto half_tb = tb / 2; // Halving the terabytes\n```\n\nYou can compare durations using relational operators:\n\n```c++\nmu::gigabytes compute_filesystem_size(...);\nmu::megabytes compute_available_cache_size(...);\nmu::megabytes compute_used_memory_in_cache(...);\nmu::bytes free_memory(mu::bytes how_much_to_free, ....);\n// --------------------------------------\nauto size {computeFileSystemSize()};\nif(size \u003e mu::gigabytes(20))\n    // ...\n   \nauto available_size{compute_available_cache_size()};\nif(available_size \u003e mu::megabytes(20))\n    // There is at least 20MB of non used memory in the cache\n  \nauto used{compute_used_memory_in_cache(...)};  \nauto freed{free_memory(used, ...)};\nif(freed == used)\n    // All memory has been reclaimed\n```\n\nType conversion between different units (e.g., megabytes to bytes) is also supported and automatically handled by the type's \nimplicit conversion mechanism.\n```c++\nstd::vector\u003c...\u003e find_all_files_with_size_gt(mu::bytes threshold);\nvoid delete_file_if_size_gt(... file, mu::bytes threshold);\n// ...\n// --------------------------------------\n\nstd::gigabytes max_size{10}; // 10GB\nstd::kilobytes delete_threshold{512};\n\nauto files {find_all_files_with_size_gt(max_size)};\nfor(const auto \u0026 f : files)\n    delete_file_if_size_gt(f, delete_threshold); \n```\n\nA method for casting from one memory unit type to another is provided within the\nnamespace. This functionality enables explicit conversion between different units,\nsuch as converting from bytes to kilobytes or gigabytes to megabytes, ensuring easy\nunit conversion:\n\n```c++\nmu::bytes large_number_of_bytes(5000000); // 5,000,000 bytes\n//...\n\n// Casting bytes to megabytes\nauto size_in_mb{mu::memory_size_cast\u003cmu::megabytes\u003e(large_number_of_bytes)};\nstd::cout \u003c\u003c \"5,000,000 bytes is \" \u003c\u003c size_in_mb.count() \u003c\u003c \" megabytes.\" \u003c\u003c std::endl;\n```\n## Base 2 vs Base 10\n\nThe megabytes, gigabytes (etc.) are representation of size in the decimal system or Base 10:\n```c++\nusing kb = std::ratio\u003c1000 * b::num\u003e;\nusing mb = std::ratio\u003c1000 * kb::num\u003e;\nusing gb = std::ratio\u003c1000 * mb::num\u003e;\nusing tb = std::ratio\u003c1000 * gb::num\u003e;\nusing pb = std::ratio\u003c1000 * tb::num\u003e;\nusing eb = std::ratio\u003c1000 * pb::num\u003e;\n\nusing kilobytes = memory_size\u003c..., details::kb\u003e;\nusing megabytes = memory_size\u003c..., details::mb\u003e;\nusing gigabytes = memory_size\u003c..., details::gb\u003e;\nusing terabytes = memory_size\u003c..., details::tb\u003e;\nusing petabytes = memory_size\u003c..., details::pb\u003e;\nusing exabytes = memory_size\u003c..., details::eb\u003e;\n```\nIn Base 10 (or decimal system), which is one of the main standards for measuring memory-related quantities, every unit is related \nby a factor of 1000\n\nHowever, an alternative system is the binary system, or Base 2. In this system, units are related by a factor \nof 1024. This reflects the binary nature of computing, where data is processed in powers of 2. The namespace \nprovides types for both these measurement systems:\n\n```c++\nusing kib = std::ratio\u003c1024 * b::num\u003e;\nusing mib = std::ratio\u003c1024 * kib::num\u003e;\nusing gib = std::ratio\u003c1024 * mib::num\u003e;\nusing tib = std::ratio\u003c1024 * gib::num\u003e;\nusing pib = std::ratio\u003c1024 * tib::num\u003e;\nusing eib = std::ratio\u003c1024 * pib::num\u003e;\n\nusing kibibytes = memory_size\u003c..., details::kib\u003e;\nusing mebibytes = memory_size\u003c..., details::mib\u003e;\nusing gibibytes = memory_size\u003c..., details::gib\u003e;\nusing tebibytes = memory_size\u003c..., details::tib\u003e;\nusing pebibytes = memory_size\u003c..., details::pib\u003e;\nusing exbibytes = memory_size\u003c..., details::eib\u003e;\n```\nThe floating-point backed memory_size types are as usable as the Base 10 types in the provided usage examples.\n\n## Floating types\n\nFloating-point representation types for the memory_size are also supported in the implementation. These are\nparticularly useful in scenarios where precise memory amounts are needed, without the rounding that occurs \nwith integer types. Floating-point backed memory_size types ensure accuracy across conversions between different \nunits, maintaining the exactness of memory quantities even in fractional values:\n\n```c++\nusing f_kilobytes = memory_size\u003c /* floating type rep*/, details::kb\u003e;\nusing f_megabytes = memory_size\u003c/* floating type rep*/, details::mb\u003e;\nusing f_gigabytes = memory_size\u003c/* floating typerep*/, details::gb\u003e;\nusing f_terrabytes = memory_size\u003c/* floating type rep*/, details::tb\u003e;\nusing f_petabytes = memory_size\u003c/* floating type rep*/, details::pb\u003e;\nusing f_exabytes = memory_size\u003c/* floating type rep*/, details::eb\u003e;\n\nusing f_kibibytes = memory_size\u003c/* floating type rep*/, details::kib\u003e;\nusing f_mebibytes = memory_size\u003c/* floating type rep*/, details::mib\u003e;\nusing f_gibibytes = memory_size\u003c/* floating type rep*/, details::gib\u003e;\nusing f_tebibytes = memory_size\u003c/* floating type rep*/, details::tib\u003e;\nusing f_pebibytes = memory_size\u003c/* floating type rep*/, details::pib\u003e;\nusing f_exbibytes = memory_size\u003c/* floating type rep*/, details::eib\u003e;\n```\n\n## Literals operators\n\nLiteral operators are available for all types from both Base 10 and Base 2 systems, enabling the creation\nof memory size constants at compile-time. These operators are part of the mu::literals namespace. Users can\nemploy these operators to specify memory sizes in their preferred unit:\n\n```c++\n// ...\nusing namespace mu::literals;\n// ...\n\nauto size_kb = 256_kB; // 256 kilobytes\nauto size_mb = 128_MB; // 128 megabytes\nauto size_gb = 2_GB;   // 2 gigabytes\nauto size_tb = 1_TB;   // 1 terabyte\nauto size_pb = 3_PB;   // 3 petabytes\nauto size_eb = 5_EB;   // 5 exabytes\n\nauto size_kib = 256_kiB; // 256 kibibytes\nauto size_mib = 128_MiB; // 128 mebibytes\nauto size_gib = 2_GiB;   // 2 gibibytes\nauto size_tib = 1_TiB;   // 1 tebibyte\nauto size_pib = 3_PiB;   // 3 petbibytes\nauto size_eib = 5_EiB;   // 5 exbibytes\n\nif (size_gb \u003c 5_GB) \n    // ...\n\nif (2_TiB \u003e= size_kib)\n    // ...\n\n//...\n```\n\n## Dependencies\n\nThe implementation is self-contained and needs only standard language support through a compiler supporting at \nleast **[C++11](https://en.cppreference.com/w/cpp/compiler_support)**.\n\nTo run the test suite, [Google Test (GTest)](https://github.com/google/googletest) is required.\n\n## Installation\n\nJust copy the [header file](include/memory_units.hpp) or its content into your project.\n\n## Feedback\n\nDon't hesitate if you have any suggestions for improving this project, or if you find any error. I will be glad to\nhear from you. Contributions are welcomed :)\n\n## License\n\nDistributed under the MIT Software License (X11 license).\nSee the accompanying [LICENSE](LICENSE) file.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnandite%2Fmemory-units","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnandite%2Fmemory-units","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnandite%2Fmemory-units/lists"}