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.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 {
  39. if (p != nullptr) {
  40. free(p);
  41. }
  42. }
  43. Status Reallocate(void **p, size_t old_sz, size_t new_sz) override {
  44. RETURN_UNEXPECTED_IF_NULL(p);
  45. if (old_sz >= new_sz) {
  46. // Do nothing if we shrink.
  47. return Status::OK();
  48. } else {
  49. void *ptr = *p;
  50. void *q = nullptr;
  51. RETURN_IF_NOT_OK(DeMalloc(new_sz, &q, false));
  52. errno_t err = memcpy_s(q, new_sz, ptr, old_sz);
  53. if (err) {
  54. free(q);
  55. RETURN_STATUS_UNEXPECTED(std::to_string(err));
  56. }
  57. free(ptr);
  58. *p = q;
  59. return Status::OK();
  60. }
  61. }
  62. uint64_t get_max_size() const override { return std::numeric_limits<uint64_t>::max(); }
  63. int PercentFree() const override { return 100; }
  64. template <typename T>
  65. static Allocator<T> GetAllocator() {
  66. return Allocator<T>(std::make_shared<SystemPool>());
  67. }
  68. };
  69. } // namespace dataset
  70. } // namespace mindspore
  71. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SYSTEM_POOL_H_