allocator.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //========================================================================
  2. // Custom heap allocator test
  3. // Copyright (c) Camilla Löwy <[email protected]>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would
  16. // be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. // be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. //========================================================================
  25. #define GLAD_GL_IMPLEMENTATION
  26. #include <glad/gl.h>
  27. #define GLFW_INCLUDE_NONE
  28. #include <GLFW/glfw3.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <assert.h>
  32. #define CALL(x) (function_name = #x, x)
  33. static const char* function_name = NULL;
  34. struct allocator_stats
  35. {
  36. size_t total;
  37. size_t current;
  38. size_t maximum;
  39. };
  40. static void error_callback(int error, const char* description)
  41. {
  42. fprintf(stderr, "Error: %s\n", description);
  43. }
  44. static void* allocate(size_t size, void* user)
  45. {
  46. struct allocator_stats* stats = user;
  47. assert(size > 0);
  48. stats->total += size;
  49. stats->current += size;
  50. if (stats->current > stats->maximum)
  51. stats->maximum = stats->current;
  52. printf("%s: allocate %zu bytes (current %zu maximum %zu total %zu)\n",
  53. function_name, size, stats->current, stats->maximum, stats->total);
  54. size_t* real_block = malloc(size + sizeof(size_t));
  55. assert(real_block != NULL);
  56. *real_block = size;
  57. return real_block + 1;
  58. }
  59. static void deallocate(void* block, void* user)
  60. {
  61. struct allocator_stats* stats = user;
  62. assert(block != NULL);
  63. size_t* real_block = (size_t*) block - 1;
  64. stats->current -= *real_block;
  65. printf("%s: deallocate %zu bytes (current %zu maximum %zu total %zu)\n",
  66. function_name, *real_block, stats->current, stats->maximum, stats->total);
  67. free(real_block);
  68. }
  69. static void* reallocate(void* block, size_t size, void* user)
  70. {
  71. struct allocator_stats* stats = user;
  72. assert(block != NULL);
  73. assert(size > 0);
  74. size_t* real_block = (size_t*) block - 1;
  75. stats->total += size;
  76. stats->current += size - *real_block;
  77. if (stats->current > stats->maximum)
  78. stats->maximum = stats->current;
  79. printf("%s: reallocate %zu bytes to %zu bytes (current %zu maximum %zu total %zu)\n",
  80. function_name, *real_block, size, stats->current, stats->maximum, stats->total);
  81. real_block = realloc(real_block, size + sizeof(size_t));
  82. assert(real_block != NULL);
  83. *real_block = size;
  84. return real_block + 1;
  85. }
  86. int main(void)
  87. {
  88. struct allocator_stats stats = {0};
  89. const GLFWallocator allocator =
  90. {
  91. .allocate = allocate,
  92. .deallocate = deallocate,
  93. .reallocate = reallocate,
  94. .user = &stats
  95. };
  96. glfwSetErrorCallback(error_callback);
  97. glfwInitAllocator(&allocator);
  98. if (!CALL(glfwInit)())
  99. exit(EXIT_FAILURE);
  100. GLFWwindow* window = CALL(glfwCreateWindow)(400, 400, "Custom allocator test", NULL, NULL);
  101. if (!window)
  102. {
  103. glfwTerminate();
  104. exit(EXIT_FAILURE);
  105. }
  106. CALL(glfwMakeContextCurrent)(window);
  107. gladLoadGL(glfwGetProcAddress);
  108. CALL(glfwSwapInterval)(1);
  109. while (!CALL(glfwWindowShouldClose)(window))
  110. {
  111. glClear(GL_COLOR_BUFFER_BIT);
  112. CALL(glfwSwapBuffers)(window);
  113. CALL(glfwWaitEvents)();
  114. }
  115. CALL(glfwTerminate)();
  116. exit(EXIT_SUCCESS);
  117. }