slab.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * linux/mm/slab.h
  3. * Written by Mark Hemment, 1996.
  4. * (markhe@nextd.demon.co.uk)
  5. */
  6. #ifndef _LINUX_SLAB_H
  7. #define _LINUX_SLAB_H
  8. #if defined(__KERNEL__)
  9. typedef struct kmem_cache kmem_cache_t;
  10. #include <linux/gfp.h>
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <asm/page.h> /* kmalloc_sizes.h needs PAGE_SIZE */
  14. #include <asm/cache.h> /* kmalloc_sizes.h needs L1_CACHE_BYTES */
  15. /* flags for kmem_cache_alloc() */
  16. #define SLAB_NOFS GFP_NOFS
  17. #define SLAB_NOIO GFP_NOIO
  18. #define SLAB_ATOMIC GFP_ATOMIC
  19. #define SLAB_USER GFP_USER
  20. #define SLAB_KERNEL GFP_KERNEL
  21. #define SLAB_DMA GFP_DMA
  22. #define SLAB_LEVEL_MASK GFP_LEVEL_MASK
  23. #define SLAB_NO_GROW __GFP_NO_GROW /* don't grow a cache */
  24. /* flags to pass to kmem_cache_create().
  25. * The first 3 are only valid when the allocator as been build
  26. * SLAB_DEBUG_SUPPORT.
  27. */
  28. #define SLAB_DEBUG_FREE 0x00000100UL /* Peform (expensive) checks on free */
  29. #define SLAB_DEBUG_INITIAL 0x00000200UL /* Call constructor (as verifier) */
  30. #define SLAB_RED_ZONE 0x00000400UL /* Red zone objs in a cache */
  31. #define SLAB_POISON 0x00000800UL /* Poison objects */
  32. #define SLAB_HWCACHE_ALIGN 0x00002000UL /* align objs on a h/w cache lines */
  33. #define SLAB_CACHE_DMA 0x00004000UL /* use GFP_DMA memory */
  34. #define SLAB_MUST_HWCACHE_ALIGN 0x00008000UL /* force alignment */
  35. #define SLAB_STORE_USER 0x00010000UL /* store the last owner for bug hunting */
  36. #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* track pages allocated to indicate
  37. what is reclaimable later*/
  38. #define SLAB_PANIC 0x00040000UL /* panic if kmem_cache_create() fails */
  39. #define SLAB_DESTROY_BY_RCU 0x00080000UL /* defer freeing pages to RCU */
  40. #define SLAB_MEM_SPREAD 0x00100000UL /* Spread some memory over cpuset */
  41. /* flags passed to a constructor func */
  42. #define SLAB_CTOR_CONSTRUCTOR 0x001UL /* if not set, then deconstructor */
  43. #define SLAB_CTOR_ATOMIC 0x002UL /* tell constructor it can't sleep */
  44. #define SLAB_CTOR_VERIFY 0x004UL /* tell constructor it's a verify call */
  45. #ifndef CONFIG_SLOB
  46. /* prototypes */
  47. extern void __init kmem_cache_init(void);
  48. extern kmem_cache_t *kmem_cache_create(const char *, size_t, size_t, unsigned long,
  49. void (*)(void *, kmem_cache_t *, unsigned long),
  50. void (*)(void *, kmem_cache_t *, unsigned long));
  51. extern void kmem_cache_destroy(kmem_cache_t *);
  52. extern int kmem_cache_shrink(kmem_cache_t *);
  53. extern void *kmem_cache_alloc(kmem_cache_t *, gfp_t);
  54. extern void *kmem_cache_zalloc(struct kmem_cache *, gfp_t);
  55. extern void kmem_cache_free(kmem_cache_t *, void *);
  56. extern unsigned int kmem_cache_size(kmem_cache_t *);
  57. extern const char *kmem_cache_name(kmem_cache_t *);
  58. /* Size description struct for general caches. */
  59. struct cache_sizes {
  60. size_t cs_size;
  61. kmem_cache_t *cs_cachep;
  62. kmem_cache_t *cs_dmacachep;
  63. };
  64. extern struct cache_sizes malloc_sizes[];
  65. extern void *__kmalloc(size_t, gfp_t);
  66. #ifndef CONFIG_DEBUG_SLAB
  67. #define ____kmalloc(size, flags) __kmalloc(size, flags)
  68. #else
  69. extern void *__kmalloc_track_caller(size_t, gfp_t, void*);
  70. #define ____kmalloc(size, flags) \
  71. __kmalloc_track_caller(size, flags, __builtin_return_address(0))
  72. #endif
  73. /**
  74. * kmalloc - allocate memory
  75. * @size: how many bytes of memory are required.
  76. * @flags: the type of memory to allocate.
  77. *
  78. * kmalloc is the normal method of allocating memory
  79. * in the kernel.
  80. *
  81. * The @flags argument may be one of:
  82. *
  83. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  84. *
  85. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  86. *
  87. * %GFP_ATOMIC - Allocation will not sleep.
  88. * For example, use this inside interrupt handlers.
  89. *
  90. * %GFP_HIGHUSER - Allocate pages from high memory.
  91. *
  92. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  93. *
  94. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  95. *
  96. * Also it is possible to set different flags by OR'ing
  97. * in one or more of the following additional @flags:
  98. *
  99. * %__GFP_COLD - Request cache-cold pages instead of
  100. * trying to return cache-warm pages.
  101. *
  102. * %__GFP_DMA - Request memory from the DMA-capable zone.
  103. *
  104. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  105. *
  106. * %__GFP_HIGHMEM - Allocated memory may be from highmem.
  107. *
  108. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  109. * (think twice before using).
  110. *
  111. * %__GFP_NORETRY - If memory is not immediately available,
  112. * then give up at once.
  113. *
  114. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  115. *
  116. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  117. */
  118. static inline void *kmalloc(size_t size, gfp_t flags)
  119. {
  120. if (__builtin_constant_p(size)) {
  121. int i = 0;
  122. #define CACHE(x) \
  123. if (size <= x) \
  124. goto found; \
  125. else \
  126. i++;
  127. #include "kmalloc_sizes.h"
  128. #undef CACHE
  129. {
  130. extern void __you_cannot_kmalloc_that_much(void);
  131. __you_cannot_kmalloc_that_much();
  132. }
  133. found:
  134. return kmem_cache_alloc((flags & GFP_DMA) ?
  135. malloc_sizes[i].cs_dmacachep :
  136. malloc_sizes[i].cs_cachep, flags);
  137. }
  138. return __kmalloc(size, flags);
  139. }
  140. extern void *__kzalloc(size_t, gfp_t);
  141. /**
  142. * kzalloc - allocate memory. The memory is set to zero.
  143. * @size: how many bytes of memory are required.
  144. * @flags: the type of memory to allocate (see kmalloc).
  145. */
  146. static inline void *kzalloc(size_t size, gfp_t flags)
  147. {
  148. if (__builtin_constant_p(size)) {
  149. int i = 0;
  150. #define CACHE(x) \
  151. if (size <= x) \
  152. goto found; \
  153. else \
  154. i++;
  155. #include "kmalloc_sizes.h"
  156. #undef CACHE
  157. {
  158. extern void __you_cannot_kzalloc_that_much(void);
  159. __you_cannot_kzalloc_that_much();
  160. }
  161. found:
  162. return kmem_cache_zalloc((flags & GFP_DMA) ?
  163. malloc_sizes[i].cs_dmacachep :
  164. malloc_sizes[i].cs_cachep, flags);
  165. }
  166. return __kzalloc(size, flags);
  167. }
  168. /**
  169. * kcalloc - allocate memory for an array. The memory is set to zero.
  170. * @n: number of elements.
  171. * @size: element size.
  172. * @flags: the type of memory to allocate.
  173. */
  174. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  175. {
  176. if (n != 0 && size > ULONG_MAX / n)
  177. return NULL;
  178. return kzalloc(n * size, flags);
  179. }
  180. extern void kfree(const void *);
  181. extern unsigned int ksize(const void *);
  182. extern int slab_is_available(void);
  183. #ifdef CONFIG_NUMA
  184. extern void *kmem_cache_alloc_node(kmem_cache_t *, gfp_t flags, int node);
  185. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  186. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  187. {
  188. if (__builtin_constant_p(size)) {
  189. int i = 0;
  190. #define CACHE(x) \
  191. if (size <= x) \
  192. goto found; \
  193. else \
  194. i++;
  195. #include "kmalloc_sizes.h"
  196. #undef CACHE
  197. {
  198. extern void __you_cannot_kmalloc_that_much(void);
  199. __you_cannot_kmalloc_that_much();
  200. }
  201. found:
  202. return kmem_cache_alloc_node((flags & GFP_DMA) ?
  203. malloc_sizes[i].cs_dmacachep :
  204. malloc_sizes[i].cs_cachep, flags, node);
  205. }
  206. return __kmalloc_node(size, flags, node);
  207. }
  208. #else
  209. static inline void *kmem_cache_alloc_node(kmem_cache_t *cachep, gfp_t flags, int node)
  210. {
  211. return kmem_cache_alloc(cachep, flags);
  212. }
  213. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  214. {
  215. return kmalloc(size, flags);
  216. }
  217. #endif
  218. extern int FASTCALL(kmem_cache_reap(int));
  219. extern int FASTCALL(kmem_ptr_validate(kmem_cache_t *cachep, void *ptr));
  220. #else /* CONFIG_SLOB */
  221. /* SLOB allocator routines */
  222. void kmem_cache_init(void);
  223. struct kmem_cache *kmem_cache_create(const char *c, size_t, size_t,
  224. unsigned long,
  225. void (*)(void *, struct kmem_cache *, unsigned long),
  226. void (*)(void *, struct kmem_cache *, unsigned long));
  227. void kmem_cache_destroy(struct kmem_cache *c);
  228. void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags);
  229. void *kmem_cache_zalloc(struct kmem_cache *, gfp_t);
  230. void kmem_cache_free(struct kmem_cache *c, void *b);
  231. const char *kmem_cache_name(struct kmem_cache *);
  232. void *kmalloc(size_t size, gfp_t flags);
  233. void *__kzalloc(size_t size, gfp_t flags);
  234. void kfree(const void *m);
  235. unsigned int ksize(const void *m);
  236. unsigned int kmem_cache_size(struct kmem_cache *c);
  237. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  238. {
  239. return __kzalloc(n * size, flags);
  240. }
  241. #define kmem_cache_shrink(d) (0)
  242. #define kmem_cache_reap(a)
  243. #define kmem_ptr_validate(a, b) (0)
  244. #define kmem_cache_alloc_node(c, f, n) kmem_cache_alloc(c, f)
  245. #define kmalloc_node(s, f, n) kmalloc(s, f)
  246. #define kzalloc(s, f) __kzalloc(s, f)
  247. #define ____kmalloc kmalloc
  248. #endif /* CONFIG_SLOB */
  249. /* System wide caches */
  250. extern kmem_cache_t *vm_area_cachep;
  251. extern kmem_cache_t *names_cachep;
  252. extern kmem_cache_t *files_cachep;
  253. extern kmem_cache_t *filp_cachep;
  254. extern kmem_cache_t *fs_cachep;
  255. extern kmem_cache_t *sighand_cachep;
  256. extern kmem_cache_t *bio_cachep;
  257. #endif /* __KERNEL__ */
  258. #endif /* _LINUX_SLAB_H */