| 149 | } |
| 150 | |
| 151 | int BlockDeviceToMemoryTechnologyDevice::program(const uint64_t address, const void* const buffer, const size_t size) |
| 152 | { |
| 153 | const std::lock_guard<BlockDeviceToMemoryTechnologyDevice> lockGuard {*this}; |
| 154 | |
| 155 | assert(openCount_ != 0); |
| 156 | assert(buffer != nullptr); |
| 157 | |
| 158 | const auto blockSize = blockDevice_.getBlockSize(); |
| 159 | assert(address % blockSize == 0 && size % blockSize == 0); |
| 160 | assert(address + size <= blockDevice_.getSize()); |
| 161 | |
| 162 | if (size == 0) |
| 163 | return {}; |
| 164 | |
| 165 | if (pendingEraseSize_ != 0 && address > pendingEraseAddress_ && |
| 166 | address + size < pendingEraseAddress_ + pendingEraseSize_) |
| 167 | { |
| 168 | // Data should be written in the middle of the pending erase range, so the erase range would have to be split in |
| 169 | // two separate parts, which is not possible. Flush part of the pending erase which is below the written data, |
| 170 | // based on assumption that the part above is more likely to be written soon. |
| 171 | |
| 172 | const auto eraseSize = address - pendingEraseAddress_; |
| 173 | const auto ret = blockDevice_.erase(pendingEraseAddress_, eraseSize); |
| 174 | if (ret != 0) |
| 175 | return ret; |
| 176 | |
| 177 | pendingEraseAddress_ += eraseSize; |
| 178 | pendingEraseSize_ -= eraseSize; |
| 179 | } |
| 180 | |
| 181 | const auto ret = blockDevice_.write(address, buffer, size); |
| 182 | if (ret != 0) |
| 183 | return ret; |
| 184 | |
| 185 | const auto overlapBegin = std::max(address, pendingEraseAddress_); |
| 186 | const auto overlapEnd = std::min(address + size, pendingEraseAddress_ + pendingEraseSize_); |
| 187 | if (overlapBegin < overlapEnd) |
| 188 | { |
| 189 | if (pendingEraseAddress_ == overlapBegin) |
| 190 | pendingEraseAddress_ = overlapEnd; |
| 191 | pendingEraseSize_ -= overlapEnd - overlapBegin; |
| 192 | } |
| 193 | |
| 194 | return {}; |
| 195 | } |
| 196 | |
| 197 | int BlockDeviceToMemoryTechnologyDevice::read(const uint64_t address, void* const buffer, const size_t size) |
| 198 | { |
no test coverage detected