SharedMemoryParcelable.h revision 942bdc0aebc88dc8b12c0e7742ec0003bbb8b80f
1/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BINDING_SHAREDMEMORYPARCELABLE_H
18#define BINDING_SHAREDMEMORYPARCELABLE_H
19
20#include <stdint.h>
21
22#include <sys/mman.h>
23#include <binder/Parcel.h>
24#include <binder/Parcelable.h>
25
26using android::status_t;
27using android::Parcel;
28using android::Parcelable;
29
30namespace aaudio {
31
32// Arbitrary limits for sanity checks. TODO remove after debugging.
33#define MAX_SHARED_MEMORIES (32)
34#define MAX_MMAP_OFFSET_BYTES (32 * 1024 * 8)
35#define MAX_MMAP_SIZE_BYTES (32 * 1024 * 8)
36
37/**
38 * This is a parcelable description of a shared memory referenced by a file descriptor.
39 * It may be divided into several regions.
40 */
41class SharedMemoryParcelable : public Parcelable {
42public:
43    SharedMemoryParcelable();
44    virtual ~SharedMemoryParcelable();
45
46    void setup(int fd, int32_t sizeInBytes);
47
48    virtual status_t writeToParcel(Parcel* parcel) const override;
49
50    virtual status_t readFromParcel(const Parcel* parcel) override;
51
52    // mmap() shared memory
53    aaudio_result_t resolve(int32_t offsetInBytes, int32_t sizeInBytes, void **regionAddressPtr);
54
55    // munmap() any mapped memory
56    aaudio_result_t close();
57
58    bool isFileDescriptorSafe();
59
60    int32_t getSizeInBytes();
61
62    aaudio_result_t validate();
63
64    void dump();
65
66protected:
67
68#define MMAP_UNRESOLVED_ADDRESS    reinterpret_cast<uint8_t*>(MAP_FAILED)
69
70    int      mFd = -1;
71    int      mOriginalFd = -1;
72    int32_t  mSizeInBytes = 0;
73    uint8_t *mResolvedAddress = MMAP_UNRESOLVED_ADDRESS;
74};
75
76} /* namespace aaudio */
77
78#endif //BINDING_SHAREDMEMORYPARCELABLE_H
79