slab.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Written by Mark Hemment, 1996 (markhe@nextd.demon.co.uk).
  3. *
  4. * (C) SGI 2006, Christoph Lameter <clameter@sgi.com>
  5. * Cleaned up and restructured to ease the addition of alternative
  6. * implementations of SLAB allocators.
  7. */
  8. #ifndef _LINUX_SLAB_H
  9. #define _LINUX_SLAB_H
  10. #ifdef __KERNEL__
  11. #include <linux/gfp.h>
  12. #include <linux/types.h>
  13. typedef struct kmem_cache kmem_cache_t __deprecated;
  14. /*
  15. * Flags to pass to kmem_cache_create().
  16. * The ones marked DEBUG are only valid if CONFIG_SLAB_DEBUG is set.
  17. */
  18. #define SLAB_DEBUG_FREE 0x00000100UL /* DEBUG: Perform (expensive) checks on free */
  19. #define SLAB_DEBUG_INITIAL 0x00000200UL /* DEBUG: Call constructor (as verifier) */
  20. #define SLAB_RED_ZONE 0x00000400UL /* DEBUG: Red zone objs in a cache */
  21. #define SLAB_POISON 0x00000800UL /* DEBUG: Poison objects */
  22. #define SLAB_HWCACHE_ALIGN 0x00002000UL /* Align objs on cache lines */
  23. #define SLAB_CACHE_DMA 0x00004000UL /* Use GFP_DMA memory */
  24. #define SLAB_MUST_HWCACHE_ALIGN 0x00008000UL /* Force alignment even if debuggin is active */
  25. #define SLAB_STORE_USER 0x00010000UL /* DEBUG: Store the last owner for bug hunting */
  26. #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */
  27. #define SLAB_PANIC 0x00040000UL /* Panic if kmem_cache_create() fails */
  28. #define SLAB_DESTROY_BY_RCU 0x00080000UL /* Defer freeing slabs to RCU */
  29. #define SLAB_MEM_SPREAD 0x00100000UL /* Spread some memory over cpuset */
  30. /* Flags passed to a constructor functions */
  31. #define SLAB_CTOR_CONSTRUCTOR 0x001UL /* If not set, then deconstructor */
  32. #define SLAB_CTOR_ATOMIC 0x002UL /* Tell constructor it can't sleep */
  33. #define SLAB_CTOR_VERIFY 0x004UL /* Tell constructor it's a verify call */
  34. /*
  35. * struct kmem_cache related prototypes
  36. */
  37. void __init kmem_cache_init(void);
  38. extern int slab_is_available(void);
  39. struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
  40. unsigned long,
  41. void (*)(void *, struct kmem_cache *, unsigned long),
  42. void (*)(void *, struct kmem_cache *, unsigned long));
  43. void kmem_cache_destroy(struct kmem_cache *);
  44. int kmem_cache_shrink(struct kmem_cache *);
  45. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  46. void *kmem_cache_zalloc(struct kmem_cache *, gfp_t);
  47. void kmem_cache_free(struct kmem_cache *, void *);
  48. unsigned int kmem_cache_size(struct kmem_cache *);
  49. const char *kmem_cache_name(struct kmem_cache *);
  50. int kmem_ptr_validate(struct kmem_cache *cachep, void *ptr);
  51. #ifdef CONFIG_NUMA
  52. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  53. #else
  54. static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
  55. gfp_t flags, int node)
  56. {
  57. return kmem_cache_alloc(cachep, flags);
  58. }
  59. #endif
  60. /*
  61. * Common kmalloc functions provided by all allocators
  62. */
  63. void *__kmalloc(size_t, gfp_t);
  64. void *__kzalloc(size_t, gfp_t);
  65. void kfree(const void *);
  66. unsigned int ksize(const void *);
  67. /**
  68. * kcalloc - allocate memory for an array. The memory is set to zero.
  69. * @n: number of elements.
  70. * @size: element size.
  71. * @flags: the type of memory to allocate.
  72. */
  73. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  74. {
  75. if (n != 0 && size > ULONG_MAX / n)
  76. return NULL;
  77. return __kzalloc(n * size, flags);
  78. }
  79. /*
  80. * Allocator specific definitions. These are mainly used to establish optimized
  81. * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by selecting
  82. * the appropriate general cache at compile time.
  83. */
  84. #ifdef CONFIG_SLAB
  85. #include <linux/slab_def.h>
  86. #else
  87. /*
  88. * Fallback definitions for an allocator not wanting to provide
  89. * its own optimized kmalloc definitions (like SLOB).
  90. */
  91. #if defined(CONFIG_NUMA) || defined(CONFIG_DEBUG_SLAB)
  92. #error "SLAB fallback definitions not usable for NUMA or Slab debug"
  93. #endif
  94. /**
  95. * kmalloc - allocate memory
  96. * @size: how many bytes of memory are required.
  97. * @flags: the type of memory to allocate.
  98. *
  99. * kmalloc is the normal method of allocating memory
  100. * in the kernel.
  101. *
  102. * The @flags argument may be one of:
  103. *
  104. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  105. *
  106. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  107. *
  108. * %GFP_ATOMIC - Allocation will not sleep.
  109. * For example, use this inside interrupt handlers.
  110. *
  111. * %GFP_HIGHUSER - Allocate pages from high memory.
  112. *
  113. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  114. *
  115. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  116. *
  117. * Also it is possible to set different flags by OR'ing
  118. * in one or more of the following additional @flags:
  119. *
  120. * %__GFP_COLD - Request cache-cold pages instead of
  121. * trying to return cache-warm pages.
  122. *
  123. * %__GFP_DMA - Request memory from the DMA-capable zone.
  124. *
  125. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  126. *
  127. * %__GFP_HIGHMEM - Allocated memory may be from highmem.
  128. *
  129. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  130. * (think twice before using).
  131. *
  132. * %__GFP_NORETRY - If memory is not immediately available,
  133. * then give up at once.
  134. *
  135. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  136. *
  137. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  138. */
  139. void *kmalloc(size_t size, gfp_t flags)
  140. {
  141. return __kmalloc(size, flags);
  142. }
  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. void *kzalloc(size_t size, gfp_t flags)
  149. {
  150. return __kzalloc(size, flags);
  151. }
  152. #endif
  153. /*
  154. * kmalloc_track_caller is a special version of kmalloc that records the
  155. * calling function of the routine calling it for slab leak tracking instead
  156. * of just the calling function (confusing, eh?).
  157. * It's useful when the call to kmalloc comes from a widely-used standard
  158. * allocator where we care about the real place the memory allocation
  159. * request comes from.
  160. */
  161. #ifdef CONFIG_DEBUG_SLAB
  162. extern void *__kmalloc_track_caller(size_t, gfp_t, void*);
  163. #define kmalloc_track_caller(size, flags) \
  164. __kmalloc_track_caller(size, flags, __builtin_return_address(0))
  165. #else
  166. #define kmalloc_track_caller(size, flags) \
  167. __kmalloc(size, flags)
  168. #endif /* DEBUG_SLAB */
  169. #ifdef CONFIG_NUMA
  170. /*
  171. * kmalloc_node_track_caller is a special version of kmalloc_node that
  172. * records the calling function of the routine calling it for slab leak
  173. * tracking instead of just the calling function (confusing, eh?).
  174. * It's useful when the call to kmalloc_node comes from a widely-used
  175. * standard allocator where we care about the real place the memory
  176. * allocation request comes from.
  177. */
  178. #ifdef CONFIG_DEBUG_SLAB
  179. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, void *);
  180. #define kmalloc_node_track_caller(size, flags, node) \
  181. __kmalloc_node_track_caller(size, flags, node, \
  182. __builtin_return_address(0))
  183. #else
  184. #define kmalloc_node_track_caller(size, flags, node) \
  185. __kmalloc_node(size, flags, node)
  186. #endif
  187. #else /* CONFIG_NUMA */
  188. #define kmalloc_node_track_caller(size, flags, node) \
  189. kmalloc_track_caller(size, flags)
  190. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  191. {
  192. return kmalloc(size, flags);
  193. }
  194. #endif /* !CONFIG_NUMA */
  195. #endif /* __KERNEL__ */
  196. #endif /* _LINUX_SLAB_H */