MMBuffer.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2018 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef MMKV_MMBUFFER_H
  21. #define MMKV_MMBUFFER_H
  22. #ifdef __cplusplus
  23. #include "MMKVPredef.h"
  24. #include <cstdint>
  25. #include <cstdlib>
  26. #include <cstddef>
  27. namespace mmkv {
  28. enum MMBufferCopyFlag : bool {
  29. MMBufferCopy = false,
  30. MMBufferNoCopy = true,
  31. };
  32. #pragma pack(push, 1)
  33. #ifndef MMKV_DISABLE_CRYPT
  34. struct KeyValueHolderCrypt;
  35. #endif
  36. class MMBuffer {
  37. enum MMBufferType : uint8_t {
  38. MMBufferType_Small, // store small buffer in stack memory
  39. MMBufferType_Normal, // store in heap memory
  40. };
  41. MMBufferType type;
  42. union {
  43. struct {
  44. MMBufferCopyFlag isNoCopy;
  45. size_t size;
  46. void *ptr;
  47. #ifdef MMKV_APPLE
  48. NSData *m_data;
  49. #endif
  50. };
  51. struct {
  52. uint8_t paddedSize;
  53. // make at least 10 bytes to hold all primitive types (negative int32, int64, double etc) on 32 bit device
  54. // on 64 bit device it's guaranteed larger than 10 bytes
  55. uint8_t paddedBuffer[10];
  56. };
  57. };
  58. static constexpr size_t SmallBufferSize() {
  59. return sizeof(MMBuffer) - offsetof(MMBuffer, paddedBuffer);
  60. }
  61. public:
  62. explicit MMBuffer(size_t length = 0);
  63. MMBuffer(void *source, size_t length, MMBufferCopyFlag flag = MMBufferCopy);
  64. #ifdef MMKV_APPLE
  65. explicit MMBuffer(NSData *data, MMBufferCopyFlag flag = MMBufferCopy);
  66. #endif
  67. MMBuffer(MMBuffer &&other) noexcept;
  68. MMBuffer(MMBuffer &&other, size_t length) noexcept;
  69. MMBuffer &operator=(MMBuffer &&other) noexcept;
  70. ~MMBuffer();
  71. bool isStoredOnStack() const { return (type == MMBufferType_Small); }
  72. void *getPtr() const { return isStoredOnStack() ? (void *) paddedBuffer : ptr; }
  73. size_t length() const { return isStoredOnStack() ? paddedSize : size; }
  74. // transfer ownership to others
  75. void detach();
  76. // those are expensive, just forbid it for possibly misuse
  77. explicit MMBuffer(const MMBuffer &other) = delete;
  78. MMBuffer &operator=(const MMBuffer &other) = delete;
  79. #ifndef MMKV_DISABLE_CRYPT
  80. friend KeyValueHolderCrypt;
  81. #endif
  82. };
  83. #pragma pack(pop)
  84. } // namespace mmkv
  85. #endif
  86. #endif //MMKV_MMBUFFER_H