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.

47 lines
1.0KB

  1. /*
  2. * This file is a part of SMalloc.
  3. * SMalloc is MIT licensed.
  4. * Copyright (c) 2017 Andrey Rys.
  5. */
  6. #include "smalloc_i.h"
  7. int sm_malloc_stats_pool(struct smalloc_pool *spool, size_t *total, size_t *user, size_t *free, int *nr_blocks)
  8. {
  9. struct smalloc_hdr *shdr, *basehdr;
  10. int r = 0;
  11. if (!smalloc_verify_pool(spool)) {
  12. errno = EINVAL;
  13. return -1;
  14. }
  15. if (!total && !user && !free && !nr_blocks) return 0;
  16. if (total) *total = 0;
  17. if (user) *user = 0;
  18. if (free) *free = 0;
  19. if (nr_blocks) *nr_blocks = 0;
  20. shdr = basehdr = spool->pool;
  21. while (CHAR_PTR(shdr)-CHAR_PTR(basehdr) < spool->pool_size) {
  22. if (smalloc_is_alloc(spool, shdr)) {
  23. if (total) *total += HEADER_SZ + shdr->rsz + HEADER_SZ;
  24. if (user) *user += shdr->usz;
  25. if (nr_blocks) *nr_blocks += 1;
  26. r = 1;
  27. }
  28. shdr++;
  29. }
  30. *free = spool->pool_size - *total;
  31. return r;
  32. }
  33. int sm_malloc_stats(size_t *total, size_t *user, size_t *free, int *nr_blocks)
  34. {
  35. return sm_malloc_stats_pool(&smalloc_curr_pool, total, user, free, nr_blocks);
  36. }