slab.h 8.2 KB

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