2 * Copyright (c) 1997, 1998 John S. Dyson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Absolutely no warranty of function or purpose is made by the author
14 * $Id: vm_zone.c,v 1.20 1998/04/15 17:47:40 bde Exp $
17 #if !defined(_SYS_ZONE_H)
21 #define ZONE_INTERRUPT 1 /* Use this if you need to allocate at int time */
22 #define ZONE_BOOT 16 /* This is an internal flag used by zbootinit */
24 #include <machine/lock.h>
26 typedef struct vm_zone {
27 struct simplelock zlock; /* lock for data structure */
28 void *zitems; /* linked list of items */
29 int zfreecnt; /* free entries */
30 int zfreemin; /* minimum number of free entries */
31 int znalloc; /* number of allocations */
32 vm_offset_t zkva; /* Base kva of zone */
33 int zpagecount; /* Total # of allocated pages */
34 int zpagemax; /* Max address space */
35 int zmax; /* Max number of entries allocated */
36 int ztotal; /* Total entries allocated now */
37 int zsize; /* size of each entry */
38 int zalloc; /* hint for # of pages to alloc */
39 int zflags; /* flags for zone */
40 int zallocflag; /* flag for allocation */
41 struct vm_object *zobj; /* object to hold zone */
42 char *zname; /* name for diags */
43 struct vm_zone *znext; /* list of zones for sysctl */
47 void zerror __P((int)) __dead2;
48 vm_zone_t zinit __P((char *name, int size, int nentries, int flags,
50 int zinitna __P((vm_zone_t z, struct vm_object *obj, char *name,
51 int size, int nentries, int flags, int zalloc));
52 static void * zalloc __P((vm_zone_t z));
53 static void zfree __P((vm_zone_t z, void *item));
54 void * zalloci __P((vm_zone_t z));
55 void zfreei __P((vm_zone_t z, void *item));
56 void zbootinit __P((vm_zone_t z, char *name, int size, void *item,
58 void * _zget __P((vm_zone_t z));
60 #define ZONE_ERROR_INVALID 0
61 #define ZONE_ERROR_NOTFREE 1
62 #define ZONE_ERROR_ALREADYFREE 2
64 #define ZONE_ROUNDING 32
66 #define ZENTRY_FREE 0x12342378
68 * void *zalloc(vm_zone_t zone) --
69 * Returns an item from a specified zone.
71 * void zfree(vm_zone_t zone, void *item) --
72 * Frees an item back to a specified zone.
74 static __inline__ void *
79 #if defined(DIAGNOSTIC)
81 zerror(ZONE_ERROR_INVALID);
84 if (z->zfreecnt <= z->zfreemin)
88 z->zitems = ((void **) item)[0];
89 #if defined(DIAGNOSTIC)
90 if (((void **) item)[1] != (void *) ZENTRY_FREE)
91 zerror(ZONE_ERROR_NOTFREE);
92 ((void **) item)[1] = 0;
100 static __inline__ void
101 _zfree(vm_zone_t z, void *item)
103 ((void **) item)[0] = z->zitems;
104 #if defined(DIAGNOSTIC)
105 if (((void **) item)[1] == (void *) ZENTRY_FREE)
106 zerror(ZONE_ERROR_ALREADYFREE);
107 ((void **) item)[1] = (void *) ZENTRY_FREE;
113 static __inline__ void *
123 static __inline__ void
124 zfree(vm_zone_t z, void *item)