2 * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
6 #include <io_requests.h>
11 // #pragma mark - static helpers
15 transfer_io_request_data(io_request
* request
, void* buffer
, size_t size
,
18 if (writeToRequest
== request
->IsWrite()
19 || request
->RemainingBytes() < size
) {
23 // lock the request buffer memory, if it is user memory
24 IOBuffer
* ioBuffer
= request
->Buffer();
25 if (ioBuffer
->IsUser() && !ioBuffer
->IsMemoryLocked()) {
26 status_t error
= ioBuffer
->LockMemory(request
->TeamID(),
33 off_t offset
= request
->Offset() + request
->TransferredBytes();
34 status_t error
= writeToRequest
35 ? request
->CopyData(buffer
, offset
, size
)
36 : request
->CopyData(offset
, buffer
, size
);
40 request
->Advance(size
);
45 // #pragma mark - public API
48 /*! Returns whether the given I/O request is a write request. */
50 io_request_is_write(const io_request
* request
)
52 return request
->IsWrite();
56 /*! Returns whether the I/O request has VIP status. */
58 io_request_is_vip(const io_request
* request
)
60 return (request
->Flags() & B_VIP_IO_REQUEST
) != 0;
64 /*! Returns the read/write offset of the given I/O request.
65 This is the immutable offset the request was created with;
66 read_from_io_request() and write_to_io_request() don't change it.
69 io_request_offset(const io_request
* request
)
71 return request
->Offset();
75 /*! Returns the read/write length of the given I/O request.
76 This is the immutable length the request was created with;
77 read_from_io_request() and write_to_io_request() don't change it.
80 io_request_length(const io_request
* request
)
82 return request
->Length();
86 /*! Reads data from the given I/O request into the given buffer and advances
87 the request's transferred data pointer.
88 Multiple calls to read_from_io_request() are allowed, but the total size
89 must not exceed io_request_length(request).
92 read_from_io_request(io_request
* request
, void* buffer
, size_t size
)
94 return transfer_io_request_data(request
, buffer
, size
, false);
98 /*! Writes data from the given buffer to the given I/O request and advances
99 the request's transferred data pointer.
100 Multiple calls to write_to_io_request() are allowed, but the total size
101 must not exceed io_request_length(request).
104 write_to_io_request(io_request
* request
, const void* buffer
, size_t size
)
106 return transfer_io_request_data(request
, (void*)buffer
, size
, true);
110 /*! Sets the given I/O request's status and notifies listeners that the request
114 notify_io_request(io_request
* request
, status_t status
)
116 request
->SetStatusAndNotify(status
);