1 | /*- |
---|
2 | * Copyright (c) 2010 Isilon Systems, Inc. |
---|
3 | * Copyright (c) 2010 iX Systems, Inc. |
---|
4 | * Copyright (c) 2010 Panasas, Inc. |
---|
5 | * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. |
---|
6 | * All rights reserved. |
---|
7 | * |
---|
8 | * Redistribution and use in source and binary forms, with or without |
---|
9 | * modification, are permitted provided that the following conditions |
---|
10 | * are met: |
---|
11 | * 1. Redistributions of source code must retain the above copyright |
---|
12 | * notice unmodified, this list of conditions, and the following |
---|
13 | * disclaimer. |
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer in the |
---|
16 | * documentation and/or other materials provided with the distribution. |
---|
17 | * |
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | * |
---|
29 | * $FreeBSD$ |
---|
30 | */ |
---|
31 | #ifndef _LINUX_SLAB_H_ |
---|
32 | #define _LINUX_SLAB_H_ |
---|
33 | |
---|
34 | #include <sys/param.h> |
---|
35 | #include <sys/systm.h> |
---|
36 | #include <sys/kernel.h> |
---|
37 | #include <sys/malloc.h> |
---|
38 | #include <vm/uma.h> |
---|
39 | |
---|
40 | #include <linux/types.h> |
---|
41 | #include <linux/gfp.h> |
---|
42 | |
---|
43 | MALLOC_DECLARE(M_KMALLOC); |
---|
44 | |
---|
45 | #define kmalloc(size, flags) malloc((size), M_KMALLOC, (flags)) |
---|
46 | #define kmalloc_array(size, n, flags) malloc((size) * (n), M_KMALLOC, (flags)) |
---|
47 | #define kvmalloc(size) kmalloc((size), 0) |
---|
48 | #define kzalloc(size, flags) kmalloc((size), (flags) | M_ZERO) |
---|
49 | #define kzalloc_node(size, flags, node) kzalloc(size, flags) |
---|
50 | #define kfree(ptr) free(__DECONST(void *, (ptr)), M_KMALLOC) |
---|
51 | #define krealloc(ptr, size, flags) realloc((ptr), (size), M_KMALLOC, (flags)) |
---|
52 | #define kcalloc(n, size, flags) kmalloc((n) * (size), flags | M_ZERO) |
---|
53 | #define vzalloc(size) kzalloc(size, GFP_KERNEL | __GFP_NOWARN) |
---|
54 | #define vfree(arg) kfree(arg) |
---|
55 | #define kvfree(arg) kfree(arg) |
---|
56 | #define vmalloc(size) kmalloc(size, GFP_KERNEL) |
---|
57 | #define vmalloc_node(size, node) kmalloc(size, GFP_KERNEL) |
---|
58 | |
---|
59 | struct kmem_cache { |
---|
60 | uma_zone_t cache_zone; |
---|
61 | void (*cache_ctor)(void *); |
---|
62 | }; |
---|
63 | |
---|
64 | #define SLAB_HWCACHE_ALIGN 0x0001 |
---|
65 | |
---|
66 | static inline int |
---|
67 | kmem_ctor(void *mem, int size, void *arg, int flags) |
---|
68 | { |
---|
69 | void (*ctor)(void *); |
---|
70 | |
---|
71 | ctor = arg; |
---|
72 | ctor(mem); |
---|
73 | |
---|
74 | return (0); |
---|
75 | } |
---|
76 | |
---|
77 | static inline struct kmem_cache * |
---|
78 | kmem_cache_create(char *name, size_t size, size_t align, u_long flags, |
---|
79 | void (*ctor)(void *)) |
---|
80 | { |
---|
81 | struct kmem_cache *c; |
---|
82 | |
---|
83 | c = malloc(sizeof(*c), M_KMALLOC, M_WAITOK); |
---|
84 | if (align) |
---|
85 | align--; |
---|
86 | if (flags & SLAB_HWCACHE_ALIGN) |
---|
87 | align = UMA_ALIGN_CACHE; |
---|
88 | c->cache_zone = uma_zcreate(name, size, ctor ? kmem_ctor : NULL, |
---|
89 | NULL, NULL, NULL, align, 0); |
---|
90 | c->cache_ctor = ctor; |
---|
91 | |
---|
92 | return c; |
---|
93 | } |
---|
94 | |
---|
95 | static inline void * |
---|
96 | kmem_cache_alloc(struct kmem_cache *c, int flags) |
---|
97 | { |
---|
98 | return uma_zalloc_arg(c->cache_zone, c->cache_ctor, flags); |
---|
99 | } |
---|
100 | |
---|
101 | static inline void |
---|
102 | kmem_cache_free(struct kmem_cache *c, void *m) |
---|
103 | { |
---|
104 | uma_zfree(c->cache_zone, m); |
---|
105 | } |
---|
106 | |
---|
107 | static inline void |
---|
108 | kmem_cache_destroy(struct kmem_cache *c) |
---|
109 | { |
---|
110 | uma_zdestroy(c->cache_zone); |
---|
111 | free(c, M_KMALLOC); |
---|
112 | } |
---|
113 | |
---|
114 | #endif /* _LINUX_SLAB_H_ */ |
---|