linux.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/poison.h>
  8. #include <linux/slab.h>
  9. #include <linux/radix-tree.h>
  10. #include <urcu/uatomic.h>
  11. int nr_allocated;
  12. int preempt_count;
  13. struct kmem_cache {
  14. pthread_mutex_t lock;
  15. int size;
  16. int nr_objs;
  17. void *objs;
  18. void (*ctor)(void *);
  19. };
  20. void *kmem_cache_alloc(struct kmem_cache *cachep, int flags)
  21. {
  22. struct radix_tree_node *node;
  23. if (flags & __GFP_NOWARN)
  24. return NULL;
  25. pthread_mutex_lock(&cachep->lock);
  26. if (cachep->nr_objs) {
  27. cachep->nr_objs--;
  28. node = cachep->objs;
  29. cachep->objs = node->private_data;
  30. pthread_mutex_unlock(&cachep->lock);
  31. node->private_data = NULL;
  32. } else {
  33. pthread_mutex_unlock(&cachep->lock);
  34. node = malloc(cachep->size);
  35. if (cachep->ctor)
  36. cachep->ctor(node);
  37. }
  38. uatomic_inc(&nr_allocated);
  39. return node;
  40. }
  41. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  42. {
  43. assert(objp);
  44. uatomic_dec(&nr_allocated);
  45. pthread_mutex_lock(&cachep->lock);
  46. if (cachep->nr_objs > 10) {
  47. memset(objp, POISON_FREE, cachep->size);
  48. free(objp);
  49. } else {
  50. struct radix_tree_node *node = objp;
  51. cachep->nr_objs++;
  52. node->private_data = cachep->objs;
  53. cachep->objs = node;
  54. }
  55. pthread_mutex_unlock(&cachep->lock);
  56. }
  57. void *kmalloc(size_t size, gfp_t gfp)
  58. {
  59. void *ret = malloc(size);
  60. uatomic_inc(&nr_allocated);
  61. return ret;
  62. }
  63. void kfree(void *p)
  64. {
  65. if (!p)
  66. return;
  67. uatomic_dec(&nr_allocated);
  68. free(p);
  69. }
  70. struct kmem_cache *
  71. kmem_cache_create(const char *name, size_t size, size_t offset,
  72. unsigned long flags, void (*ctor)(void *))
  73. {
  74. struct kmem_cache *ret = malloc(sizeof(*ret));
  75. pthread_mutex_init(&ret->lock, NULL);
  76. ret->size = size;
  77. ret->nr_objs = 0;
  78. ret->objs = NULL;
  79. ret->ctor = ctor;
  80. return ret;
  81. }