slab.h 9.5 KB

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