総メモリ量を取得

次のhost_basic_info構造体を用いる。

#include <mach/host_info.h>
struct host_basic_info
{
       integer_t            max_cpus;
       integer_t          avail_cpus;
       vm_size_t         memory_size;
       cpu_type_t           cpu_type;
       cpu_subtype_t     cpu_subtype;
};

前エントリのhost_info関数をflavorをHOST_BASIC_INFOにして実行すると、memory_sizeに総メモリ量が格納される。

/* 総メモリ量を標準出力に表示 */
#include <mach/host_info.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
  struct host_basic_info host;
  mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
  host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&host, &count);
  printf("%d\n",host.memory_size);
  return 0;
}