summaryrefslogtreecommitdiff
path: root/usr/src/man/man9f/kmem_cache_create.9f
blob: d096223d90516422be3e18b5bc91d9e6b7b34f9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
'\" te
.\" Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
.\" Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH KMEM_CACHE_CREATE 9F "Feb 18, 2015"
.SH NAME
kmem_cache_create, kmem_cache_alloc, kmem_cache_free, kmem_cache_destroy,
kmem_cache_set_move \- kernel memory cache allocator operations
.SH SYNOPSIS
.LP
.nf
#include <sys/types.h>
#include <sys/kmem.h>

\fBkmem_cache_t *\fR\fBkmem_cache_create\fR(\fBchar *\fR\fIname\fR, \fBsize_t\fR \fIbufsize\fR,
     \fBsize_t\fR \fIalign\fR, \fBint\fR (*\fIconstructor\fR)(void *, void *, int),
     \fBvoid\fR (*\fIdestructor\fR)(void *, void *), \fBvoid\fR (*\fIreclaim\fR)(void *),
     \fBvoid\fR *\fIprivate\fR, \fBvoid\fR *\fIvmp\fR, \fBint\fR \fIcflags\fR);
.fi

.LP
.nf
\fBvoid\fR \fBkmem_cache_destroy\fR(\fBkmem_cache_t\fR *\fIcp\fR);
.fi

.LP
.nf
\fBvoid *\fR\fBkmem_cache_alloc\fR(\fBkmem_cache_t\fR *\fIcp\fR, \fBint\fR \fIkmflag\fR);
.fi

.LP
.nf
\fBvoid\fR \fBkmem_cache_free\fR(\fBkmem_cache_t\fR *\fIcp\fR, \fBvoid\fR *\fIobj\fR);
.fi

.LP
.nf
\fBvoid\fR \fBkmem_cache_set_move\fR(\fBkmem_cache_t\fR *\fIcp\fR, \fBkmem_cbrc_t\fR (*\fImove\fR)(\fBvoid\fR *,
     \fBvoid\fR *, \fBsize_t\fR *, \fBvoid\fR *));
.fi

.LP
.nf
 [Synopsis for callback functions:]
.fi

.LP
.nf
\fBint\fR (*\fIconstructor\fR)(\fBvoid\fR *\fIbuf\fR, \fBvoid\fR *\fIuser_arg\fR, \fBint\fR \fIkmflags\fR);
.fi

.LP
.nf
\fBvoid\fR (*\fIdestructor\fR)(\fBvoid\fR *\fIbuf\fR, \fBvoid\fR *\fIuser_arg\fR);
.fi

.LP
.nf
\fBkmem_cbrc_t\fR (*\fImove\fR)(\fBvoid\fR *\fIold\fR, \fBvoid\fR *\fInew\fR, \fBsize_t\fR \fIbufsize\fR,
     \fBvoid\fR *\fIuser_arg\fR);
.fi

.SH INTERFACE LEVEL
.LP
Solaris DDI specific (Solaris DDI)
.SH PARAMETERS
.LP
The parameters for the \fBkmem_cache_*\fR functions are as follows:
.sp
.ne 2
.na
\fB\fIname\fR\fR
.ad
.RS 15n
Descriptive name of a \fBkstat\fR(9S) structure of class \fBkmem_cache\fR.
Names longer than 31 characters are truncated.
.RE

.sp
.ne 2
.na
\fB\fIbufsize\fR\fR
.ad
.RS 15n
Size of the objects it manages.
.RE

.sp
.ne 2
.na
\fB\fIalign\fR\fR
.ad
.RS 15n
Required object alignment.
.RE

.sp
.ne 2
.na
\fB\fIconstructor\fR\fR
.ad
.RS 15n
Pointer to an object constructor function. Parameters are defined below.
.RE

.sp
.ne 2
.na
\fB\fIdestructor\fR\fR
.ad
.RS 15n
Pointer to an object destructor function. Parameters are defined below.
.RE

