duck_mem.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***********************************************\
  2. ??? duck_mem.c
  3. \***********************************************/
  4. #pragma warning(disable:4786)
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <dos.h>
  8. #include <time.h>
  9. #include <malloc.h>
  10. #include "duck_mem.h"
  11. #include "duck_io.h"
  12. //#include "duck_hfb.h"
  13. #include "duck_dxl.h"
  14. //#define CHECK_MEM
  15. #ifdef CHECK_MEM
  16. #include <map>
  17. #include "debug.h"
  18. #endif
  19. #ifdef CHECK_MEM
  20. struct comp
  21. {
  22. bool operator()(void* v1, void* v2) const
  23. {
  24. return v1 < v2;
  25. }
  26. };
  27. #endif
  28. #ifdef CHECK_MEM
  29. std::map<void*, size_t, struct comp> pointers;
  30. int g_allocs = 0;
  31. int g_frees = 0;
  32. #endif
  33. void *duck_memmove( void *dst, const void *src, size_t length )
  34. {
  35. return memmove(dst, src, length);
  36. }
  37. void *duck_malloc(size_t size, dmemType foo)
  38. {
  39. void *temp = malloc(size);
  40. #ifdef CHECK_MEM
  41. g_allocs++;
  42. TRACE("a=%d\t%d\n", g_allocs, (int)temp);
  43. pointers[temp] = size;
  44. #endif
  45. return temp;
  46. }
  47. void *duck_memset( void *dest, int c, size_t count )
  48. {
  49. return((void *) memset(dest, c, count));
  50. }
  51. void *duck_calloc(size_t n,size_t size, dmemType foo)
  52. {
  53. void *temp = calloc(n, size);
  54. #ifdef CHECK_MEM
  55. g_allocs++;
  56. TRACE("a=%d\t%d\n", g_allocs, (int)temp);
  57. pointers[temp] = size;
  58. #endif
  59. return temp;
  60. }
  61. void duck_free(void *old_blk)
  62. {
  63. #ifdef CHECK_MEM
  64. g_frees++;
  65. TRACE("f=%d\t%d\n", g_frees, (int)old_blk);
  66. if(!pointers.erase(old_blk))
  67. assert(0);
  68. #endif
  69. free(old_blk);
  70. }
  71. void* duck_realloc(void *src, size_t newSize, size_t oldSize)
  72. {
  73. void *temp;
  74. if(newSize <= oldSize)
  75. return src;
  76. #ifdef CHECK_MEM
  77. temp = duck_malloc(newSize, DMEM_GENERAL);
  78. duck_memcpy(temp, src, oldSize);
  79. duck_free(src);
  80. #else
  81. temp = realloc(src, newSize);
  82. #endif
  83. return temp;
  84. }
  85. void *duck_memcpy(void *dest, const void *source, size_t length)
  86. {
  87. return memcpy(dest,source,length);
  88. }
  89. void *duck_memcpy64(void *dest, const void *source, int64_t length)
  90. {
  91. /* Not fully 64 bit compliant */
  92. return memcpy(dest,source,(size_t)length);
  93. }
  94. int duck_strcmp(const char *one, const char *two)
  95. {
  96. return strcmp(one, two);
  97. }
  98. void set_memfuncs()
  99. {
  100. #if defined(DXV_DLL)
  101. DXV_Setmalloc(malloc);
  102. DXV_Setcalloc(calloc);
  103. DXV_Setfree(free);
  104. #endif
  105. #if defined(HFB_DLL)
  106. HFB_Setmalloc(malloc);
  107. HFB_Setcalloc(calloc);
  108. HFB_Setfree(free);
  109. #endif
  110. }