malloc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. * $Id: malloc.c,v 1.28 2004/11/16 20:36:11 dwmw2 Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/jffs2.h>
  17. #include "nodelist.h"
  18. #if 0
  19. #define JFFS2_SLAB_POISON SLAB_POISON
  20. #else
  21. #define JFFS2_SLAB_POISON 0
  22. #endif
  23. // replace this by #define D3 (x) x for cache debugging
  24. #define D3(x)
  25. /* These are initialised to NULL in the kernel startup code.
  26. If you're porting to other operating systems, beware */
  27. static kmem_cache_t *full_dnode_slab;
  28. static kmem_cache_t *raw_dirent_slab;
  29. static kmem_cache_t *raw_inode_slab;
  30. static kmem_cache_t *tmp_dnode_info_slab;
  31. static kmem_cache_t *raw_node_ref_slab;
  32. static kmem_cache_t *node_frag_slab;
  33. static kmem_cache_t *inode_cache_slab;
  34. int __init jffs2_create_slab_caches(void)
  35. {
  36. full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
  37. sizeof(struct jffs2_full_dnode),
  38. 0, JFFS2_SLAB_POISON, NULL, NULL);
  39. if (!full_dnode_slab)
  40. goto err;
  41. raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
  42. sizeof(struct jffs2_raw_dirent),
  43. 0, JFFS2_SLAB_POISON, NULL, NULL);
  44. if (!raw_dirent_slab)
  45. goto err;
  46. raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
  47. sizeof(struct jffs2_raw_inode),
  48. 0, JFFS2_SLAB_POISON, NULL, NULL);
  49. if (!raw_inode_slab)
  50. goto err;
  51. tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode",
  52. sizeof(struct jffs2_tmp_dnode_info),
  53. 0, JFFS2_SLAB_POISON, NULL, NULL);
  54. if (!tmp_dnode_info_slab)
  55. goto err;
  56. raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref",
  57. sizeof(struct jffs2_raw_node_ref),
  58. 0, JFFS2_SLAB_POISON, NULL, NULL);
  59. if (!raw_node_ref_slab)
  60. goto err;
  61. node_frag_slab = kmem_cache_create("jffs2_node_frag",
  62. sizeof(struct jffs2_node_frag),
  63. 0, JFFS2_SLAB_POISON, NULL, NULL);
  64. if (!node_frag_slab)
  65. goto err;
  66. inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
  67. sizeof(struct jffs2_inode_cache),
  68. 0, JFFS2_SLAB_POISON, NULL, NULL);
  69. if (inode_cache_slab)
  70. return 0;
  71. err:
  72. jffs2_destroy_slab_caches();
  73. return -ENOMEM;
  74. }
  75. void jffs2_destroy_slab_caches(void)
  76. {
  77. if(full_dnode_slab)
  78. kmem_cache_destroy(full_dnode_slab);
  79. if(raw_dirent_slab)
  80. kmem_cache_destroy(raw_dirent_slab);
  81. if(raw_inode_slab)
  82. kmem_cache_destroy(raw_inode_slab);
  83. if(tmp_dnode_info_slab)
  84. kmem_cache_destroy(tmp_dnode_info_slab);
  85. if(raw_node_ref_slab)
  86. kmem_cache_destroy(raw_node_ref_slab);
  87. if(node_frag_slab)
  88. kmem_cache_destroy(node_frag_slab);
  89. if(inode_cache_slab)
  90. kmem_cache_destroy(inode_cache_slab);
  91. }
  92. struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
  93. {
  94. return kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
  95. }
  96. void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
  97. {
  98. kfree(x);
  99. }
  100. struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
  101. {
  102. struct jffs2_full_dnode *ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
  103. D3 (printk (KERN_DEBUG "alloc_full_dnode at %p\n", ret));
  104. return ret;
  105. }
  106. void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
  107. {
  108. D3 (printk (KERN_DEBUG "free full_dnode at %p\n", x));
  109. kmem_cache_free(full_dnode_slab, x);
  110. }
  111. struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
  112. {
  113. struct jffs2_raw_dirent *ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
  114. D3 (printk (KERN_DEBUG "alloc_raw_dirent\n", ret));
  115. return ret;
  116. }
  117. void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
  118. {
  119. D3 (printk (KERN_DEBUG "free_raw_dirent at %p\n", x));
  120. kmem_cache_free(raw_dirent_slab, x);
  121. }
  122. struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
  123. {
  124. struct jffs2_raw_inode *ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
  125. D3 (printk (KERN_DEBUG "alloc_raw_inode at %p\n", ret));
  126. return ret;
  127. }
  128. void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
  129. {
  130. D3 (printk (KERN_DEBUG "free_raw_inode at %p\n", x));
  131. kmem_cache_free(raw_inode_slab, x);
  132. }
  133. struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
  134. {
  135. struct jffs2_tmp_dnode_info *ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
  136. D3 (printk (KERN_DEBUG "alloc_tmp_dnode_info at %p\n", ret));
  137. return ret;
  138. }
  139. void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
  140. {
  141. D3 (printk (KERN_DEBUG "free_tmp_dnode_info at %p\n", x));
  142. kmem_cache_free(tmp_dnode_info_slab, x);
  143. }
  144. struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void)
  145. {
  146. struct jffs2_raw_node_ref *ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
  147. D3 (printk (KERN_DEBUG "alloc_raw_node_ref at %p\n", ret));
  148. return ret;
  149. }
  150. void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x)
  151. {
  152. D3 (printk (KERN_DEBUG "free_raw_node_ref at %p\n", x));
  153. kmem_cache_free(raw_node_ref_slab, x);
  154. }
  155. struct jffs2_node_frag *jffs2_alloc_node_frag(void)
  156. {
  157. struct jffs2_node_frag *ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
  158. D3 (printk (KERN_DEBUG "alloc_node_frag at %p\n", ret));
  159. return ret;
  160. }
  161. void jffs2_free_node_frag(struct jffs2_node_frag *x)
  162. {
  163. D3 (printk (KERN_DEBUG "free_node_frag at %p\n", x));
  164. kmem_cache_free(node_frag_slab, x);
  165. }
  166. struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
  167. {
  168. struct jffs2_inode_cache *ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
  169. D3 (printk(KERN_DEBUG "Allocated inocache at %p\n", ret));
  170. return ret;
  171. }
  172. void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
  173. {
  174. D3 (printk(KERN_DEBUG "Freeing inocache at %p\n", x));
  175. kmem_cache_free(inode_cache_slab, x);
  176. }