MallocAllocator_test.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/MallocAllocator_pvt.h"
  21. #ifdef MallocAllocator_USE_CANARIES
  22. #define ALLOCATION_SIZE sizeof(struct MallocAllocator_Allocation) + sizeof(long)
  23. #else
  24. #define ALLOCATION_SIZE sizeof(struct MallocAllocator_Allocation)
  25. #endif
  26. #define ALLOCATOR_SIZE sizeof(struct MallocAllocator_pvt)
  27. int main()
  28. {
  29. struct Allocator* alloc = MallocAllocator_new(2048);
  30. size_t bytesUsed;
  31. bytesUsed = MallocAllocator_bytesAllocated(alloc);
  32. Assert_always(bytesUsed == ALLOCATION_SIZE + sizeof(struct MallocAllocator_FirstCtx));
  33. Allocator_malloc(alloc, 25);
  34. bytesUsed += (((25 / sizeof(char*)) + 1) * sizeof(char*)) + ALLOCATION_SIZE;
  35. Assert_always(MallocAllocator_bytesAllocated(alloc) == bytesUsed);
  36. struct Allocator* child = Allocator_child(alloc);
  37. bytesUsed += ALLOCATION_SIZE + ALLOCATOR_SIZE;
  38. Assert_always(MallocAllocator_bytesAllocated(alloc) == bytesUsed);
  39. Allocator_malloc(child, 30);
  40. bytesUsed += 32 + ALLOCATION_SIZE;
  41. Assert_always(MallocAllocator_bytesAllocated(alloc) == bytesUsed);
  42. Allocator_free(child);
  43. bytesUsed -= 32 + ALLOCATION_SIZE;
  44. bytesUsed -= ALLOCATION_SIZE + ALLOCATOR_SIZE;
  45. Assert_always(MallocAllocator_bytesAllocated(alloc) == bytesUsed);
  46. Allocator_free(alloc);
  47. }