#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/tty.h>
#include <sys/quota.h>
#include <ufs/ufs/inode.h>
#include <hfs/hfs_cnode.h>
#include <isofs/cd9660/cd9660_node.h>
#include <miscfs/volfs/volfs.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#include <nfs/nfsnode.h>
#include <nfs/nfsmount.h>
#include <nfs/nqnfs.h>
#include <vfs/vfs_journal.h>
#include <mach/mach_types.h>
#include <kern/zalloc.h>
#include <kern/kalloc.h>
struct kmemstats kmemstats[M_LAST];
char *memname[] = INITKMEMNAMES;
struct kmzones {
size_t kz_elemsize;
void *kz_zalloczone;
#define KMZ_CREATEZONE ((void *)-2)
#define KMZ_LOOKUPZONE ((void *)-1)
#define KMZ_MALLOC ((void *)0)
#define KMZ_SHAREZONE ((void *)1)
} kmzones[M_LAST] = {
#define SOS(sname) sizeof (struct sname)
#define SOX(sname) -1
-1, 0,
MSIZE, KMZ_CREATEZONE,
0, KMZ_MALLOC,
SOS(socket), KMZ_CREATEZONE,
SOS(inpcb), KMZ_LOOKUPZONE,
M_MBUF, KMZ_SHAREZONE,
M_MBUF, KMZ_SHAREZONE,
M_MBUF, KMZ_SHAREZONE,
SOS(rusage), KMZ_CREATEZONE,
0, KMZ_MALLOC,
M_MBUF, KMZ_SHAREZONE,
0, KMZ_MALLOC,
MAXPATHLEN, KMZ_CREATEZONE,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
SOS(ucred), KMZ_CREATEZONE,
SOS(pgrp), KMZ_CREATEZONE,
SOS(session), KMZ_CREATEZONE,
SOS(iovec), KMZ_LOOKUPZONE,
SOS(mount), KMZ_CREATEZONE,
0, KMZ_MALLOC,
SOS(nfsreq), KMZ_CREATEZONE,
SOS(nfsmount), KMZ_CREATEZONE,
SOS(nfsnode), KMZ_CREATEZONE,
SOS(vnode), KMZ_CREATEZONE,
SOS(namecache), KMZ_CREATEZONE,
SOX(dquot), KMZ_LOOKUPZONE,
SOX(ufsmount), KMZ_LOOKUPZONE,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
SOS(file), KMZ_CREATEZONE,
SOS(filedesc), KMZ_CREATEZONE,
SOX(lockf), KMZ_CREATEZONE,
SOS(proc), KMZ_CREATEZONE,
SOS(pcred), KMZ_CREATEZONE,
0, KMZ_MALLOC,
M_FFSNODE, KMZ_SHAREZONE,
SOS(inode), KMZ_CREATEZONE,
M_FFSNODE, KMZ_SHAREZONE,
SOS(nqlease), KMZ_CREATEZONE,
SOS(nqm), KMZ_CREATEZONE,
0, KMZ_MALLOC,
SOX(nfssvc_sock),
KMZ_CREATEZONE,
SOS(nfsuid), KMZ_CREATEZONE,
SOX(nfsrvcache),
KMZ_CREATEZONE,
SOX(ip_moptions),
KMZ_LOOKUPZONE,
SOX(in_multi), KMZ_LOOKUPZONE,
SOX(ether_multi),
KMZ_LOOKUPZONE,
SOX(mrt), KMZ_CREATEZONE,
SOX(iso_mnt), KMZ_LOOKUPZONE,
SOS(iso_node), KMZ_CREATEZONE,
SOS(nfsrv_descript),
KMZ_CREATEZONE,
SOS(nfsdmap), KMZ_CREATEZONE,
SOS(fhandle), KMZ_LOOKUPZONE,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
SOS(tty), KMZ_CREATEZONE,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
SOX(buf), KMZ_CREATEZONE,
(NDFILE * OFILESIZE),
KMZ_CREATEZONE,
MCLBYTES, KMZ_CREATEZONE,
SOX(hfsmount), KMZ_LOOKUPZONE,
SOS(cnode), KMZ_CREATEZONE,
SOS(filefork), KMZ_CREATEZONE,
SOX(volfs_mntdata), KMZ_LOOKUPZONE,
SOS(volfs_vndata), KMZ_CREATEZONE,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
0, KMZ_MALLOC,
SOS(journal), KMZ_CREATEZONE,
SOS(transaction), KMZ_CREATEZONE,
#undef SOS
#undef SOX
};
void
kmeminit(void)
{
struct kmzones *kmz;
if ((sizeof(kmzones)/sizeof(kmzones[0])) != (sizeof(memname)/sizeof(memname[0]))) {
panic("kmeminit: kmzones has %d elements but memname has %d\n",
(sizeof(kmzones)/sizeof(kmzones[0])), (sizeof(memname)/sizeof(memname[0])));
}
kmz = kmzones;
while (kmz < &kmzones[M_LAST]) {
if (kmz->kz_elemsize == -1)
;
else
if (kmz->kz_zalloczone == KMZ_CREATEZONE) {
kmz->kz_zalloczone = zinit(kmz->kz_elemsize,
1024 * 1024, PAGE_SIZE,
memname[kmz - kmzones]);
}
else if (kmz->kz_zalloczone == KMZ_LOOKUPZONE)
kmz->kz_zalloczone = kalloc_zone(kmz->kz_elemsize);
kmz++;
}
kmz = kmzones;
while (kmz < &kmzones[M_LAST]) {
if (kmz->kz_elemsize == -1)
;
else
if (kmz->kz_zalloczone == KMZ_SHAREZONE) {
kmz->kz_zalloczone =
kmzones[kmz->kz_elemsize].kz_zalloczone;
kmz->kz_elemsize =
kmzones[kmz->kz_elemsize].kz_elemsize;
}
kmz++;
}
}
#define MDECL(reqlen) \
union { \
struct _mhead hdr; \
char _m[(reqlen) + sizeof (struct _mhead)]; \
}
struct _mhead {
size_t mlen;
char dat[0];
};
#define ZEROSIZETOKEN 0xFADEDFAD
void *_MALLOC(
size_t size,
int type,
int flags)
{
MDECL(size) *mem;
size_t memsize = sizeof (*mem);
if (type >= M_LAST)
panic("_malloc TYPE");
if (size == 0)
return (ZEROSIZETOKEN);
if (flags & M_NOWAIT) {
mem = (void *)kalloc_noblock(memsize);
} else {
mem = (void *)kalloc(memsize);
}
if (!mem)
return (0);
mem->hdr.mlen = memsize;
return (mem->hdr.dat);
}
void _FREE(
void *addr,
int type)
{
struct _mhead *hdr;
if (type >= M_LAST)
panic("_free TYPE");
if (addr == (void *)ZEROSIZETOKEN)
return;
if (!addr)
return;
hdr = addr; hdr--;
kfree((vm_offset_t)hdr, hdr->mlen);
}
void *_MALLOC_ZONE(
size_t size,
int type,
int flags)
{
struct kmzones *kmz;
void *elem;
if (type >= M_LAST)
panic("_malloc_zone TYPE");
kmz = &kmzones[type];
if (kmz->kz_zalloczone == KMZ_MALLOC)
panic("_malloc_zone ZONE");
if (kmz->kz_elemsize == -1)
panic("_malloc_zone XXX");
if (size == kmz->kz_elemsize)
if (flags & M_NOWAIT) {
elem = (void *)zalloc_noblock(kmz->kz_zalloczone);
} else {
elem = (void *)zalloc(kmz->kz_zalloczone);
}
else
if (flags & M_NOWAIT) {
elem = (void *)kalloc_noblock(size);
} else {
elem = (void *)kalloc(size);
}
return (elem);
}
void _FREE_ZONE(
void *elem,
size_t size,
int type)
{
struct kmzones *kmz;
if (type >= M_LAST)
panic("FREE_SIZE");
kmz = &kmzones[type];
if (kmz->kz_zalloczone == KMZ_MALLOC)
panic("free_zone ZONE");
if (kmz->kz_elemsize == -1)
panic("FREE_SIZE XXX");
if (size == kmz->kz_elemsize)
zfree(kmz->kz_zalloczone, (vm_offset_t)elem);
else
kfree((vm_offset_t)elem, size);
}