11eaf0ac3Slogwang /*
21eaf0ac3Slogwang * Copyright (c) 2013 Hudson River Trading LLC
31eaf0ac3Slogwang * Written by: John H. Baldwin <[email protected]>
41eaf0ac3Slogwang * All rights reserved.
51eaf0ac3Slogwang *
61eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
71eaf0ac3Slogwang * modification, are permitted provided that the following conditions
81eaf0ac3Slogwang * are met:
91eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
101eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer.
111eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
121eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
131eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
141eaf0ac3Slogwang *
151eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251eaf0ac3Slogwang * SUCH DAMAGE.
261eaf0ac3Slogwang */
271eaf0ac3Slogwang
281eaf0ac3Slogwang #include <sys/cdefs.h>
291eaf0ac3Slogwang __FBSDID("$FreeBSD$");
301eaf0ac3Slogwang
311eaf0ac3Slogwang #include <sys/types.h>
321eaf0ac3Slogwang #include <sys/sysctl.h>
331eaf0ac3Slogwang #include <sys/user.h>
341eaf0ac3Slogwang #include <stdlib.h>
351eaf0ac3Slogwang #include <string.h>
361eaf0ac3Slogwang
371eaf0ac3Slogwang #include "libutil.h"
381eaf0ac3Slogwang
391eaf0ac3Slogwang struct kinfo_vmobject *
kinfo_getvmobject(int * cntp)401eaf0ac3Slogwang kinfo_getvmobject(int *cntp)
411eaf0ac3Slogwang {
421eaf0ac3Slogwang char *buf, *bp, *ep;
431eaf0ac3Slogwang struct kinfo_vmobject *kvo, *list, *kp;
441eaf0ac3Slogwang size_t len;
451eaf0ac3Slogwang int cnt, i;
461eaf0ac3Slogwang
471eaf0ac3Slogwang buf = NULL;
481eaf0ac3Slogwang for (i = 0; i < 3; i++) {
49*22ce4affSfengbojiang if (sysctlbyname("vm.objects", NULL, &len, NULL, 0) < 0) {
50*22ce4affSfengbojiang free(buf);
511eaf0ac3Slogwang return (NULL);
52*22ce4affSfengbojiang }
531eaf0ac3Slogwang buf = reallocf(buf, len);
541eaf0ac3Slogwang if (buf == NULL)
551eaf0ac3Slogwang return (NULL);
561eaf0ac3Slogwang if (sysctlbyname("vm.objects", buf, &len, NULL, 0) == 0)
571eaf0ac3Slogwang goto unpack;
581eaf0ac3Slogwang if (errno != ENOMEM) {
591eaf0ac3Slogwang free(buf);
601eaf0ac3Slogwang return (NULL);
611eaf0ac3Slogwang }
621eaf0ac3Slogwang }
631eaf0ac3Slogwang free(buf);
641eaf0ac3Slogwang return (NULL);
651eaf0ac3Slogwang
661eaf0ac3Slogwang unpack:
671eaf0ac3Slogwang /* Count items */
681eaf0ac3Slogwang cnt = 0;
691eaf0ac3Slogwang bp = buf;
701eaf0ac3Slogwang ep = buf + len;
711eaf0ac3Slogwang while (bp < ep) {
721eaf0ac3Slogwang kvo = (struct kinfo_vmobject *)(uintptr_t)bp;
731eaf0ac3Slogwang bp += kvo->kvo_structsize;
741eaf0ac3Slogwang cnt++;
751eaf0ac3Slogwang }
761eaf0ac3Slogwang
771eaf0ac3Slogwang list = calloc(cnt, sizeof(*list));
781eaf0ac3Slogwang if (list == NULL) {
791eaf0ac3Slogwang free(buf);
801eaf0ac3Slogwang return (NULL);
811eaf0ac3Slogwang }
821eaf0ac3Slogwang
831eaf0ac3Slogwang /* Unpack */
841eaf0ac3Slogwang bp = buf;
851eaf0ac3Slogwang kp = list;
861eaf0ac3Slogwang while (bp < ep) {
871eaf0ac3Slogwang kvo = (struct kinfo_vmobject *)(uintptr_t)bp;
881eaf0ac3Slogwang memcpy(kp, kvo, kvo->kvo_structsize);
891eaf0ac3Slogwang bp += kvo->kvo_structsize;
901eaf0ac3Slogwang kp->kvo_structsize = sizeof(*kp);
911eaf0ac3Slogwang kp++;
921eaf0ac3Slogwang }
931eaf0ac3Slogwang free(buf);
941eaf0ac3Slogwang *cntp = cnt;
951eaf0ac3Slogwang return (list);
961eaf0ac3Slogwang }
97