slab.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. 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. /**
  67. * kmalloc - allocate memory
  68. * @size: how many bytes of memory are required.
  69. * @flags: the type of memory to allocate.
  70. *
  71. * kmalloc is the normal method of allocating memory
  72. * in the kernel.
  73. *
  74. * The @flags argument may be one of:
  75. *
  76. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  77. *
  78. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  79. *
  80. * %GFP_ATOMIC - Allocation will not sleep.
  81. * For example, use this inside interrupt handlers.
  82. *
  83. * %GFP_HIGHUSER - Allocate pages from high memory.
  84. *
  85. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  86. *
  87. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  88. *
  89. * Also it is possible to set different flags by OR'ing
  90. * in one or more of the following additional @flags:
  91. *
  92. * %__GFP_COLD - Request cache-cold pages instead of
  93. * trying to return cache-warm pages.
  94. *
  95. * %__GFP_DMA - Request memory from the DMA-capable zone.
  96. *
  97. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  98. *
  99. * %__GFP_HIGHMEM - Allocated memory may be from highmem.
  100. *
  101. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  102. * (think twice before using).
  103. *
  104. * %__GFP_NORETRY - If memory is not immediately available,
  105. * then give up at once.
  106. *
  107. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  108. *
  109. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  110. */
  111. static inline void *kmalloc(size_t size, gfp_t flags)
  112. {
  113. if (__builtin_constant_p(size)) {
  114. int i = 0;
  115. #define CACHE(x) \
  116. if (size <= x) \
  117. goto found; \
  118. else \
  119. i++;
  120. #include "kmalloc_sizes.h"
  121. #undef CACHE
  122. {
  123. extern void __you_cannot_kmalloc_that_much(void);
  124. __you_cannot_kmalloc_that_much();
  125. }
  126. found:
  127. return kmem_cache_alloc((flags & GFP_DMA) ?
  128. malloc_sizes[i].cs_dmacachep :
  129. malloc_sizes[i].cs_cachep, flags);
  130. }
  131. return __kmalloc(size, flags);
  132. }
  133. /*
  134. * kmalloc_track_caller is a special version of kmalloc that records the
  135. * calling function of the routine calling it for slab leak tracking instead
  136. * of just the calling function (confusing, eh?).
  137. * It's useful when the call to kmalloc comes from a widely-used standard
  138. * allocator where we care about the real place the memory allocation
  139. * request comes from.
  140. */
  141. #ifndef CONFIG_DEBUG_SLAB
  142. #define kmalloc_track_caller(size, flags) \
  143. __kmalloc(size, flags)
  144. #else
  145. extern void *__kmalloc_track_caller(size_t, gfp_t, void*);
  146. #define kmalloc_track_caller(size, flags) \
  147. __kmalloc_track_caller(size, flags, __builtin_return_address(0))
  148. #endif
  149. extern void *__kzalloc(size_t, gfp_t);
  150. /**
  151. * kzalloc - allocate memory. The memory is set to zero.
  152. * @size: how many bytes of memory are required.
  153. * @flags: the type of memory to allocate (see kmalloc).
  154. */
  155. static inline void *kzalloc(size_t size, gfp_t flags)
  156. {
  157. if (__builtin_constant_p(size)) {
  158. int i = 0;
  159. #define CACHE(x) \
  160. if (size <= x) \
  161. goto found; \
  162. else \
  163. i++;
  164. #include "kmalloc_sizes.h"
  165. #undef CACHE
  166. {
  167. extern void __you_cannot_kzalloc_that_much(void);
  168. __you_cannot_kzalloc_that_much();
  169. }
  170. found:
  171. return kmem_cache_zalloc((flags & GFP_DMA) ?
  172. malloc_sizes[i].cs_dmacachep :
  173. malloc_sizes[i].cs_cachep, flags);
  174. }
  175. return __kzalloc(size, flags);
  176. }
  177. /**
  178. * kcalloc - allocate memory for an array. The memory is set to zero.
  179. * @n: number of elements.
  180. * @size: element size.
  181. * @flags: the type of memory to allocate.
  182. */
  183. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  184. {
  185. if (n != 0 && size > ULONG_MAX / n)
  186. return NULL;
  187. return kzalloc(n * size, flags);
  188. }
  189. extern void kfree(const void *);
  190. extern unsigned int ksize(const void *);
  191. extern int slab_is_available(void);
  192. #ifdef CONFIG_NUMA
  193. extern void *kmem_cache_alloc_node(kmem_cache_t *, gfp_t flags, int node);
  194. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  195. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  196. {
  197. if (__builtin_constant_p(size)) {
  198. int i = 0;
  199. #define CACHE(x) \
  200. if (size <= x) \
  201. goto found; \
  202. else \
  203. i++;
  204. #include "kmalloc_sizes.h"
  205. #undef CACHE
  206. {
  207. extern void __you_cannot_kmalloc_that_much(void);
  208. __you_cannot_kmalloc_that_much();
  209. }
  210. found:
  211. return kmem_cache_alloc_node((flags & GFP_DMA) ?
  212. malloc_sizes[i].cs_dmacachep :
  213. malloc_sizes[i].cs_cachep, flags, node);
  214. }
  215. return __kmalloc_node(size, flags, node);
  216. }
  217. /*
  218. * kmalloc_node_track_caller is a special version of kmalloc_node that
  219. * records the calling function of the routine calling it for slab leak
  220. * tracking instead of just the calling function (confusing, eh?).
  221. * It's useful when the call to kmalloc_node comes from a widely-used
  222. * standard allocator where we care about the real place the memory
  223. * allocation request comes from.
  224. */
  225. #ifndef CONFIG_DEBUG_SLAB
  226. #define kmalloc_node_track_caller(size, flags, node) \
  227. __kmalloc_node(size, flags, node)
  228. #else
  229. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, void *);
  230. #define kmalloc_node_track_caller(size, flags, node) \
  231. __kmalloc_node_track_caller(size, flags, node, \
  232. __builtin_return_address(0))
  233. #endif
  234. #else /* CONFIG_NUMA */
  235. static inline void *kmem_cache_alloc_node(kmem_cache_t *cachep, gfp_t flags, int node)
  236. {
  237. return kmem_cache_alloc(cachep, flags);
  238. }
  239. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  240. {
  241. return kmalloc(size, flags);
  242. }
  243. #define kmalloc_node_track_caller(size, flags, node) \
  244. kmalloc_track_caller(size, flags)
  245. #endif
  246. extern int FASTCALL(kmem_cache_reap(int));
  247. extern int FASTCALL(kmem_ptr_validate(kmem_cache_t *cachep, void *ptr));
  248. #else /* CONFIG_SLOB */
  249. /* SLOB allocator routines */
  250. void kmem_cache_init(void);
  251. struct kmem_cache *kmem_cache_create(const char *c, size_t, size_t,
  252. unsigned long,
  253. void (*)(void *, struct kmem_cache *, unsigned long),
  254. void (*)(void *, struct kmem_cache *, unsigned long));
  255. void kmem_cache_destroy(struct kmem_cache *c);
  256. void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags);
  257. void *kmem_cache_zalloc(struct kmem_cache *, gfp_t);
  258. void kmem_cache_free(struct kmem_cache *c, void *b);
  259. const char *kmem_cache_name(struct kmem_cache *);
  260. void *kmalloc(size_t size, gfp_t flags);
  261. void *__kzalloc(size_t size, gfp_t flags);
  262. void kfree(const void *m);
  263. unsigned int ksize(const void *m);
  264. unsigned int kmem_cache_size(struct kmem_cache *c);
  265. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  266. {
  267. return __kzalloc(n * size, flags);
  268. }
  269. #define kmem_cache_shrink(d) (0)
  270. #define kmem_cache_reap(a)
  271. #define kmem_ptr_validate(a, b) (0)
  272. #define kmem_cache_alloc_node(c, f, n) kmem_cache_alloc(c, f)
  273. #define kmalloc_node(s, f, n) kmalloc(s, f)
  274. #define kzalloc(s, f) __kzalloc(s, f)
  275. #define kmalloc_track_caller kmalloc
  276. #define kmalloc_node_track_caller kmalloc_node
  277. #endif /* CONFIG_SLOB */
  278. #endif /* __KERNEL__ */
  279. #endif /* _LINUX_SLAB_H */