Converts a byte count into a human-readable value and unit
| 506 | |
| 507 | /// Converts a byte count into a human-readable value and unit |
| 508 | void bytes_to_readable(size_t bytes, double* value_out, const char** unit_out) { |
| 509 | double value = (double)bytes; |
| 510 | const char* unit = "Bytes"; |
| 511 | |
| 512 | if (bytes >= (1ULL << 30)) { // 1 GB |
| 513 | value = value / (1ULL << 30); |
| 514 | unit = "GB"; |
| 515 | } else if (bytes >= (1ULL << 20)) { // 1 MB |
| 516 | value = value / (1ULL << 20); |
| 517 | unit = "MB"; |
| 518 | } else if (bytes >= (1ULL << 10)) { // 1 KB |
| 519 | value = value / (1ULL << 10); |
| 520 | unit = "KB"; |
| 521 | } |
| 522 | |
| 523 | if (value_out) *value_out = value; |
| 524 | if (unit_out) *unit_out = unit; |
| 525 | } |
| 526 | |
| 527 | /// Returns the amount of currently available physical RAM in bytes |
| 528 | /// Note that this value is only a snapshot. |
no outgoing calls
no test coverage detected