1
0

Allocator_test.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Assert.h"
  16. #include "util/platform/libc/string.h"
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include "memory/Allocator.h"
  20. #include "memory/Allocator_pvt.h"
  21. #include "memory/MallocAllocator.h"
  22. #ifdef Allocator_USE_CANARIES
  23. #define ALLOCATION_SIZE sizeof(struct Allocator_Allocation_pvt) + sizeof(long)
  24. #else
  25. #define ALLOCATION_SIZE sizeof(struct Allocator_Allocation_pvt)
  26. #endif
  27. #define ALLOCATOR_SIZE sizeof(struct Allocator_pvt)
  28. static int increment(int* num)
  29. {
  30. (*num)++;
  31. return 1;
  32. }
  33. struct TestStruct {
  34. int value;
  35. };
  36. static void allocatorClone()
  37. {
  38. struct Allocator* alloc = MallocAllocator_new(2048);
  39. int calls = 0;
  40. struct TestStruct* ts = Allocator_clone(alloc, (&(struct TestStruct) {
  41. .value = increment(&calls)
  42. }));
  43. Assert_true(calls == 1);
  44. Assert_true(ts->value == 1);
  45. Allocator_free(alloc);
  46. }
  47. static void structureSizes()
  48. {
  49. struct Allocator* alloc = MallocAllocator_new(2048);
  50. size_t bytesUsed;
  51. bytesUsed = Allocator_bytesAllocated(alloc);
  52. Assert_true(bytesUsed == ALLOCATION_SIZE + sizeof(struct Allocator_FirstCtx));
  53. Allocator_malloc(alloc, 25);
  54. bytesUsed += (((25 / sizeof(char*)) + 1) * sizeof(char*)) + ALLOCATION_SIZE;
  55. Assert_true(Allocator_bytesAllocated(alloc) == bytesUsed);
  56. struct Allocator* child = Allocator_child(alloc);
  57. bytesUsed += ALLOCATION_SIZE + ALLOCATOR_SIZE;
  58. Assert_true(Allocator_bytesAllocated(alloc) == bytesUsed);
  59. Allocator_malloc(child, 30);
  60. bytesUsed += 32 + ALLOCATION_SIZE;
  61. Assert_true(Allocator_bytesAllocated(alloc) == bytesUsed);
  62. Allocator_free(child);
  63. bytesUsed -= 32 + ALLOCATION_SIZE;
  64. bytesUsed -= ALLOCATION_SIZE + ALLOCATOR_SIZE;
  65. Assert_true(Allocator_bytesAllocated(alloc) == bytesUsed);
  66. Allocator_free(alloc);
  67. }
  68. int main()
  69. {
  70. allocatorClone();
  71. structureSizes();
  72. return 0;
  73. }