slab.h 9.4 KB

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