.sp
.ne 2
.na
\fB\fIreclaim\fR\fR
.ad
.RS 15n
Drivers should pass \fBNULL\fR.
.RE

.sp
.ne 2
.na
\fB\fIprivate\fR\fR
.ad
.RS 15n
Pass-through argument for constructor/destructor.
.RE

.sp
.ne 2
.na
\fB\fIvmp\fR\fR
.ad
.RS 15n
Drivers should pass \fBNULL\fR.
.RE

.sp
.ne 2
.na
\fB\fIcflags\fR\fR
.ad
.RS 15n
Drivers must pass 0.
.RE

.sp
.ne 2
.na
\fB\fIkmflag\fR\fR
.ad
.RS 15n
Possible flags are:
.sp
.ne 2
.na
\fB\fBKM_SLEEP\fR\fR
.ad
.RS 15n
Allow sleeping (blocking) until memory is available.
.RE

.sp
.ne 2
.na
\fB\fBKM_NOSLEEP\fR\fR
.ad
.RS 15n
Return NULL immediately if memory is not available.
.RE

.sp
.ne 2
.na
\fB\fBKM_PUSHPAGE\fR\fR
.ad
.RS 15n
Allow the allocation to use reserved memory.
.RE

.RE

.sp
.ne 2
.na
\fB\fIobj\fR\fR
.ad
.RS 15n
Pointer to the object allocated by \fBkmem_cache_alloc()\fR.
.RE

.sp
.ne 2
.na
\fB\fImove\fR\fR
.ad
.RS 15n
Pointer to an object relocation function. Parameters are defined below.
.RE

.sp
.LP
The parameters for the callback constructor function are as follows:
.sp
.ne 2
.na
\fB\fBvoid *\fIbuf\fR\fR\fR
.ad
.RS 18n
Pointer to the object to be constructed.
.RE

.sp
.ne 2
.na
\fB\fBvoid *\fIuser_arg\fR\fR\fR
.ad
.RS 18n
The \fIprivate\fR parameter from the call to \fBkmem_cache_create()\fR; it is
typically a pointer to the soft-state structure.
.RE

.sp
.ne 2
.na
\fB\fBint \fIkmflags\fR\fR\fR
.ad
.RS 18n
Propagated \fIkmflag\fR values.
.RE

.sp
.LP
The parameters for the callback destructor function are as follows:
.sp
.ne 2
.na
\fB\fBvoid *\fIbuf\fR\fR\fR
.ad
.RS 18n
Pointer to the object to be deconstructed.
.RE

.sp
.ne 2
.na
\fB\fBvoid *\fIuser_arg\fR\fR\fR
.ad
.RS 18n
The \fIprivate\fR parameter from the call to \fBkmem_cache_create()\fR; it is
typically a pointer to the soft-state structure.
.RE

.sp
.LP
The parameters for the callback \fBmove()\fR function are as follows:
.sp
.ne 2
.na
\fB\fBvoid *\fIold\fR\fR\fR
.ad
.RS 18n
Pointer to the object to be moved.
.RE

.sp
.ne 2
.na
\fB\fBvoid *\fInew\fR\fR\fR
.ad
.RS 18n
Pointer to the object that serves as the copy destination for the contents of
the old parameter.
.RE

.sp
.ne 2
.na
\fB\fBsize_t \fIbufsize\fR\fR\fR
.ad
.RS 18n
Size of the object to be moved.
.RE

.sp
.ne 2
.na
\fB\fBvoid *\fIuser_arg\fR\fR\fR
.ad
.RS 18n
The private parameter from the call to \fBkmem_cache_create()\fR; it is
typically a pointer to the \fBsoft-state\fR structure.
.RE

