#include <sys/cdefs.h>
__RCSID("$NetBSD: intrctl_io.c,v 1.5 2019/09/23 09:17:19 mrg Exp $");
#include <sys/sysctl.h>
#include <sys/intrio.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "intrctl_io.h"
void *
intrctl_io_alloc(int retry)
{
size_t buf_size;
int i, error;
void *buf = NULL;
error = sysctlbyname("kern.intr.list", NULL, &buf_size, NULL, 0);
if (error < 0) {
goto error;
}
buf = malloc(buf_size);
if (buf == NULL) {
goto error;
}
for (i = 0; i < retry; i++) {
error = sysctlbyname("kern.intr.list", buf, &buf_size, NULL, 0);
if (error >= 0)
return buf;
else if (errno == ENOMEM) {
void *temp;
temp = realloc(buf, buf_size);
if (temp == NULL) {
goto error;
}
buf = temp;
} else {
goto error;
}
}
error:
free(buf);
return NULL;
}
void
intrctl_io_free(void *handle)
{
free(handle);
}
int
intrctl_io_ncpus(void *handle)
{
struct intrio_list *list = handle;
return list->il_ncpus;
}
int
intrctl_io_nintrs(void *handle)
{
struct intrio_list *list = handle;
return list->il_nintrs;
}
struct intrio_list_line *
intrctl_io_firstline(void *handle)
{
struct intrio_list *list = handle;
struct intrio_list_line *next;
char *buf_end;
buf_end = (char *)list + list->il_bufsize;
next = (struct intrio_list_line *)((char *)list + list->il_lineoffset);
if ((char *)next >= buf_end)
return NULL;
return next;
}
struct intrio_list_line *
intrctl_io_nextline(void *handle, struct intrio_list_line *cur)
{
struct intrio_list *list;
struct intrio_list_line *next;
size_t line_size;
char *buf_end;
list = handle;
buf_end = (char *)list + list->il_bufsize;
line_size = list->il_linesize;
next = (struct intrio_list_line *)((char *)cur + line_size);
if ((char *)next >= buf_end)
return NULL;
return next;
}