You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

system_pool.h 2.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  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 MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SYSTEM_POOL_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SYSTEM_POOL_H_
  18. #include <cstddef>
  19. #include <cstdlib>
  20. #include <limits>
  21. #include <memory>
  22. #include <new>
  23. #include "./securec.h"
  24. #include "minddata/dataset/util/allocator.h"
  25. #include "minddata/dataset/util/memory_pool.h"
  26. namespace mindspore {
  27. namespace dataset {
  28. // This class demonstrate how to implement a simple MemoryPool
  29. // for minddata/dataset using malloc/free/realloc. We need to
  30. // implement 4 virtual functions. Other MemoryPool
  31. // implementation, e.g., are BuddyArena and CircularPool. All
  32. // these MemoryPool can be used together with Allocator.h for
  33. // C++ STL containers.
  34. class SystemPool : public MemoryPool {
  35. public:
  36. ~SystemPool() override {}
  37. Status Allocate(size_t n, void **pp) override { return DeMalloc(n, pp, false); }
  38. void Deallocate(void *p) override { free(p); }
  39. Status Reallocate(void **p, size_t old_sz, size_t new_sz) override {
  40. if (old_sz >= new_sz) {
  41. // Do nothing if we shrink.
  42. return Status::OK();
  43. } else {
  44. void *ptr = *p;
  45. void *q = nullptr;
  46. RETURN_IF_NOT_OK(DeMalloc(new_sz, &q, false));
  47. errno_t err = memcpy_s(q, new_sz, ptr, old_sz);
  48. if (err) {
  49. free(q);
  50. RETURN_STATUS_UNEXPECTED(std::to_string(err));
  51. }
  52. free(ptr);
  53. *p = q;
  54. return Status::OK();
  55. }
  56. }
  57. uint64_t get_max_size() const override { return std::numeric_limits<uint64_t>::max(); }
  58. int PercentFree() const override { return 100; }
  59. template <typename T>
  60. static Allocator<T> GetAllocator() {
  61. return Allocator<T>(std::make_shared<SystemPool>());
  62. }
  63. };
  64. } // namespace dataset
  65. } // namespace mindspore
  66. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SYSTEM_POOL_H_