MMBuffer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. namespace mmkv {
  27. enum MMBufferCopyFlag : bool {
  28. MMBufferCopy = false,
  29. MMBufferNoCopy = true,
  30. };
  31. #pragma pack(push, 1)
  32. #ifndef MMKV_DISABLE_CRYPT
  33. struct KeyValueHolderCrypt;
  34. #endif
  35. class MMBuffer {
  36. enum MMBufferType : uint8_t {
  37. MMBufferType_Small, // store small buffer in stack memory
  38. MMBufferType_Normal, // store in heap memory
  39. };
  40. MMBufferType type;
  41. union {
  42. struct {
  43. MMBufferCopyFlag isNoCopy;
  44. size_t size;
  45. void *ptr;
  46. #ifdef MMKV_APPLE
  47. NSData *m_data;
  48. #endif
  49. };
  50. struct {
  51. uint8_t paddedSize;
  52. // make at least 10 bytes to hold all primitive types (negative int32, int64, double etc) on 32 bit device
  53. // on 64 bit device it's guaranteed larger than 10 bytes
  54. uint8_t paddedBuffer[10];
  55. };
  56. };
  57. static constexpr size_t SmallBufferSize() {
  58. return sizeof(MMBuffer) - offsetof(MMBuffer, paddedBuffer);
  59. }
  60. public:
  61. explicit MMBuffer(size_t length = 0);
  62. MMBuffer(void *source, size_t length, MMBufferCopyFlag flag = MMBufferCopy);
  63. #ifdef MMKV_APPLE
  64. explicit MMBuffer(NSData *data, MMBufferCopyFlag flag = MMBufferCopy);
  65. #endif
  66. MMBuffer(MMBuffer &&other) noexcept;
  67. MMBuffer &operator=(MMBuffer &&other) noexcept;
  68. ~MMBuffer();
  69. bool isStoredOnStack() const { return (type == MMBufferType_Small); }
  70. void *getPtr() const { return isStoredOnStack() ? (void *) paddedBuffer : ptr; }
  71. size_t length() const { return isStoredOnStack() ? paddedSize : size; }
  72. // transfer ownership to others
  73. void detach();
  74. // those are expensive, just forbid it for possibly misuse
  75. explicit MMBuffer(const MMBuffer &other) = delete;
  76. MMBuffer &operator=(const MMBuffer &other) = delete;
  77. #ifndef MMKV_DISABLE_CRYPT
  78. friend KeyValueHolderCrypt;
  79. #endif
  80. };
  81. #pragma pack(pop)
  82. } // namespace mmkv
  83. #endif
  84. #endif //MMKV_MMBUFFER_H