perf.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* iksemel (XML parser for Jabber)
  2. ** Copyright (C) 2000-2003 Gurer Ozen <madcat@e-kolay.net>
  3. ** This code is free software; you can redistribute it and/or
  4. ** modify it under the terms of GNU Lesser General Public License.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #ifdef _WIN32
  10. #include <windows.h>
  11. #include <limits.h>
  12. #else
  13. #include <sys/time.h>
  14. #endif
  15. #include "iksemel.h"
  16. /* timing functions */
  17. #ifdef _WIN32
  18. static DWORD start_tv;
  19. void
  20. t_reset (void)
  21. {
  22. start_tv = GetTickCount ();
  23. }
  24. unsigned long
  25. t_elapsed (void)
  26. {
  27. DWORD end_tv;
  28. end_tv = GetTickCount ();
  29. if (end_tv < start_tv)
  30. return UINT_MAX - (start_tv - end_tv - 1);
  31. else
  32. return end_tv - start_tv;
  33. }
  34. #else
  35. static struct timeval start_tv;
  36. void
  37. t_reset (void)
  38. {
  39. gettimeofday (&start_tv, NULL);
  40. }
  41. unsigned long
  42. t_elapsed (void)
  43. {
  44. unsigned long msec;
  45. struct timeval cur_tv;
  46. gettimeofday (&cur_tv, NULL);
  47. msec = (cur_tv.tv_sec * 1000) + (cur_tv.tv_usec / 1000);
  48. msec -= (start_tv.tv_sec * 1000) + (start_tv.tv_usec / 1000);
  49. return msec;
  50. }
  51. #endif
  52. /* memory functions */
  53. static void *
  54. m_malloc (size_t size)
  55. {
  56. void *ptr = malloc (size);
  57. printf ("MEM: malloc (%d) => %p\n", size, ptr);
  58. return ptr;
  59. }
  60. static void
  61. m_free (void *ptr)
  62. {
  63. printf ("MEM: free (%p)\n", ptr);
  64. }
  65. void
  66. m_trace (void)
  67. {
  68. iks_set_mem_funcs (m_malloc, m_free);
  69. }