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.

memory_pool.cc 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include "dataset/util/memory_pool.h"
  17. #include "./securec.h"
  18. namespace mindspore {
  19. namespace dataset {
  20. Status DeMalloc(std::size_t s, void **p, bool init_to_zero = false) {
  21. if (p == nullptr) {
  22. RETURN_STATUS_UNEXPECTED("p is null");
  23. }
  24. void *q = ::malloc(s);
  25. if (q == nullptr) {
  26. return Status(StatusCode::kOutOfMemory, __LINE__, __FILE__);
  27. } else {
  28. *p = q;
  29. if (init_to_zero) {
  30. (void)memset_s(q, s, 0, s);
  31. }
  32. return Status::OK();
  33. }
  34. }
  35. } // namespace dataset
  36. } // namespace mindspore
  37. void *operator new(std::size_t s, mindspore::dataset::Status *rc, std::shared_ptr<mindspore::dataset::MemoryPool> b) {
  38. void *ptr = nullptr;
  39. *rc = b->Allocate(s, &ptr);
  40. return ptr;
  41. }
  42. void *operator new[](std::size_t s, mindspore::dataset::Status *rc, std::shared_ptr<mindspore::dataset::MemoryPool> b) {
  43. void *ptr = nullptr;
  44. *rc = b->Allocate(s, &ptr);
  45. return ptr;
  46. }
  47. void operator delete(void *p, std::shared_ptr<mindspore::dataset::MemoryPool> b) {
  48. if (p != nullptr) b->Deallocate(p);
  49. }
  50. void operator delete[](void *p, std::shared_ptr<mindspore::dataset::MemoryPool> b) {
  51. if (p != nullptr) b->Deallocate(p);
  52. }