3 * Christopher Bowns, 2008
5 * Formerly: Memory Monitor, by Bernhard Baehr
7 * Copyright © 2001-2002 Bernhard Baehr
9 * MemInfo.m - Memory Usage History Container Class
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #import "mach/mach_host.h"
31 @implementation MemInfo
34 static void getVMStat (vm_statistics_t vmstat)
36 unsigned count = HOST_VM_INFO_COUNT;
38 if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) vmstat, &count) != KERN_SUCCESS)
39 NSLog (@"Failed to get VM statistics.");
43 - (MemInfo *) initWithCapacity:(unsigned)numItems
47 vmdata = calloc(numItems, sizeof(VMData));
49 NSLog (@"Failed to allocate buffer for MemInfo");
54 getVMStat (&lastvmstat);
61 vm_statistics_data_t vmstat;
65 total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
66 vmdata[inptr].wired = vmstat.wire_count / total;
67 vmdata[inptr].active = vmstat.active_count / total;
68 vmdata[inptr].inactive = vmstat.inactive_count / total;
69 vmdata[inptr].free = vmstat.free_count / total;
70 vmdata[inptr].pageins = vmstat.pageins - lastvmstat.pageins;
71 vmdata[inptr].pageouts = vmstat.pageouts - lastvmstat.pageouts;
84 - (BOOL)getNext:(VMDataPtr)ptr
88 *ptr = vmdata[outptr++];
97 - (void)getCurrent:(VMDataPtr)ptr
99 *ptr = vmdata[inptr ? inptr - 1 : size - 1];
103 - (void)getLast:(VMDataPtr)ptr
105 *ptr = vmdata[inptr > 1 ? inptr - 2 : size + inptr - 2];