1283624Sjhb/*
2283624Sjhb * Copyright (c) 2013 Hudson River Trading LLC
3283624Sjhb * Written by: John H. Baldwin <jhb@FreeBSD.org>
4283624Sjhb * All rights reserved.
5283624Sjhb *
6283624Sjhb * Redistribution and use in source and binary forms, with or without
7283624Sjhb * modification, are permitted provided that the following conditions
8283624Sjhb * are met:
9283624Sjhb * 1. Redistributions of source code must retain the above copyright
10283624Sjhb *    notice, this list of conditions and the following disclaimer.
11283624Sjhb * 2. Redistributions in binary form must reproduce the above copyright
12283624Sjhb *    notice, this list of conditions and the following disclaimer in the
13283624Sjhb *    documentation and/or other materials provided with the distribution.
14283624Sjhb *
15283624Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16283624Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17283624Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18283624Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19283624Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20283624Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21283624Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22283624Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23283624Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24283624Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25283624Sjhb * SUCH DAMAGE.
26283624Sjhb */
27283624Sjhb
28283624Sjhb#include <sys/cdefs.h>
29283624Sjhb__FBSDID("$FreeBSD$");
30283624Sjhb
31283624Sjhb#include <sys/types.h>
32283624Sjhb#include <sys/sysctl.h>
33283624Sjhb#include <sys/user.h>
34283624Sjhb#include <stdlib.h>
35283624Sjhb#include <string.h>
36283624Sjhb
37283624Sjhb#include "libutil.h"
38283624Sjhb
39283624Sjhbstruct kinfo_vmobject *
40283624Sjhbkinfo_getvmobject(int *cntp)
41283624Sjhb{
42283624Sjhb	char *buf, *bp, *ep;
43283624Sjhb	struct kinfo_vmobject *kvo, *list, *kp;
44283624Sjhb	size_t len;
45283624Sjhb	int cnt, i;
46283624Sjhb
47283624Sjhb	buf = NULL;
48283624Sjhb	for (i = 0; i < 3; i++) {
49283624Sjhb		if (sysctlbyname("vm.objects", NULL, &len, NULL, 0) < 0)
50283624Sjhb			return (NULL);
51283624Sjhb		buf = reallocf(buf, len);
52283624Sjhb		if (buf == NULL)
53283624Sjhb			return (NULL);
54283624Sjhb		if (sysctlbyname("vm.objects", buf, &len, NULL, 0) == 0)
55283624Sjhb			goto unpack;
56283624Sjhb		if (errno != ENOMEM) {
57283624Sjhb			free(buf);
58283624Sjhb			return (NULL);
59283624Sjhb		}
60283624Sjhb	}
61283624Sjhb	free(buf);
62283624Sjhb	return (NULL);
63283624Sjhb
64283624Sjhbunpack:
65283624Sjhb	/* Count items */
66283624Sjhb	cnt = 0;
67283624Sjhb	bp = buf;
68283624Sjhb	ep = buf + len;
69283624Sjhb	while (bp < ep) {
70283624Sjhb		kvo = (struct kinfo_vmobject *)(uintptr_t)bp;
71283624Sjhb		bp += kvo->kvo_structsize;
72283624Sjhb		cnt++;
73283624Sjhb	}
74283624Sjhb
75283624Sjhb	list = calloc(cnt, sizeof(*list));
76283624Sjhb	if (list == NULL) {
77283624Sjhb		free(buf);
78283624Sjhb		return (NULL);
79283624Sjhb	}
80283624Sjhb
81283624Sjhb	/* Unpack */
82283624Sjhb	bp = buf;
83283624Sjhb	kp = list;
84283624Sjhb	while (bp < ep) {
85283624Sjhb		kvo = (struct kinfo_vmobject *)(uintptr_t)bp;
86283624Sjhb		memcpy(kp, kvo, kvo->kvo_structsize);
87283624Sjhb		bp += kvo->kvo_structsize;
88283624Sjhb		kp->kvo_structsize = sizeof(*kp);
89283624Sjhb		kp++;
90283624Sjhb	}
91283624Sjhb	free(buf);
92283624Sjhb	*cntp = cnt;
93283624Sjhb	return (list);
94283624Sjhb}
95