util_vector.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_VECTOR_H__
  17. #define __UTIL_VECTOR_H__
  18. #include <cassert>
  19. #include <cstring>
  20. #include <vector>
  21. #include "util/util_aligned_malloc.h"
  22. #include "util/util_guarded_allocator.h"
  23. #include "util/util_types.h"
  24. CCL_NAMESPACE_BEGIN
  25. /* Own subclass-ed vestion of std::vector. Subclass is needed because:
  26. *
  27. * - Use own allocator which keeps track of used/peak memory.
  28. * - Have method to ensure capacity is re-set to 0.
  29. */
  30. template<typename value_type, typename allocator_type = GuardedAllocator<value_type>>
  31. class vector : public std::vector<value_type, allocator_type> {
  32. public:
  33. typedef std::vector<value_type, allocator_type> BaseClass;
  34. /* Inherit all constructors from base class. */
  35. using BaseClass::vector;
  36. /* Try as hard as possible to use zero memory. */
  37. void free_memory()
  38. {
  39. BaseClass::resize(0);
  40. BaseClass::shrink_to_fit();
  41. }
  42. /* Some external API might demand working with std::vector. */
  43. operator std::vector<value_type>()
  44. {
  45. return std::vector<value_type>(this->begin(), this->end());
  46. }
  47. };
  48. CCL_NAMESPACE_END
  49. #endif /* __UTIL_VECTOR_H__ */