You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: alloc.c,v 1.1 2004/10/29 09:28:25 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003-2004 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ferhatayaz@yahoo.com
  23. ******************************************************************/
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "alloc.h"
  27. #undef malloc
  28. #undef free
  29. typedef struct _alloc_node
  30. {
  31. char func[150];
  32. char file[150];
  33. int line;
  34. int size;
  35. void* pointer;
  36. struct _alloc_node *next;
  37. }alloc_node_t;
  38. static alloc_node_t *alloc_first = NULL;
  39. static alloc_node_t *alloc_last = NULL;
  40. void* _mem_alloc(const char* func, const char* file, int line, size_t size)
  41. {
  42. int i = strlen(file)-1;
  43. alloc_node_t *node = (alloc_node_t*)malloc(sizeof(alloc_node_t));
  44. strcpy(node->func, func);
  45. while (i>0 && file[i]!='\\')i--;
  46. strcpy(node->file, (file[i]!='\\')?file:(file+i+1));
  47. node->line = line;
  48. node->size = size;
  49. node->pointer = malloc(size);
  50. node->next = NULL;
  51. if (alloc_first == NULL)
  52. alloc_first = alloc_last = node;
  53. else {
  54. alloc_last->next = node;
  55. alloc_last = node;
  56. }
  57. return node->pointer;
  58. }
  59. void _mem_free(void *p)
  60. {
  61. alloc_node_t *node = alloc_first;
  62. while (node) {
  63. if (node->pointer == p) {
  64. free(p);
  65. node->pointer = NULL;
  66. return;
  67. }
  68. node = node->next;
  69. }
  70. }
  71. void _mem_report()
  72. {
  73. alloc_node_t *tmp, *node = alloc_first;
  74. printf("****************** String Report **********************\n");
  75. while (node) {
  76. if (node->pointer) {
  77. fprintf(stdout, "[%s:%d] \t%s()\t%d bytes\n",
  78. node->file, node->line, node->func, node->size);
  79. }
  80. tmp = node->next;
  81. free(node);
  82. node = tmp;
  83. }
  84. printf("****************** End Report **********************\n");
  85. }