MallocAllocator_test.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #define ALLOCATION_SIZE sizeof(struct MallocAllocator_Allocation)
  22. #define ALLOCATOR_SIZE sizeof(struct MallocAllocator_FirstCtx)
  23. int main()
  24. {
  25. struct Allocator* alloc = MallocAllocator_new(2048);
  26. size_t bytesUsed;
  27. Assert_always((bytesUsed = MallocAllocator_bytesAllocated(alloc)) == 0);
  28. Allocator_malloc(alloc, 25);
  29. bytesUsed += 25 + ALLOCATION_SIZE;
  30. Assert_always(MallocAllocator_bytesAllocated(alloc) == bytesUsed);
  31. struct Allocator* child = Allocator_child(alloc);
  32. bytesUsed += ALLOCATION_SIZE + ALLOCATOR_SIZE;
  33. Assert_always(MallocAllocator_bytesAllocated(alloc) == bytesUsed);
  34. Allocator_malloc(child, 30);
  35. bytesUsed += 30 + ALLOCATION_SIZE;
  36. Assert_always(MallocAllocator_bytesAllocated(alloc) == bytesUsed);
  37. Allocator_free(child);
  38. bytesUsed -= 30 + ALLOCATION_SIZE;
  39. bytesUsed -= ALLOCATION_SIZE + ALLOCATOR_SIZE;
  40. Assert_always(MallocAllocator_bytesAllocated(alloc) == bytesUsed);
  41. }