.SH DESCRIPTION
.LP
In many cases, the cost of initializing and destroying an object exceeds the
cost of allocating and freeing memory for it. The functions described here
address this condition.
.sp
.LP
Object caching is a technique for dealing with objects that are:
.RS +4
.TP
.ie t \(bu
.el o
frequently allocated and freed, and
.RE
.RS +4
.TP
.ie t \(bu
.el o
have setup and initialization costs.
.RE
.sp
.LP
The idea is to allow the allocator and its clients to cooperate to preserve the
invariant portion of an object's initial state, or constructed state, between
uses, so it does not have to be destroyed and re-created every time the object
is used. For example, an object containing a mutex only needs to have
\fBmutex_init()\fR applied once, the first time the object is allocated. The
object can then be freed and reallocated many times without incurring the
expense of \fBmutex_destroy()\fR and \fBmutex_init()\fR each time. An object's
embedded locks, condition variables, reference counts, lists of other objects,
and read-only data all generally qualify as constructed state. The essential
requirement is that the client must free the object (using
\fBkmem_cache_free()\fR) in its constructed state. The allocator cannot enforce
this, so programming errors will lead to hard-to-find bugs.
.sp
.LP
A driver should call \fBkmem_cache_create()\fR at the time of \fB_init\fR(9E)
or \fBattach\fR(9E), and call the corresponding \fBkmem_cache_destroy()\fR at
the time of \fB_fini\fR(9E) or \fBdetach\fR(9E).
.sp
.LP
\fBkmem_cache_create()\fR creates a cache of objects, each of size
\fIbufsize\fR bytes, aligned on an \fIalign\fR boundary. Drivers not requiring
a specific alignment can pass 0. \fIname\fR identifies the cache for statistics
and debugging. \fIconstructor\fR and \fIdestructor\fR convert plain memory into
objects and back again; \fIconstructor\fR can fail if it needs to allocate
memory but cannot. \fIprivate\fR is a parameter passed to the constructor and
destructor callbacks to support parameterized caches (for example, a pointer to
an instance of the driver's soft-state structure). To facilitate debugging,
\fBkmem_cache_create()\fR creates a \fBkstat\fR(9S) structure of class
\fBkmem_cache\fR and name \fIname\fR. It returns an opaque pointer to the
object cache.
.sp
.LP
\fBkmem_cache_alloc()\fR gets an object from the cache. The object will be in
its constructed state. \fIkmflag\fR has either \fBKM_SLEEP\fR or
\fBKM_NOSLEEP\fR set, indicating whether it is acceptable to wait for memory if
none is currently available.
.sp
.LP
A small pool of reserved memory is available to allow the system to progress
toward the goal of freeing additional memory while in a low memory situation.
The \fBKM_PUSHPAGE\fR flag enables use of this reserved memory pool on an
allocation. This flag can be used by drivers that implement \fBstrategy\fR(9E)
on memory allocations associated with a single I/O operation. The driver
guarantees that the I/O operation will complete (or timeout) and, on
completion, that the memory will be returned. The \fBKM_PUSHPAGE\fR flag should
be used only in \fBkmem_cache_alloc()\fR calls. All allocations from a given
cache should be consistent in their use of the flag. A driver that adheres to
these restrictions can guarantee progress in a low memory situation without
resorting to complex private allocation and queuing schemes. If
\fBKM_PUSHPAGE\fR is specified, \fBKM_SLEEP\fR can also be used without causing
deadlock.
.sp
.LP
\fBkmem_cache_free()\fR returns an object to the cache. The object must be in
its constructed state.
.sp
.LP
\fBkmem_cache_destroy()\fR destroys the cache and releases all associated
resources. All allocated objects must have been previously freed.
.sp
.LP
\fBkmem_cache_set_move()\fR registers a function that the allocator may call to
move objects from sparsely allocated pages of memory so that the system can
reclaim pages that are tied up by the client. Since caching objects of the same
size and type already makes severe memory fragmentation unlikely, there is
generally no need to register such a function. The idea is to make it possible
to limit worst-case fragmentation in caches that exhibit a tendency to become
highly fragmented. Only clients that allocate a mix of long- and short-lived
objects from the same cache are prone to exhibit this tendency, making them
candidates for a \fBmove()\fR callback.
.sp
.LP
The \fBmove()\fR callback supplies the client with two addresses: the allocated
object that the allocator wants to move and a buffer selected by the allocator
for the client to use as the copy destination. The new parameter is an
allocated, constructed object ready to receive the contents of the old
parameter. The \fIbufsize\fR parameter supplies the size of the object, in case
a single move function handles multiple caches whose objects differ only in
size. Finally, the private parameter passed to the constructor and destructor
is also passed to the \fBmove()\fR callback.
.sp
.LP
Only the client knows about its own data and when it is a good time to move it.
The client cooperates with the allocator to return unused memory to the system,
and the allocator accepts this help at the client's convenience. When asked to
move an object, the client can respond with any of the following:
.sp
.in +2
.nf
typedef enum kmem_cbrc {
             KMEM_CBRC_YES,
             KMEM_CBRC_NO,
             KMEM_CBRC_LATER,
             KMEM_CBRC_DONT_NEED,
             KMEM_CBRC_DONT_KNOW
} kmem_cbrc_t;
.fi
.in -2
.sp

.sp
.LP
The client must not explicitly free either of the objects passed to the
\fBmove()\fR callback, since the allocator wants to free them directly to the
slab layer (bypassing the per-CPU magazine layer). The response tells the
allocator which of the two object parameters to free:
.sp
.ne 2
.na
\fB\fBKMEM_CBRC_YES\fR\fR
.ad
.RS 23n
The client moved the object; the allocator frees the old parameter.
.RE

.sp
.ne 2
.na
\fB\fBKMEM_CBRC_NO\fR\fR
.ad
.RS 23n
The client refused to move the object; the allocator frees the new parameter
(the unused copy destination).
.RE

.sp
.ne 2
.na
\fB\fBKMEM_CBRC_LATER\fR\fR
.ad
.RS 23n
The client is using the object and cannot move it now; the allocator frees the
new parameter (the unused copy destination). The client should use
\fBKMEM_CBRC_LATER\fR instead of \fBKMEM_CBRC_NO\fR if the object is likely to
become movable soon.
.RE

.sp
.ne 2
.na
\fB\fBKMEM_CBRC_DONT_NEED\fR\fR
.ad
.RS 23n
The client no longer needs the object; the allocator frees both the old and new
parameters. This response is the client's opportunity to be a model citizen and
give back as much as it can.
.RE

.sp
.ne 2
.na
\fB\fBKMEM_CBRC_DONT_KNOW\fR\fR
.ad
.RS 23n
The client does not know about the object because:
.sp
.ne 2
.na
\fBa)\fR
.ad
.RS 6n
the client has just allocated the object and has not yet put it wherever it
expects to find known objects
.RE

.sp
.ne 2
.na
\fBb)\fR
.ad
.RS 6n
the client has removed the object from wherever it expects to find known
objects and is about to free the object
.RE

.sp
.ne 2
.na
\fBc)\fR
.ad
.RS 6n
the client has freed the object
.RE

