Fix a uni-processor bug with the last commit... we weren't rescheduling on
[dragonfly.git] / sys / sys / malloc.h
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)malloc.h 8.5 (Berkeley) 5/3/95
34 * $FreeBSD: src/sys/sys/malloc.h,v 1.48.2.2 2002/03/16 02:19:16 archie Exp $
074dcfe8 35 * $DragonFly: src/sys/sys/malloc.h,v 1.8 2003/09/26 19:23:34 dillon Exp $
984263bc
MD
36 */
37
38#ifndef _SYS_MALLOC_H_
39#define _SYS_MALLOC_H_
40
a108bf71
MD
41#ifdef _KERNEL
42
03aa8d99
MD
43#ifndef _MACHINE_VMPARAM_H_
44#include <machine/vmparam.h> /* for VM_MIN_KERNEL_ADDRESS */
45#endif
46
984263bc
MD
47#define splmem splhigh
48
a108bf71
MD
49#endif
50
51#if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
52
984263bc
MD
53/*
54 * flags to malloc.
55 */
97482992
HP
56#define M_NOWAIT 0x0001 /* do not block */
57#define M_WAITOK 0x0002 /* wait for resources */
58#define M_ZERO 0x0100 /* bzero() the allocation */
59#define M_USE_RESERVE 0x0200 /* can alloc out of reserve memory */
a108bf71 60#define M_NULLOK 0x0400 /* ok to return NULL in M_WAITOK case */
984263bc
MD
61
62#define M_MAGIC 877983977 /* time when first defined :-) */
63
64struct malloc_type {
65 struct malloc_type *ks_next; /* next in list */
66 long ks_memuse; /* total memory held in bytes */
67 long ks_limit; /* most that are allowed to exist */
68 long ks_size; /* sizes of this thing that are allocated */
69 long ks_inuse; /* # of packets of this type currently in use */
70 int64_t ks_calls; /* total packets of this type ever allocated */
71 long ks_maxused; /* maximum number ever used */
72 u_long ks_magic; /* if it's not magic, don't touch it */
73 const char *ks_shortdesc; /* short description */
74 u_short ks_limblocks; /* number of times blocked for hitting limit */
75 u_short ks_mapblocks; /* number of times blocked for kernel map */
76};
77
a108bf71
MD
78#endif
79
984263bc
MD
80#ifdef _KERNEL
81#define MALLOC_DEFINE(type, shortdesc, longdesc) \
82 struct malloc_type type[1] = { \
83 { NULL, 0, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, 0, 0 } \
84 }; \
85 SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_ANY, malloc_init, type); \
86 SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, malloc_uninit, type)
87
88#define MALLOC_DECLARE(type) \
89 extern struct malloc_type type[1]
90
91MALLOC_DECLARE(M_CACHE);
92MALLOC_DECLARE(M_DEVBUF);
93MALLOC_DECLARE(M_TEMP);
94
95MALLOC_DECLARE(M_IP6OPT); /* for INET6 */
96MALLOC_DECLARE(M_IP6NDP); /* for INET6 */
97#endif /* _KERNEL */
98
a108bf71
MD
99#if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
100
984263bc
MD
101/*
102 * Array of descriptors that describe the contents of each page
103 */
104struct kmemusage {
105 short ku_indx; /* bucket index */
106 union {
107 u_short freecnt;/* for small allocations, free pieces in page */
108 u_short pagecnt;/* for large allocations, pages alloced */
109 } ku_un;
110};
111#define ku_freecnt ku_un.freecnt
112#define ku_pagecnt ku_un.pagecnt
113
114/*
115 * Set of buckets for each size of memory block that is retained
116 */
117struct kmembuckets {
118 caddr_t kb_next; /* list of free blocks */
119 caddr_t kb_last; /* last free block */
120 int64_t kb_calls; /* total calls to allocate this size */
121 long kb_total; /* total number of blocks allocated */
122 long kb_elmpercl; /* # of elements in this sized allocation */
123 long kb_totalfree; /* # of free elements in this bucket */
124 long kb_highwat; /* high water mark */
125 long kb_couldfree; /* over high water mark and could free */
126};
127
984263bc
MD
128#define MINALLOCSIZE (1 << MINBUCKET)
129#define BUCKETINDX(size) \
130 ((size) <= (MINALLOCSIZE * 128) \
131 ? (size) <= (MINALLOCSIZE * 8) \
132 ? (size) <= (MINALLOCSIZE * 2) \
133 ? (size) <= (MINALLOCSIZE * 1) \
134 ? (MINBUCKET + 0) \
135 : (MINBUCKET + 1) \
136 : (size) <= (MINALLOCSIZE * 4) \
137 ? (MINBUCKET + 2) \
138 : (MINBUCKET + 3) \
139 : (size) <= (MINALLOCSIZE* 32) \
140 ? (size) <= (MINALLOCSIZE * 16) \
141 ? (MINBUCKET + 4) \
142 : (MINBUCKET + 5) \
143 : (size) <= (MINALLOCSIZE * 64) \
144 ? (MINBUCKET + 6) \
145 : (MINBUCKET + 7) \
146 : (size) <= (MINALLOCSIZE * 2048) \
147 ? (size) <= (MINALLOCSIZE * 512) \
148 ? (size) <= (MINALLOCSIZE * 256) \
149 ? (MINBUCKET + 8) \
150 : (MINBUCKET + 9) \
151 : (size) <= (MINALLOCSIZE * 1024) \
152 ? (MINBUCKET + 10) \
153 : (MINBUCKET + 11) \
154 : (size) <= (MINALLOCSIZE * 8192) \
155 ? (size) <= (MINALLOCSIZE * 4096) \
156 ? (MINBUCKET + 12) \
157 : (MINBUCKET + 13) \
158 : (size) <= (MINALLOCSIZE * 16384) \
159 ? (MINBUCKET + 14) \
160 : (MINBUCKET + 15))
161
a108bf71
MD
162#endif
163
164#ifdef _KERNEL
165
984263bc
MD
166/*
167 * Turn virtual addresses into kmem map indices
168 */
074dcfe8 169#if defined(USE_KMEM_MAP)
984263bc
MD
170#define kmemxtob(alloc) (kmembase + (alloc) * PAGE_SIZE)
171#define btokmemx(addr) (((caddr_t)(addr) - kmembase) / PAGE_SIZE)
172#define btokup(addr) (&kmemusage[((caddr_t)(addr) - kmembase) >> PAGE_SHIFT])
074dcfe8
MD
173#else
174#define btokup(addr) (&kmemusage[((caddr_t)(addr) - (caddr_t)VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT])
03aa8d99 175#endif
984263bc
MD
176
177/*
178 * Deprecated macro versions of not-quite-malloc() and free().
179 */
180#define MALLOC(space, cast, size, type, flags) \
181 (space) = (cast)malloc((u_long)(size), (type), (flags))
182#define FREE(addr, type) free((addr), (type))
183
184/*
185 * XXX this should be declared in <sys/uio.h>, but that tends to fail
186 * because <sys/uio.h> is included in a header before the source file
187 * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
188 */
189MALLOC_DECLARE(M_IOV);
190
191/* XXX struct malloc_type is unused for contig*(). */
b153f746
RG
192void contigfree (void *addr, unsigned long size,
193 struct malloc_type *type);
194void *contigmalloc (unsigned long size, struct malloc_type *type,
984263bc 195 int flags, unsigned long low, unsigned long high,
b153f746
RG
196 unsigned long alignment, unsigned long boundary);
197void free (void *addr, struct malloc_type *type);
198void *malloc (unsigned long size, struct malloc_type *type, int flags);
199void malloc_init (void *);
200void malloc_uninit (void *);
201void *realloc (void *addr, unsigned long size,
202 struct malloc_type *type, int flags);
203void *reallocf (void *addr, unsigned long size,
204 struct malloc_type *type, int flags);
a108bf71 205
984263bc
MD
206#endif /* _KERNEL */
207
208#endif /* !_SYS_MALLOC_H_ */