slab.h 9.6 KB

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