In all of these cases above, the allocator frees the new parameter (the unused
copy destination) and searches for the old parameter in the magazine layer. If
the object is found, it is removed from the magazine layer and freed to the
slab layer so that it will no longer tie up an entire page of memory.
.RE

.sp
.LP
Any object passed to the \fBmove()\fR callback is guaranteed to have been
touched only by the allocator or by the client. Because memory patterns applied
by the allocator always set at least one of the two lowest order bits, the
bottom two bits of any pointer member (other than \fBchar *\fR or \fBshort
*\fR, which may not be 8-byte aligned on all platforms) are available to the
client for marking cached objects that the client is about to free. This way,
the client can recognize known objects in the \fBmove()\fR callback by the
unmarked (valid) pointer value.
.sp
.LP
If the client refuses to move an object with either \fBKMEM_CBRC_NO\fR or
\fBKMEM_CBRC_LATER\fR, and that object later becomes movable, the client can
notify the allocator by calling \fBkmem_cache_move_notify()\fR. Alternatively,
the client can simply wait for the allocator to call back again with the same
object address. Responding \fBKMEM_CRBC_NO\fR even once or responding
\fBKMEM_CRBC_LATER\fR too many times for the same object makes the allocator
less likely to call back again for that object.
.LP
.nf
[Synopsis for notification function:]
.fi

