| 1267 | } |
| 1268 | |
| 1269 | size_t IOBuf::append_to(IOBuf* buf, size_t n, size_t pos) const { |
| 1270 | const size_t nref = _ref_num(); |
| 1271 | // Skip `pos' bytes. `offset' is the starting position in starting BlockRef. |
| 1272 | size_t offset = pos; |
| 1273 | size_t i = 0; |
| 1274 | for (; offset != 0 && i < nref; ++i) { |
| 1275 | IOBuf::BlockRef const& r = _ref_at(i); |
| 1276 | if (offset < (size_t)r.length) { |
| 1277 | break; |
| 1278 | } |
| 1279 | offset -= r.length; |
| 1280 | } |
| 1281 | size_t m = n; |
| 1282 | for (; m != 0 && i < nref; ++i) { |
| 1283 | IOBuf::BlockRef const& r = _ref_at(i); |
| 1284 | const size_t nc = std::min(m, (size_t)r.length - offset); |
| 1285 | const IOBuf::BlockRef r2 = { (uint32_t)(r.offset + offset), |
| 1286 | (uint32_t)nc, r.block }; |
| 1287 | buf->_push_back_ref(r2); |
| 1288 | offset = 0; |
| 1289 | m -= nc; |
| 1290 | } |
| 1291 | // If nref == 0, here returns 0 correctly |
| 1292 | return n - m; |
| 1293 | } |
| 1294 | |
| 1295 | size_t IOBuf::copy_to(void* d, size_t n, size_t pos) const { |
| 1296 | const size_t nref = _ref_num(); |