[32] | 1 | /*
|
---|
| 2 | * linux/mm/slab.h
|
---|
| 3 | * Written by Mark Hemment, 1996.
|
---|
| 4 | * (markhe@nextd.demon.co.uk)
|
---|
| 5 | */
|
---|
| 6 |
|
---|
[679] | 7 | #include <linux/gfp.h>
|
---|
| 8 | #include <linux/overflow.h>
|
---|
[442] | 9 | #include <linux/types.h>
|
---|
[679] | 10 | #include <linux/list.h>
|
---|
| 11 | #include <linux/workqueue.h>
|
---|
[442] | 12 |
|
---|
[32] | 13 | #if !defined(_LINUX_SLAB_H)
|
---|
| 14 | #define _LINUX_SLAB_H
|
---|
| 15 |
|
---|
| 16 | #if defined(__KERNEL__)
|
---|
| 17 |
|
---|
| 18 | typedef struct kmem_cache_s kmem_cache_t;
|
---|
| 19 |
|
---|
| 20 | /* flags for kmem_cache_alloc() */
|
---|
| 21 | #define SLAB_BUFFER GFP_BUFFER
|
---|
| 22 | #define SLAB_ATOMIC GFP_ATOMIC
|
---|
| 23 | #define SLAB_USER GFP_USER
|
---|
| 24 | #define SLAB_KERNEL GFP_KERNEL
|
---|
| 25 | #define SLAB_NFS GFP_NFS
|
---|
| 26 | #define SLAB_DMA GFP_DMA
|
---|
| 27 |
|
---|
| 28 | #define SLAB_LEVEL_MASK 0x0000007fUL
|
---|
| 29 | #define SLAB_NO_GROW 0x00001000UL /* don't grow a cache */
|
---|
| 30 |
|
---|
| 31 | /* flags to pass to kmem_cache_create().
|
---|
| 32 | * The first 3 are only valid when the allocator as been build
|
---|
| 33 | * SLAB_DEBUG_SUPPORT.
|
---|
| 34 | */
|
---|
| 35 | #define SLAB_DEBUG_FREE 0x00000100UL /* Peform (expensive) checks on free */
|
---|
| 36 | #define SLAB_DEBUG_INITIAL 0x00000200UL /* Call constructor (as verifier) */
|
---|
| 37 | #define SLAB_RED_ZONE 0x00000400UL /* Red zone objs in a cache */
|
---|
| 38 | #define SLAB_POISON 0x00000800UL /* Poison objects */
|
---|
| 39 | #define SLAB_NO_REAP 0x00001000UL /* never reap from the cache */
|
---|
| 40 | #define SLAB_HWCACHE_ALIGN 0x00002000UL /* align objs on a h/w cache lines */
|
---|
| 41 | #if 0
|
---|
| 42 | #define SLAB_HIGH_PACK 0x00004000UL /* XXX */
|
---|
| 43 | #endif
|
---|
| 44 |
|
---|
| 45 | /* flags passed to a constructor func */
|
---|
| 46 | #define SLAB_CTOR_CONSTRUCTOR 0x001UL /* if not set, then deconstructor */
|
---|
| 47 | #define SLAB_CTOR_ATOMIC 0x002UL /* tell constructor it can't sleep */
|
---|
| 48 | #define SLAB_CTOR_VERIFY 0x004UL /* tell constructor it's a verify call */
|
---|
| 49 |
|
---|
| 50 | #endif /* __KERNEL__ */
|
---|
| 51 |
|
---|
[679] | 52 | /*
|
---|
| 53 | * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
|
---|
| 54 | *
|
---|
| 55 | * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
|
---|
| 56 | *
|
---|
| 57 | * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
|
---|
| 58 | * Both make kfree a no-op.
|
---|
| 59 | */
|
---|
| 60 | #define ZERO_SIZE_PTR ((void *)16)
|
---|
| 61 |
|
---|
[32] | 62 | //NOTE: enabling this in the non-KEE driver causes problems (file name strings
|
---|
| 63 | // put in seperate private segments)
|
---|
| 64 | #ifdef DEBUGHEAP
|
---|
| 65 | extern void near *__kmalloc(int, int, const char *filename, int lineno);
|
---|
| 66 | extern void __kfree(const void near *, const char *filename, int lineno);
|
---|
| 67 |
|
---|
[679] | 68 | static inline void *kmalloc(size_t size, gfp_t flags)
|
---|
| 69 | {
|
---|
| 70 | return __kmalloc(size, flags, __FILE__, __LINE__);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
[32] | 74 | #define kfree(a) __kfree(a, __FILE__, __LINE__)
|
---|
| 75 | #define kfree_s(a,b) __kfree(a, __FILE__, __LINE__)
|
---|
| 76 | #define kfree_nocheck(a) __kfree(a, __FILE__, __LINE__)
|
---|
| 77 |
|
---|
| 78 | #else
|
---|
| 79 | extern void near *__kmalloc(int, int);
|
---|
| 80 | extern void __kfree(const void near *);
|
---|
| 81 |
|
---|
| 82 | #define kmalloc(a,b) __kmalloc(a,b)
|
---|
| 83 | #define kfree(a) __kfree(a)
|
---|
| 84 |
|
---|
| 85 | #define kfree_s(a,b) kfree(a)
|
---|
| 86 | #define kfree_nocheck(a) kfree(a)
|
---|
| 87 | #endif
|
---|
| 88 |
|
---|
[772] | 89 | /**
|
---|
| 90 | * kzalloc - allocate memory. The memory is set to zero.
|
---|
| 91 | * @size: how many bytes of memory are required.
|
---|
| 92 | * @flags: the type of memory to allocate (see kmalloc).
|
---|
| 93 | */
|
---|
| 94 | static inline void *kzalloc(size_t size, gfp_t flags)
|
---|
| 95 | {
|
---|
| 96 | return kmalloc(size, flags | __GFP_ZERO);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[442] | 99 | void *kcalloc(size_t n, size_t size, unsigned int __nocast gfp_flags);
|
---|
[679] | 100 | void *krealloc(const void *, size_t, gfp_t);
|
---|
[442] | 101 |
|
---|
[679] | 102 | #define SIZE_MAX (~(size_t)0)
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * kmalloc_array - allocate memory for an array.
|
---|
| 106 | * @n: number of elements.
|
---|
| 107 | * @size: element size.
|
---|
| 108 | * @flags: the type of memory to allocate (see kmalloc).
|
---|
| 109 | */
|
---|
| 110 | static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
|
---|
| 111 | {
|
---|
| 112 | if (size != 0 && n > SIZE_MAX / size)
|
---|
| 113 | return NULL;
|
---|
| 114 | return __kmalloc(n * size, flags);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[695] | 117 | /**
|
---|
| 118 | * krealloc_array - reallocate memory for an array.
|
---|
| 119 | * @p: pointer to the memory chunk to reallocate
|
---|
| 120 | * @new_n: new number of elements to alloc
|
---|
| 121 | * @new_size: new size of a single member of the array
|
---|
| 122 | * @flags: the type of memory to allocate (see kmalloc)
|
---|
| 123 | */
|
---|
| 124 | static __must_check inline void *
|
---|
| 125 | krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t flags)
|
---|
| 126 | {
|
---|
| 127 | // size_t bytes;
|
---|
| 128 |
|
---|
| 129 | // if (check_mul_overflow(new_n, new_size, &bytes))
|
---|
| 130 | // return NULL;
|
---|
| 131 |
|
---|
| 132 | return krealloc(p, new_n*new_size, flags);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[679] | 135 | #define kmalloc_node_track_caller(size, flags, node) \
|
---|
| 136 | kmalloc_track_caller(size, flags)
|
---|
| 137 | #define kmalloc_track_caller(size, flags) __kmalloc(size, flags)
|
---|
| 138 | #define kvfree(arg) kfree(arg)
|
---|
| 139 |
|
---|
| 140 | struct kmem_cache {
|
---|
| 141 | unsigned int object_size;/* The original size of the object */
|
---|
| 142 | unsigned int size; /* The aligned/padded/added on size */
|
---|
| 143 | unsigned int align; /* Alignment as calculated */
|
---|
| 144 | unsigned long flags; /* Active flags on the slab */
|
---|
| 145 | const char *name; /* Slab name for sysfs */
|
---|
| 146 | int refcount; /* Use counter */
|
---|
| 147 | void (*ctor)(void *); /* Called on object slot creation */
|
---|
| 148 | struct list_head list; /* List of all slab caches on the system */
|
---|
| 149 | };
|
---|
| 150 |
|
---|
| 151 | size_t ksize(const void *);
|
---|
| 152 |
|
---|
| 153 | static inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
|
---|
| 154 | {
|
---|
| 155 | return __kmalloc(size, flags);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
|
---|
| 159 | {
|
---|
| 160 | return __kmalloc_node(size, flags, node);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[32] | 163 | #endif /* _LINUX_SLAB_H */
|
---|