.LP
.nf
\fBvoid\fR \fBkmem_cache_move_notify\fR(\fBkmem_cache_t\fR *\fIcp\fR, \fBvoid\fR *\fIobj\fR);
.fi

.sp
.LP
The parameters for the \fBnotification\fR function are as follows:
.sp
.ne 2
.na
\fB\fIcp\fR\fR
.ad
.RS 7n
Pointer to the object cache.
.RE

.sp
.ne 2
.na
\fB\fIobj\fR\fR
.ad
.RS 7n
Pointer to the object that has become movable since an earlier refusal to move
it.
.RE

.SH CONTEXT
.LP
Constructors can be invoked during any call to \fBkmem_cache_alloc()\fR, and
will run in that context. Similarly, destructors can be invoked during any call
to \fBkmem_cache_free()\fR, and can also be invoked during
\fBkmem_cache_destroy()\fR. Therefore, the functions that a constructor or
destructor invokes must be appropriate in that context. Furthermore, the
allocator may also call the constructor and destructor on objects still under
its control without client involvement.
.sp
.LP
\fBkmem_cache_create()\fR and \fBkmem_cache_destroy()\fR must not be called
from interrupt context. \fBkmem_cache_create()\fR can also block for available
memory.
.sp
.LP
\fBkmem_cache_alloc()\fR can be called from interrupt context only if the
\fBKM_NOSLEEP\fR flag is set. It can be called from user or kernel context with
any valid flag.
.sp
.LP
\fBkmem_cache_free()\fR can be called from user, kernel, or interrupt context.
.sp
.LP
\fBkmem_cache_set_move()\fR is called from the same context as
\fBkmem_cache_create()\fR, immediately after \fBkmem_cache_create()\fR and
before allocating any objects from the cache.
.sp
.LP
The registered \fBmove()\fR callback is always invoked in the same global
callback thread dedicated for move requests, guaranteeing that no matter how
many clients register a \fBmove()\fR function, the allocator never tries to
move more than one object at a time. Neither the allocator nor the client can
be assumed to know the object's whereabouts at the time of the callback.
.SH EXAMPLES
.LP
\fBExample 1 \fRObject Caching
.sp
.LP
Consider the following data structure:

.sp
.in +2
.nf
struct foo {
    kmutex_t foo_lock;
    kcondvar_t foo_cv;
    struct bar *foo_barlist;
    int foo_refcnt;
    };
.fi
.in -2

.sp
.LP
Assume that a \fBfoo\fR structure cannot be freed until there are no
outstanding references to it (\fBfoo_refcnt == 0\fR) and all of its pending
\fBbar\fR events (whatever they are) have completed (\fBfoo_barlist ==
NULL\fR). The life cycle of a dynamically allocated \fBfoo\fR would be
something like this:

.sp
.in +2
.nf
foo = kmem_alloc(sizeof (struct foo), KM_SLEEP);
mutex_init(&foo->foo_lock, ...);
cv_init(&foo->foo_cv, ...);
foo->foo_refcnt = 0;
foo->foo_barlist = NULL;
    use foo;
ASSERT(foo->foo_barlist == NULL);
ASSERT(foo->foo_refcnt == 0);
cv_destroy(&foo->foo_cv);
mutex_destroy(&foo->foo_lock);
kmem_free(foo);
.fi
.in -2

