Research portable Memory game | Исследовать портируемую игру Память
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.

123 lines
2.7KB

  1. #include "uthash.h"
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. /* Set up macros for alternative malloc/free functions */
  7. #undef uthash_malloc
  8. #undef uthash_free
  9. #undef uthash_strlen
  10. #undef uthash_bzero
  11. #define uthash_malloc(sz) alt_malloc(sz)
  12. #define uthash_free(ptr,sz) alt_free(ptr,sz)
  13. #define uthash_strlen(s) ..fail_to_compile..
  14. #define uthash_bzero(a,n) alt_bzero(a,n)
  15. #undef HASH_KEYCMP
  16. #define HASH_KEYCMP(a,b,n) alt_keycmp(a,b,n)
  17. typedef struct example_user_t {
  18. int id;
  19. int cookie;
  20. UT_hash_handle hh;
  21. } example_user_t;
  22. static size_t alt_malloc_sizes[10];
  23. static int alt_malloc_balance = 0;
  24. static void *alt_malloc(size_t sz)
  25. {
  26. alt_malloc_sizes[alt_malloc_balance++] = sz;
  27. if (alt_malloc_balance == 1) {
  28. assert(sz == sizeof(UT_hash_table));
  29. }
  30. return malloc(sz);
  31. }
  32. static void alt_free(void *ptr, size_t sz)
  33. {
  34. size_t expected = alt_malloc_sizes[--alt_malloc_balance];
  35. if (sz != expected) {
  36. printf("expected free of size %d, got %d\n", (int)expected, (int)sz);
  37. }
  38. free(ptr);
  39. }
  40. static int alt_keycmp_count = 0;
  41. static int alt_keycmp(const void *a, const void *b, size_t n)
  42. {
  43. ++alt_keycmp_count;
  44. return memcmp(a,b,n);
  45. }
  46. static int alt_bzero_count = 0;
  47. static void alt_bzero(void *a, size_t n)
  48. {
  49. ++alt_bzero_count;
  50. memset(a,0,n);
  51. }
  52. static void *real_malloc(size_t n)
  53. {
  54. return malloc(n);
  55. }
  56. static void real_free(void *p)
  57. {
  58. free(p);
  59. }
  60. #undef malloc
  61. #undef realloc
  62. #undef free
  63. #undef memset
  64. #undef memcmp
  65. #undef strlen
  66. #define malloc ..fail_to_compile..
  67. #define realloc ..fail_to_compile..
  68. #define free ..fail_to_compile..
  69. #define memset ..fail_to_compile..
  70. #define memcmp ..fail_to_compile..
  71. #define strlen ..fail_to_compile..
  72. int main()
  73. {
  74. int i;
  75. example_user_t *user, *tmp, *users=NULL;
  76. /* create elements */
  77. for(i=0; i<10; i++) {
  78. user = (example_user_t*)real_malloc(sizeof(example_user_t));
  79. if (user == NULL) {
  80. exit(-1);
  81. }
  82. user->id = i;
  83. user->cookie = i*i;
  84. HASH_ADD_INT(users,id,user);
  85. }
  86. /* delete each ID */
  87. for(i=0; i<10; i++) {
  88. HASH_FIND_INT(users,&i,tmp);
  89. if (tmp != NULL) {
  90. HASH_DEL(users,tmp);
  91. real_free(tmp);
  92. } else {
  93. printf("user id %d not found\n", i);
  94. }
  95. }
  96. /* show the hash */
  97. for(user=users; user != NULL; user=(example_user_t*)(user->hh.next)) {
  98. printf("user %d, cookie %d\n", user->id, user->cookie);
  99. }
  100. #ifdef HASH_BLOOM
  101. assert(alt_bzero_count == 3);
  102. #else
  103. assert(alt_bzero_count == 2);
  104. #endif
  105. assert(alt_keycmp_count == 10);
  106. assert(alt_malloc_balance == 0);
  107. return 0;
  108. }