MCPcopy Create free account
hub / github.com/Tencent/MMKV / writeValueToBuffer

Method writeValueToBuffer

Core/MMKV.cpp:1042–1075  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1040}
1041
1042int32_t MMKV::writeValueToBuffer(MMKVKey_t key, void *ptr, int32_t size) {
1043 if (isKeyEmpty(key) || size < 0) {
1044 return -1;
1045 }
1046 auto s_size = static_cast<size_t>(size);
1047
1048 SCOPED_LOCK(m_lock);
1049 SCOPED_LOCK(m_sharedProcessLock);
1050 auto data = getDataForKey(key);
1051 try {
1052 CodedInputData input(data.getPtr(), data.length());
1053 auto length = input.readInt32();
1054 auto offset = pbRawVarint32Size(length);
1055 if (length >= 0) {
1056 auto s_length = static_cast<size_t>(length);
1057 if (offset + s_length == data.length()) {
1058 if (s_length <= s_size) {
1059 memcpy(ptr, (uint8_t *) data.getPtr() + offset, s_length);
1060 return length;
1061 }
1062 } else {
1063 if (data.length() <= s_size) {
1064 memcpy(ptr, data.getPtr(), data.length());
1065 return static_cast<int32_t>(data.length());
1066 }
1067 }
1068 }
1069 } catch (std::exception &exception) {
1070 MMKVError("%s", exception.what());
1071 } catch (...) {
1072 MMKVError("encode fail");
1073 }
1074 return -1;
1075}
1076
1077// enumerate
1078

Callers 6

writeValueToNBFunction · 0.80
writeValueToNBFunction · 0.80
writeValueToNBFunction · 0.80
writeValueToNBFunction · 0.80
writeValueToNativeBufferFunction · 0.80
writeValueToNBFunction · 0.80

Calls 3

isKeyEmptyFunction · 0.85
pbRawVarint32SizeFunction · 0.70
readInt32Method · 0.45

Tested by

no test coverage detected