slab.h 9.3 KB

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