/src/ftk_allocator_profile.c

http://ftk.googlecode.com/ · C · 134 lines · 84 code · 21 blank · 29 comment · 12 complexity · 0c0464c5d352862e6f742921b2c47683 MD5 · raw file

  1. /*
  2. * File: ftk_allocator.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: memory allocator interface.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2010-01-31 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk.h"
  31. #include "ftk_allocator_profile.h"
  32. typedef struct _AllocPrivInfo
  33. {
  34. int free_times;
  35. int allocated_times;
  36. int allocated;
  37. int max_allocated;
  38. int total_allocated;
  39. FtkAllocator* allocator;
  40. }PrivInfo;
  41. void* ftk_allocator_profile_alloc(FtkAllocator* thiz, size_t size)
  42. {
  43. DECL_PRIV(thiz, priv);
  44. void* ptr = ftk_allocator_alloc(priv->allocator, size + sizeof(size_t));
  45. if(ptr != NULL)
  46. {
  47. priv->allocated_times++;
  48. priv->allocated += size;
  49. priv->total_allocated += size;
  50. priv->max_allocated = priv->allocated > priv->max_allocated? priv->allocated : priv->max_allocated;
  51. *(size_t*)ptr = size;
  52. ptr = (char*)ptr + sizeof(size_t);
  53. }
  54. return ptr;
  55. }
  56. void* ftk_allocator_profile_realloc(FtkAllocator* thiz, void* ptr, size_t new_size)
  57. {
  58. size_t old_size = 0;
  59. DECL_PRIV(thiz, priv);
  60. ptr = ptr != NULL ? (char*)ptr - sizeof(size_t) : NULL;
  61. old_size = ptr != NULL ? *(size_t*)ptr : 0;
  62. ptr = ftk_allocator_realloc(priv->allocator, ptr, new_size + sizeof(size_t));
  63. if(ptr != NULL)
  64. {
  65. priv->allocated += new_size - old_size;
  66. priv->total_allocated += new_size;
  67. priv->max_allocated = priv->allocated > priv->max_allocated? priv->allocated : priv->max_allocated;
  68. *(size_t*)ptr = new_size;
  69. ptr = (char*)ptr + sizeof(size_t);
  70. }
  71. return ptr;
  72. }
  73. void ftk_allocator_profile_free(FtkAllocator* thiz, void* ptr)
  74. {
  75. size_t size = 0;
  76. DECL_PRIV(thiz, priv);
  77. ptr = ptr != NULL ? (char*)ptr - sizeof(size_t) : NULL;
  78. size = ptr != NULL ? *(size_t*)ptr : 0;
  79. if(ptr != NULL)
  80. {
  81. priv->free_times++;
  82. priv->allocated -= size;
  83. }
  84. ftk_allocator_free(priv->allocator, ptr);
  85. return;
  86. }
  87. void ftk_allocator_profile_destroy(FtkAllocator* thiz)
  88. {
  89. DECL_PRIV(thiz, priv);
  90. FtkAllocator* allocator = priv->allocator;
  91. ftk_logd("%s: allocated_times=%d\n", __func__, priv->allocated_times);
  92. ftk_logd("%s: free_times=%d\n", __func__, priv->free_times);
  93. ftk_logd("%s: allocated=%d\n", __func__, priv->allocated);
  94. ftk_logd("%s: max_allocated=%d\n", __func__, priv->max_allocated);
  95. ftk_logd("%s: total_allocated=%d\n", __func__, priv->total_allocated);
  96. ftk_allocator_free(allocator, thiz);
  97. ftk_allocator_destroy(allocator);
  98. return;
  99. }
  100. FtkAllocator* ftk_allocator_profile_create(FtkAllocator* allocator)
  101. {
  102. FtkAllocator* thiz = (FtkAllocator*)ftk_allocator_zalloc(allocator, sizeof(FtkAllocator) + sizeof(PrivInfo));
  103. if(thiz != NULL)
  104. {
  105. DECL_PRIV(thiz, priv);
  106. priv->allocator = allocator;
  107. thiz->alloc = ftk_allocator_profile_alloc;
  108. thiz->realloc = ftk_allocator_profile_realloc;
  109. thiz->free = ftk_allocator_profile_free;
  110. thiz->destroy = ftk_allocator_profile_destroy;
  111. }
  112. return thiz;
  113. }