util_stats.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __UTIL_STATS_H__
  17. #define __UTIL_STATS_H__
  18. #include "util/util_atomic.h"
  19. #include "util/util_profiling.h"
  20. CCL_NAMESPACE_BEGIN
  21. class Stats {
  22. public:
  23. enum static_init_t { static_init = 0 };
  24. Stats() : mem_used(0), mem_peak(0)
  25. {
  26. }
  27. explicit Stats(static_init_t)
  28. {
  29. }
  30. void mem_alloc(size_t size)
  31. {
  32. atomic_add_and_fetch_z(&mem_used, size);
  33. atomic_fetch_and_update_max_z(&mem_peak, mem_used);
  34. }
  35. void mem_free(size_t size)
  36. {
  37. assert(mem_used >= size);
  38. atomic_sub_and_fetch_z(&mem_used, size);
  39. }
  40. size_t mem_used;
  41. size_t mem_peak;
  42. };
  43. CCL_NAMESPACE_END
  44. #endif /* __UTIL_STATS_H__ */