linux.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. #include <assert.h>
  7. #include <linux/gfp.h>
  8. #include <linux/poison.h>
  9. #include <linux/slab.h>
  10. #include <linux/radix-tree.h>
  11. #include <urcu/uatomic.h>
  12. int nr_allocated;
  13. int preempt_count;
  14. int kmalloc_verbose;
  15. int test_verbose;
  16. struct kmem_cache {
  17. pthread_mutex_t lock;
  18. int size;
  19. int nr_objs;
  20. void *objs;
  21. void (*ctor)(void *);
  22. };
  23. void *kmem_cache_alloc(struct kmem_cache *cachep, int flags)
  24. {
  25. struct radix_tree_node *node;
  26. if (flags & __GFP_NOWARN)
  27. return NULL;
  28. pthread_mutex_lock(&cachep->lock);
  29. if (cachep->nr_objs) {
  30. cachep->nr_objs--;
  31. node = cachep->objs;
  32. cachep->objs = node->parent;
  33. pthread_mutex_unlock(&cachep->lock);
  34. node->parent = NULL;
  35. } else {
  36. pthread_mutex_unlock(&cachep->lock);
  37. node = malloc(cachep->size);
  38. if (cachep->ctor)
  39. cachep->ctor(node);
  40. }
  41. uatomic_inc(&nr_allocated);
  42. if (kmalloc_verbose)
  43. printf("Allocating %p from slab\n", node);
  44. return node;
  45. }
  46. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  47. {
  48. assert(objp);
  49. uatomic_dec(&nr_allocated);
  50. if (kmalloc_verbose)
  51. printf("Freeing %p to slab\n", objp);
  52. pthread_mutex_lock(&cachep->lock);
  53. if (cachep->nr_objs > 10) {
  54. memset(objp, POISON_FREE, cachep->size);
  55. free(objp);
  56. } else {
  57. struct radix_tree_node *node = objp;
  58. cachep->nr_objs++;
  59. node->parent = cachep->objs;
  60. cachep->objs = node;
  61. }
  62. pthread_mutex_unlock(&cachep->lock);
  63. }
  64. void *kmalloc(size_t size, gfp_t gfp)
  65. {
  66. void *ret = malloc(size);
  67. uatomic_inc(&nr_allocated);
  68. if (kmalloc_verbose)
  69. printf("Allocating %p from malloc\n", ret);
  70. return ret;
  71. }
  72. void kfree(void *p)
  73. {
  74. if (!p)
  75. return;
  76. uatomic_dec(&nr_allocated);
  77. if (kmalloc_verbose)
  78. printf("Freeing %p to malloc\n", p);
  79. free(p);
  80. }
  81. struct kmem_cache *
  82. kmem_cache_create(const char *name, size_t size, size_t offset,
  83. unsigned long flags, void (*ctor)(void *))
  84. {
  85. struct kmem_cache *ret = malloc(sizeof(*ret));
  86. pthread_mutex_init(&ret->lock, NULL);
  87. ret->size = size;
  88. ret->nr_objs = 0;
  89. ret->objs = NULL;
  90. ret->ctor = ctor;
  91. return ret;
  92. }