1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares a utility class for mapping slices of shared files (buffers, from
16 : // the point of view of the call-trace service) into memory.
17 :
18 : #ifndef SYZYGY_TRACE_SERVICE_MAPPED_BUFFER_H_
19 : #define SYZYGY_TRACE_SERVICE_MAPPED_BUFFER_H_
20 :
21 : #include <stdint.h>
22 :
23 : #include "base/logging.h"
24 :
25 : namespace trace {
26 : namespace service {
27 :
28 : // Forward declare.
29 : struct Buffer;
30 :
31 : // A utility class for creating a scoped on demand memory mapped view into a
32 : // shared file. Automatically unmaps the buffer when destroyed.
33 : class MappedBuffer {
34 : public:
35 : // Constructor.
36 : // @param buffer The buffer to be mapped.
37 E : explicit MappedBuffer(Buffer* buffer)
38 E : : buffer_(buffer), base_(NULL), data_(NULL) {
39 E : DCHECK(buffer != NULL);
40 E : }
41 :
42 E : ~MappedBuffer() {
43 E : Unmap();
44 E : }
45 :
46 : // Maps the current buffer. Logs an error message on failure.
47 : // @returns true on success, false otherwise.
48 : bool Map();
49 :
50 : // Unmaps the current buffer. Logs an error message on failure.
51 : // @returns true on success, false otherwise.
52 : bool Unmap();
53 :
54 : // @returns true if the buffer is mapped, false otherwise.
55 E : bool IsMapped() const { return data_ != NULL; }
56 :
57 : // Returns a pointer to the mapped buffer data.
58 E : uint8_t* data() const { return data_; }
59 :
60 : protected:
61 : Buffer* buffer_;
62 : uint8_t* base_;
63 : uint8_t* data_;
64 : };
65 :
66 : } // namespace service
67 : } // namespace trace
68 :
69 : #endif // SYZYGY_TRACE_SERVICE_MAPPED_BUFFER_H_
|