.sp
.LP
Notice that between each use of a \fBfoo\fR object we perform a sequence of
operations that constitutes nothing but expensive overhead. All of this
overhead (that is, everything other than \fBuse foo\fR above) can be eliminated
by object caching.

.sp
.in +2
.nf
int
foo_constructor(void *buf, void *arg, int tags)
{
    struct foo *foo = buf;
    mutex_init(&foo->foo_lock, ...);
    cv_init(&foo->foo_cv, ...);
    foo->foo_refcnt = 0;
    foo->foo_barlist = NULL;
    return (0);
}

void
foo_destructor(void *buf, void *arg)
{
    struct foo *foo = buf;
    ASSERT(foo->foo_barlist == NULL);
    ASSERT(foo->foo_refcnt == 0);
    cv_destroy(&foo->foo_cv);
    mutex_destroy(&foo->foo_lock);
}

user_arg = ddi_get_soft_state(foo_softc, instance);
(void) snprintf(buf, KSTAT_STRLEN, "foo%d_cache",
        ddi_get_instance(dip));
foo_cache = kmem_cache_create(buf,
        sizeof (struct foo), 0,
        foo_constructor, foo_destructor,
        NULL, user_arg, 0);
.fi
.in -2

.sp
.LP
To allocate, use, and free a \fBfoo\fR object:

.sp
.in +2
.nf
foo = kmem_cache_alloc(foo_cache, KM_SLEEP);
    use foo;
kmem_cache_free(foo_cache, foo);
.fi
.in -2

.sp
.LP
This makes \fBfoo\fR allocation fast, because the allocator will usually do
nothing more than fetch an already-constructed \fBfoo\fR from the cache.
\fBfoo_constructor\fR and \fBfoo_destructor\fR will be invoked only to populate
and drain the cache, respectively.

.LP
\fBExample 2 \fRRegistering a Move Callback
.sp
.LP
To register a \fBmove()\fR callback:

.sp
.in +2
.nf
object_cache = kmem_cache_create(...);
kmem_cache_set_move(object_cache, object_move);
.fi
.in -2

.SH RETURN VALUES
.LP
If successful, the constructor function must return \fB0\fR. If KM_NOSLEEP is
set and memory cannot be allocated without sleeping, the constructor must
return -\fB1\fR.
.sp
.LP
\fBkmem_cache_create()\fR returns a pointer to the allocated cache.
.sp
.LP
If successful, \fBkmem_cache_alloc()\fR returns a pointer to the allocated
object. If \fBKM_NOSLEEP\fR is set and memory cannot be allocated without
sleeping, \fBkmem_cache_alloc()\fR returns \fBNULL\fR.
.SH ATTRIBUTES
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
.TE

.SH SEE ALSO
.LP
\fBcondvar\fR(9F), \fBkmem_alloc\fR(9F), \fBmutex\fR(9F), \fBkstat\fR(9S)
.sp
.LP
\fIWriting Device Drivers\fR
.sp
.LP
\fIThe Slab Allocator: An Object-Caching Kernel Memory Allocator\fR, Bonwick,
J.; USENIX Summer 1994 Technical Conference (1994).
.sp
.LP
\fIMagazines and vmem: Extending the Slab Allocator to Many CPUs and Arbitrary
Resources\fR, Bonwick, J. and Adams, J.; USENIX 2001 Technical Conference
(2001).
.SH NOTES
.LP
The constructor must be immediately reversible by the destructor, since the
allocator may call the constructor and destructor on objects still under its
control at any time without client involvement.
.sp
.LP
The constructor must respect the \fIkmflags\fR argument by forwarding it to
allocations made inside the \fIconstructor\fR, and must not ASSERT anything
about the given flags.
.sp
.LP
The user argument forwarded to the constructor must be fully operational before
it is passed to \fBkmem_cache_create()\fR.