4 * Copyright © 2001-2002 Bernhard Baehr
6 * MemInfo.m - Memory Usage History Container Class
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #import "mach/mach_host.h"
28 @implementation MemInfo
31 static void getVMStat (vm_statistics_t vmstat)
33 unsigned count = HOST_VM_INFO_COUNT;
35 if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) vmstat, &count) != KERN_SUCCESS)
36 NSLog (@"Failed to get VM statistics.");
40 - (MemInfo *) initWithCapacity:(unsigned)numItems
44 vmdata = calloc(numItems, sizeof(VMData));
46 NSLog (@"Failed to allocate buffer for MemInfo");
51 getVMStat (&lastvmstat);
58 vm_statistics_data_t vmstat;
62 total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
63 vmdata[inptr].wired = vmstat.wire_count / total;
64 vmdata[inptr].active = vmstat.active_count / total;
65 vmdata[inptr].inactive = vmstat.inactive_count / total;
66 vmdata[inptr].free = vmstat.free_count / total;
67 vmdata[inptr].pageins = vmstat.pageins - lastvmstat.pageins;
68 vmdata[inptr].pageouts = vmstat.pageouts - lastvmstat.pageouts;
81 - (BOOL)getNext:(VMDataPtr)ptr
85 *ptr = vmdata[outptr++];
94 - (void)getCurrent:(VMDataPtr)ptr
96 *ptr = vmdata[inptr ? inptr - 1 : size - 1];
100 - (void)getLast:(VMDataPtr)ptr
102 *ptr = vmdata[inptr > 1 ? inptr - 2 : size + inptr - 2];