progress.c 942 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include "../cache.h"
  4. #include "progress.h"
  5. static void null_progress__update(struct ui_progress *p __maybe_unused)
  6. {
  7. }
  8. static struct ui_progress_ops null_progress__ops =
  9. {
  10. .update = null_progress__update,
  11. };
  12. struct ui_progress_ops *ui_progress__ops = &null_progress__ops;
  13. void ui_progress__update(struct ui_progress *p, u64 adv)
  14. {
  15. u64 last = p->curr;
  16. p->curr += adv;
  17. if (p->curr >= p->next) {
  18. u64 nr = DIV_ROUND_UP(p->curr - last, p->step);
  19. p->next += nr * p->step;
  20. ui_progress__ops->update(p);
  21. }
  22. }
  23. void __ui_progress__init(struct ui_progress *p, u64 total,
  24. const char *title, bool size)
  25. {
  26. p->curr = 0;
  27. p->next = p->step = total / 16 ?: 1;
  28. p->total = total;
  29. p->title = title;
  30. p->size = size;
  31. if (ui_progress__ops->init)
  32. ui_progress__ops->init(p);
  33. }
  34. void ui_progress__finish(void)
  35. {
  36. if (ui_progress__ops->finish)
  37. ui_progress__ops->finish();
  38. }