Returns the size of physical memory (RAM) in bytes. * It looks ugly, but this is the cleanest way to achieve cross platform results. * Cleaned up from: * * http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system * * Note that this function: * 1) Was released under the following CC attribution license: * http://creativecommons.org/licenses/by/3.0/deed.en_US
| 648 | * 4) This note exists in order to comply with the original license. |
| 649 | */ |
| 650 | size_t zmalloc_get_memory_size(void) { |
| 651 | #if defined(__unix__) || defined(__unix) || defined(unix) || \ |
| 652 | (defined(__APPLE__) && defined(__MACH__)) |
| 653 | #if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64)) |
| 654 | int mib[2]; |
| 655 | mib[0] = CTL_HW; |
| 656 | #if defined(HW_MEMSIZE) |
| 657 | mib[1] = HW_MEMSIZE; /* OSX. --------------------- */ |
| 658 | #elif defined(HW_PHYSMEM64) |
| 659 | mib[1] = HW_PHYSMEM64; /* NetBSD, OpenBSD. --------- */ |
| 660 | #endif |
| 661 | int64_t size = 0; /* 64-bit */ |
| 662 | size_t len = sizeof(size); |
| 663 | if (sysctl( mib, 2, &size, &len, NULL, 0) == 0) |
| 664 | return (size_t)size; |
| 665 | return 0L; /* Failed? */ |
| 666 | |
| 667 | #elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) |
| 668 | /* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */ |
| 669 | return (size_t)sysconf(_SC_PHYS_PAGES) * (size_t)sysconf(_SC_PAGESIZE); |
| 670 | |
| 671 | #elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM)) |
| 672 | /* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */ |
| 673 | int mib[2]; |
| 674 | mib[0] = CTL_HW; |
| 675 | #if defined(HW_REALMEM) |
| 676 | mib[1] = HW_REALMEM; /* FreeBSD. ----------------- */ |
| 677 | #elif defined(HW_PHYSMEM) |
| 678 | mib[1] = HW_PHYSMEM; /* Others. ------------------ */ |
| 679 | #endif |
| 680 | unsigned int size = 0; /* 32-bit */ |
| 681 | size_t len = sizeof(size); |
| 682 | if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) |
| 683 | return (size_t)size; |
| 684 | return 0L; /* Failed? */ |
| 685 | #else |
| 686 | return 0L; /* Unknown method to get the data. */ |
| 687 | #endif |
| 688 | #else |
| 689 | return 0L; /* Unknown OS. */ |
| 690 | #endif |
| 691 | } |
| 692 | |
| 693 | #ifdef REDIS_TEST |
| 694 | #define UNUSED(x) ((void)(x)) |