Hidlized version of IMemory.

- Add hidl_memory type.
- Add android.hidl.memory@1.0 package
  - IMapper
  - IAllocator
  - IMemory
- Add libhidlmemory
- Add default implementation of android.hidl.memory@1.0
  - Ashmem
  - Ion (TODO, probably in another CL)

Test: compiles (WIP)
Change-Id: I4632eeb6a0051551b54ce04d919aaf551dfc5f28
diff --git a/Android.bp b/Android.bp
index ea670fa..5fe6ad5 100644
--- a/Android.bp
+++ b/Android.bp
@@ -13,8 +13,9 @@
 // limitations under the License.
 
 subdirs = [
-    "base",         // libhidlbase
-    "transport",    // libhidl
+    "base",           // libhidlbase
+    "libhidlmemory",  // libhidlmemory
+    "transport",      // libhidltransport
 ]
 
 cc_test {
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index 35e1672..0103d14 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -13,10 +13,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#define LOG_TAG "HidlSupport"
 
 #include <hidl/HidlSupport.h>
 
 #include <android-base/logging.h>
+
 #ifdef LIBHIDL_TARGET_DEBUGGABLE
 #include <cutils/properties.h>
 #include <regex>
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 6a1f4e5..f9f7f32 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -34,6 +34,16 @@
 #include <vector>
 
 namespace android {
+
+// this file is included by all hidl interface, so we must forward declare the IMemory type.
+namespace hidl {
+namespace memory {
+namespace V1_0 {
+    struct IMemory;
+}; // namespace V1_0
+}; // namespace manager
+}; // namespace hidl
+
 namespace hardware {
 
 // hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
@@ -169,6 +179,75 @@
     return !(s == hs);
 }
 
+// hidl_memory is a structure that can be used to transfer
+// pieces of shared memory between processes. The assumption
+// of this object is that the memory remains accessible as
+// long as the file descriptors in the enclosed mHandle
+// - as well as all of its cross-process dups() - remain opened.
+struct hidl_memory {
+
+    hidl_memory() : mOwnsHandle(false), mHandle(nullptr), mSize(0), mName("") {
+    }
+
+    /**
+     * Creates a hidl_memory object and takes ownership of the handle.
+     */
+    hidl_memory(const hidl_string &name, const hidl_handle &handle, size_t size)
+       : mOwnsHandle(true),
+         mHandle(handle),
+         mSize(size),
+         mName(name)
+    {}
+
+    // copy constructor
+    hidl_memory(const hidl_memory& other) {
+        *this = other;
+    }
+
+    // copy assignment
+    hidl_memory &operator=(const hidl_memory &other) {
+        if (this != &other) {
+            mOwnsHandle = true;
+            mHandle = native_handle_clone(other.mHandle);
+            mSize = other.mSize;
+            mName = other.mName;
+        }
+
+        return *this;
+    }
+
+    // TODO move constructor/move assignment
+
+    ~hidl_memory() {
+        // TODO if we had previously mapped from this object, unmap
+        if (mOwnsHandle) {
+            native_handle_close(mHandle);
+        }
+    }
+
+    const native_handle_t* handle() const {
+        return mHandle;
+    }
+
+    const hidl_string &name() const {
+        return mName;
+    }
+
+    size_t size() const {
+        return mSize;
+    }
+
+    // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
+    static const size_t kOffsetOfHandle;
+    // offsetof(hidl_memory, mName) exposed since mHandle is private.
+    static const size_t kOffsetOfName;
+private:
+    bool mOwnsHandle;
+    hidl_handle mHandle;
+    size_t mSize;
+    hidl_string mName;
+};
+
 ////////////////////////////////////////////////////////////////////////////////
 
 template<typename T>
diff --git a/libhidlmemory/Android.bp b/libhidlmemory/Android.bp
new file mode 100644
index 0000000..8f27065
--- /dev/null
+++ b/libhidlmemory/Android.bp
@@ -0,0 +1,43 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_library_shared {
+    name: "libhidlmemory",
+    shared_libs: [
+        "libbase",
+        "liblog",
+        "libutils",
+        "libcutils",
+        "libhidlbase",
+        "libhidltransport",
+        "android.hidl.memory@1.0"
+    ],
+    local_include_dirs: ["include"],
+    export_include_dirs: ["include"],
+
+    export_shared_lib_headers: [
+        "android.hidl.memory@1.0",
+        "libhidlbase"
+    ],
+
+    srcs: [
+        "mapping.cpp"
+    ],
+
+    product_variables: {
+        debuggable: {
+            cflags: ["-DLIBHIDL_TARGET_DEBUGGABLE"],
+        },
+    },
+}
diff --git a/libhidlmemory/include/hidlmemory/mapping.h b/libhidlmemory/include/hidlmemory/mapping.h
new file mode 100644
index 0000000..8ed0d54
--- /dev/null
+++ b/libhidlmemory/include/hidlmemory/mapping.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <android/hidl/memory/1.0/IMemory.h>
+#include <hidl/HidlSupport.h>
+
+namespace android {
+namespace hardware {
+
+sp<android::hidl::memory::V1_0::IMemory> mapMemory(const hidl_memory &memory);
+
+}  // namespace hardware
+}  // namespace android
\ No newline at end of file
diff --git a/libhidlmemory/mapping.cpp b/libhidlmemory/mapping.cpp
new file mode 100644
index 0000000..b4fa123
--- /dev/null
+++ b/libhidlmemory/mapping.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#define LOG_TAG "libhidlmemory"
+
+#include <hidlmemory/mapping.h>
+
+#include <android-base/logging.h>
+#include <android/hidl/memory/1.0/IMapper.h>
+#include <hidl/HidlSupport.h>
+
+using android::sp;
+using android::hidl::memory::V1_0::IMemory;
+
+namespace android {
+namespace hardware {
+
+sp<IMemory> mapMemory(const hidl_memory &memory) {
+    using android::hidl::memory::V1_0::IMapper;
+
+    sp<IMapper> mapper = IMapper::getService(memory.name(), true /* getStub */);
+
+    if (mapper == nullptr) {
+        LOG(FATAL) << "Could not fetch mapper for " << memory.name() << " shared memory";
+    }
+
+    if (mapper->isRemote()) {
+        LOG(FATAL) << "IMapper must be a passthrough service.";
+    }
+
+    sp<IMemory> retMemory = nullptr;
+
+    Return<void> ret = mapper->mapMemory(memory,
+        [&retMemory](const auto &mapped) {
+            retMemory = mapped;
+        });
+
+    if (!ret.isOk()) {
+        LOG(FATAL) << "hidl_memory map returned transport error.";
+    }
+
+    return retMemory;
+}
+
+}  // namespace hardware
+}  // namespace android
\ No newline at end of file
diff --git a/transport/Android.bp b/transport/Android.bp
index 032381a..ac4cb12 100644
--- a/transport/Android.bp
+++ b/transport/Android.bp
@@ -13,7 +13,9 @@
 // limitations under the License.
 
 subdirs = [
-    "manager/1.0"
+    "manager/1.0",
+    "memory/1.0",
+    "memory/1.0/default",
 ]
 
 cc_library_shared {
diff --git a/transport/HidlBinderSupport.cpp b/transport/HidlBinderSupport.cpp
index c206018..f3e99e1 100644
--- a/transport/HidlBinderSupport.cpp
+++ b/transport/HidlBinderSupport.cpp
@@ -27,6 +27,47 @@
 
 std::map<std::string, std::function<sp<IBinder>(void*)>> gBnConstructorMap{};
 
+const size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
+const size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
+
+status_t readEmbeddedFromParcel(hidl_memory * /* memory */,
+        const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
+    ::android::status_t _hidl_err = ::android::OK;
+    const native_handle_t *_hidl_memory_handle = parcel.readEmbeddedNativeHandle(
+            parentHandle,
+            parentOffset + hidl_memory::kOffsetOfHandle);
+
+    if (_hidl_memory_handle == nullptr) {
+        _hidl_err = ::android::UNKNOWN_ERROR;
+        return _hidl_err;
+    }
+
+    _hidl_err = readEmbeddedFromParcel(
+            (hidl_string*) nullptr,
+            parcel,
+            parentHandle,
+            parentOffset + hidl_memory::kOffsetOfName);
+
+    return _hidl_err;
+}
+
+status_t writeEmbeddedToParcel(const hidl_memory &memory,
+        Parcel *parcel, size_t parentHandle, size_t parentOffset) {
+    status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
+            memory.handle(),
+            parentHandle,
+            parentOffset + hidl_memory::kOffsetOfHandle);
+
+    if (_hidl_err == ::android::OK) {
+        _hidl_err = writeEmbeddedToParcel(
+            memory.name(),
+            parcel,
+            parentHandle,
+            parentOffset + hidl_memory::kOffsetOfName);
+    }
+
+    return _hidl_err;
+}
 // static
 const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
 
diff --git a/transport/include/hidl/HidlBinderSupport.h b/transport/include/hidl/HidlBinderSupport.h
index 84ba966..d6a308b 100644
--- a/transport/include/hidl/HidlBinderSupport.h
+++ b/transport/include/hidl/HidlBinderSupport.h
@@ -28,6 +28,14 @@
 namespace android {
 namespace hardware {
 
+// ---------------------- hidl_memory
+
+status_t readEmbeddedFromParcel(hidl_memory *memory,
+        const Parcel &parcel, size_t parentHandle, size_t parentOffset);
+
+status_t writeEmbeddedToParcel(const hidl_memory &memory,
+        Parcel *parcel, size_t parentHandle, size_t parentOffset);
+
 // ---------------------- hidl_string
 
 status_t readEmbeddedFromParcel(hidl_string *string,
diff --git a/transport/manager/1.0/update-makefiles.sh b/transport/manager/1.0/update-makefiles.sh
deleted file mode 100755
index 850ca7b..0000000
--- a/transport/manager/1.0/update-makefiles.sh
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/bash
-
-if [ ! -d system/libhidl ] ; then
-  echo "Where is system/libhidl?";
-  exit 1;
-fi
-
-hidl-gen -Lmakefile -r android.hidl:system/libhidl/transport android.hidl.manager@1.0
-hidl-gen -Landroidbp -r android.hidl:system/libhidl/transport android.hidl.manager@1.0
diff --git a/transport/memory/1.0/Android.bp b/transport/memory/1.0/Android.bp
new file mode 100644
index 0000000..5839197
--- /dev/null
+++ b/transport/memory/1.0/Android.bp
@@ -0,0 +1,66 @@
+// This file is autogenerated by hidl-gen. Do not edit manually.
+
+genrule {
+    name: "android.hidl.memory@1.0_genc++",
+    tools: ["hidl-gen"],
+    cmd: "$(location hidl-gen) -o $(genDir) -Lc++ -randroid.hidl:system/libhidl/transport android.hidl.memory@1.0",
+    srcs: [
+        "IAllocator.hal",
+        "IMapper.hal",
+        "IMemory.hal",
+    ],
+    out: [
+        "android/hidl/memory/1.0/AllocatorAll.cpp",
+        "android/hidl/memory/1.0/MapperAll.cpp",
+        "android/hidl/memory/1.0/MemoryAll.cpp",
+    ],
+}
+
+genrule {
+    name: "android.hidl.memory@1.0_genc++_headers",
+    tools: ["hidl-gen"],
+    cmd: "$(location hidl-gen) -o $(genDir) -Lc++ -randroid.hidl:system/libhidl/transport android.hidl.memory@1.0",
+    srcs: [
+        "IAllocator.hal",
+        "IMapper.hal",
+        "IMemory.hal",
+    ],
+    out: [
+        "android/hidl/memory/1.0/IAllocator.h",
+        "android/hidl/memory/1.0/IHwAllocator.h",
+        "android/hidl/memory/1.0/BnAllocator.h",
+        "android/hidl/memory/1.0/BpAllocator.h",
+        "android/hidl/memory/1.0/BsAllocator.h",
+        "android/hidl/memory/1.0/IMapper.h",
+        "android/hidl/memory/1.0/IHwMapper.h",
+        "android/hidl/memory/1.0/BnMapper.h",
+        "android/hidl/memory/1.0/BpMapper.h",
+        "android/hidl/memory/1.0/BsMapper.h",
+        "android/hidl/memory/1.0/IMemory.h",
+        "android/hidl/memory/1.0/IHwMemory.h",
+        "android/hidl/memory/1.0/BnMemory.h",
+        "android/hidl/memory/1.0/BpMemory.h",
+        "android/hidl/memory/1.0/BsMemory.h",
+    ],
+}
+
+cc_library_shared {
+    name: "android.hidl.memory@1.0",
+    generated_sources: ["android.hidl.memory@1.0_genc++"],
+    generated_headers: ["android.hidl.memory@1.0_genc++_headers"],
+    export_generated_headers: ["android.hidl.memory@1.0_genc++_headers"],
+    shared_libs: [
+        "libhidlbase",
+        "libhidltransport",
+        "libhwbinder",
+        "liblog",
+        "libutils",
+        "libcutils",
+    ],
+    export_shared_lib_headers: [
+        "libhidlbase",
+        "libhidltransport",
+        "libhwbinder",
+        "libutils",
+    ],
+}
diff --git a/transport/memory/1.0/IAllocator.hal b/transport/memory/1.0/IAllocator.hal
new file mode 100644
index 0000000..7f540aa
--- /dev/null
+++ b/transport/memory/1.0/IAllocator.hal
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hidl.memory@1.0;
+
+/**
+ * Interface which allocates the required memory.
+ */
+interface IAllocator {
+
+    /**
+     * Return memory must have instance name corresponding to this type of memory.
+     *
+     * @param size Size of memory to allocate in bytes.
+     * @return success Whether allocation succeeded.
+     * @return memory Unmapped memory object.
+     */
+    allocate(uint64_t size) generates (bool success, memory mem);
+};
\ No newline at end of file
diff --git a/transport/memory/1.0/IMapper.hal b/transport/memory/1.0/IMapper.hal
new file mode 100644
index 0000000..6f730a0
--- /dev/null
+++ b/transport/memory/1.0/IMapper.hal
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hidl.memory@1.0;
+
+import IMemory;
+
+interface IMapper {
+
+    /**
+     * @param mem Reference to shared memory.
+     * @return mappedMemory Object representing memory mapped in this process.
+     *                      This will be null if mapping fails.
+     */
+    mapMemory(memory mem) generates (IMemory mappedMemory);
+};
diff --git a/transport/memory/1.0/IMemory.hal b/transport/memory/1.0/IMemory.hal
new file mode 100644
index 0000000..1b38336
--- /dev/null
+++ b/transport/memory/1.0/IMemory.hal
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hidl.memory@1.0;
+
+interface IMemory {
+
+    /**
+     * Notify that you are about to use this memory.
+     */
+    update();
+
+    /**
+     * Notify that you are done modifying this memory.
+     */
+    commit();
+
+    /**
+     * @return ptr Actual pointer to underlying memory.
+     */
+    getPointer() generates (pointer ptr);
+
+    /**
+     * @return size Size in bytes of underlying memory.
+     */
+    getSize() generates (uint64_t size);
+
+};
\ No newline at end of file
diff --git a/transport/memory/1.0/default/Android.bp b/transport/memory/1.0/default/Android.bp
new file mode 100644
index 0000000..b5079e8
--- /dev/null
+++ b/transport/memory/1.0/default/Android.bp
@@ -0,0 +1,57 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_library_shared {
+    name: "android.hidl.memory@1.0-impl",
+    relative_install_path: "hw",
+    srcs: [
+        "AshmemAllocator.cpp",
+        "AshmemMapper.cpp",
+        "AshmemMemory.cpp",
+        "HidlFetch.cpp"
+    ],
+    shared_libs: [
+        "liblog",
+        "libcutils",
+        "libhardware",
+        "libhwbinder",
+        "libbase",
+        "libcutils",
+        "libutils",
+        "libhidlbase",
+        "libhidltransport",
+        "android.hidl.memory@1.0",
+    ],
+}
+
+cc_binary {
+    name: "android.hidl.memory@1.0-service",
+    relative_install_path: "hw",
+    srcs: ["service.cpp"],
+    init_rc: ["android.hidl.memory@1.0-service.rc"],
+
+    shared_libs: [
+        "android.hidl.memory@1.0",
+        "libhidlbase",
+        "libhidltransport",
+        "libhwbinder",
+        "liblog",
+        "libutils",
+    ],
+
+    required: [
+        // only one implementation is allowed
+        "android.hidl.memory@1.0-impl"
+    ]
+}
diff --git a/transport/memory/1.0/default/AshmemAllocator.cpp b/transport/memory/1.0/default/AshmemAllocator.cpp
new file mode 100644
index 0000000..59ae3e1
--- /dev/null
+++ b/transport/memory/1.0/default/AshmemAllocator.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "AshmemAllocator.h"
+
+#include <cutils/ashmem.h>
+
+namespace android {
+namespace hidl {
+namespace memory {
+namespace V1_0 {
+namespace implementation {
+
+// Methods from ::android::hidl::memory::V1_0::IAllocator follow.
+Return<void> AshmemAllocator::allocate(uint64_t size, allocate_cb _hidl_cb) {
+    int fd = ashmem_create_region("AshmemAllocator_hidl", size);
+    if (fd < 0) {
+        _hidl_cb(false /* success */, hidl_memory());
+        return Void();
+    }
+
+    native_handle_t* handle = native_handle_create(1, 0);
+    handle->data[0] = fd;
+    hidl_memory memory("ashmem", handle, size);
+
+    _hidl_cb(true /* success */, memory);
+    return Void();
+}
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace memory
+}  // namespace hidl
+}  // namespace android
diff --git a/transport/memory/1.0/default/AshmemAllocator.h b/transport/memory/1.0/default/AshmemAllocator.h
new file mode 100644
index 0000000..35ad420
--- /dev/null
+++ b/transport/memory/1.0/default/AshmemAllocator.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HIDL_ASHMEM_MEMORY_V1_0_ALLOCATOR_H
+#define ANDROID_HIDL_ASHMEM_MEMORY_V1_0_ALLOCATOR_H
+
+#include <android/hidl/memory/1.0/IAllocator.h>
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+
+namespace android {
+namespace hidl {
+namespace memory {
+namespace V1_0 {
+namespace implementation {
+
+using ::android::hidl::memory::V1_0::IAllocator;
+using ::android::hardware::hidl_array;
+using ::android::hardware::hidl_memory;
+using ::android::hardware::hidl_string;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::sp;
+
+struct AshmemAllocator : public IAllocator {
+    // Methods from ::android::hidl::memory::V1_0::IAllocator follow.
+    Return<void> allocate(uint64_t size, allocate_cb _hidl_cb) override;
+
+};
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace memory
+}  // namespace hidl
+}  // namespace android
+
+#endif  // ANDROID_HIDL_ASHMEM_MEMORY_V1_0_ALLOCATOR_H
diff --git a/transport/memory/1.0/default/AshmemMapper.cpp b/transport/memory/1.0/default/AshmemMapper.cpp
new file mode 100644
index 0000000..37eb8af
--- /dev/null
+++ b/transport/memory/1.0/default/AshmemMapper.cpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "AshmemMapper.h"
+
+#include <sys/mman.h>
+
+#include "AshmemMemory.h"
+
+namespace android {
+namespace hidl {
+namespace memory {
+namespace V1_0 {
+namespace implementation {
+
+// Methods from ::android::hidl::memory::V1_0::IMapper follow.
+Return<void> AshmemMapper::mapMemory(const hidl_memory& mem, mapMemory_cb _hidl_cb) {
+    if (mem.handle()->numFds == 0) {
+        _hidl_cb(nullptr);
+        return Void();
+    }
+
+    int fd = mem.handle()->data[0];
+    void* data = mmap(0, mem.size(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+    if (data == MAP_FAILED) {
+        // mmap never maps at address zero without MAP_FIXED, so we can avoid
+        // exposing clients to MAP_FAILED.
+        _hidl_cb(nullptr);
+        return Void();
+    }
+
+    sp<IMemory> memory = new AshmemMemory(mem, data);
+
+    _hidl_cb(memory);
+    return Void();
+}
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace memory
+}  // namespace hidl
+}  // namespace android
diff --git a/transport/memory/1.0/default/AshmemMapper.h b/transport/memory/1.0/default/AshmemMapper.h
new file mode 100644
index 0000000..c6cfc37
--- /dev/null
+++ b/transport/memory/1.0/default/AshmemMapper.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HIDL_ASHMEM_MEMORY_V1_0_MAPPER_H
+#define ANDROID_HIDL_ASHMEM_MEMORY_V1_0_MAPPER_H
+
+#include <android/hidl/memory/1.0/IMapper.h>
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+
+namespace android {
+namespace hidl {
+namespace memory {
+namespace V1_0 {
+namespace implementation {
+
+using ::android::hidl::memory::V1_0::IMapper;
+using ::android::hardware::hidl_array;
+using ::android::hardware::hidl_memory;
+using ::android::hardware::hidl_string;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::sp;
+
+struct AshmemMapper : public IMapper {
+    // Methods from ::android::hidl::memory::V1_0::IMapper follow.
+    Return<void> mapMemory(const hidl_memory& mem, mapMemory_cb _hidl_cb) override;
+
+};
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace memory
+}  // namespace hidl
+}  // namespace android
+
+#endif  // ANDROID_HIDL_ASHMEM_MEMORY_V1_0_MAPPER_H
diff --git a/transport/memory/1.0/default/AshmemMemory.cpp b/transport/memory/1.0/default/AshmemMemory.cpp
new file mode 100644
index 0000000..31626ea
--- /dev/null
+++ b/transport/memory/1.0/default/AshmemMemory.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/mman.h>
+
+#include "AshmemMemory.h"
+
+namespace android {
+namespace hidl {
+namespace memory {
+namespace V1_0 {
+namespace implementation {
+
+AshmemMemory::AshmemMemory(const hidl_memory& memory, void* data)
+  : mMemory(memory),
+    mData(data)
+{}
+
+AshmemMemory::~AshmemMemory()
+{
+    // TODO: Move implementation to mapper class
+    munmap(mData, mMemory.size());
+}
+
+// Methods from ::android::hidl::memory::V1_0::IMemory follow.
+Return<void> AshmemMemory::update() {
+    // NOOP
+    return Void();
+}
+
+Return<void> AshmemMemory::commit() {
+    // NOOP
+    return Void();
+}
+
+Return<void*> AshmemMemory::getPointer() {
+    return mData;
+}
+
+Return<uint64_t> AshmemMemory::getSize() {
+    return mMemory.size();
+}
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace memory
+}  // namespace hidl
+}  // namespace android
diff --git a/transport/memory/1.0/default/AshmemMemory.h b/transport/memory/1.0/default/AshmemMemory.h
new file mode 100644
index 0000000..9dbe1fe
--- /dev/null
+++ b/transport/memory/1.0/default/AshmemMemory.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HIDL_ASHMEM_MEMORY_V1_0_MEMORY_H
+#define ANDROID_HIDL_ASHMEM_MEMORY_V1_0_MEMORY_H
+
+#include <android/hidl/memory/1.0/IMemory.h>
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+
+namespace android {
+namespace hidl {
+namespace memory {
+namespace V1_0 {
+namespace implementation {
+
+using ::android::hidl::memory::V1_0::IMemory;
+using ::android::hardware::hidl_array;
+using ::android::hardware::hidl_memory;
+using ::android::hardware::hidl_string;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::sp;
+
+struct AshmemMemory : public IMemory {
+
+    AshmemMemory(const hidl_memory& memory, void* mappedMemory);
+    ~AshmemMemory();
+
+    // Methods from ::android::hidl::memory::V1_0::IMemory follow.
+    Return<void> update() override;
+    Return<void> commit() override;
+    Return<void*> getPointer() override;
+    Return<uint64_t> getSize() override;
+
+private:
+    // Holding onto hidl_memory reference because it contains
+    // handle and size, and handle will also be required for
+    // the remoted case.
+    hidl_memory mMemory;
+
+    // Mapped memory in process.
+    void* mData;
+};
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace memory
+}  // namespace hidl
+}  // namespace android
+
+#endif  // ANDROID_HIDL_ASHMEM_MEMORY_V1_0_MEMORY_H
diff --git a/transport/memory/1.0/default/HidlFetch.cpp b/transport/memory/1.0/default/HidlFetch.cpp
new file mode 100644
index 0000000..8236055
--- /dev/null
+++ b/transport/memory/1.0/default/HidlFetch.cpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "HidlFetch.h"
+
+#include "AshmemAllocator.h"
+#include "AshmemMapper.h"
+
+static std::string kAshmemMemoryName = "ashmem";
+
+namespace android {
+namespace hidl {
+namespace memory {
+namespace V1_0 {
+namespace implementation {
+
+IAllocator* HIDL_FETCH_IAllocator(const char* name) {
+    if (name == kAshmemMemoryName) {
+        return new AshmemAllocator;
+    }
+
+    return nullptr;
+}
+
+IMapper* HIDL_FETCH_IMapper(const char* name) {
+    if (name == kAshmemMemoryName) {
+        return new AshmemMapper;
+    }
+
+    return nullptr;
+}
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace memory
+}  // namespace hidl
+}  // namespace android
diff --git a/transport/memory/1.0/default/HidlFetch.h b/transport/memory/1.0/default/HidlFetch.h
new file mode 100644
index 0000000..a9d366f
--- /dev/null
+++ b/transport/memory/1.0/default/HidlFetch.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef HIDL_FETCH_H
+#define HIDL_FETCH_H
+
+#include <android/hidl/memory/1.0/IAllocator.h>
+#include <android/hidl/memory/1.0/IMapper.h>
+
+namespace android {
+namespace hidl {
+namespace memory {
+namespace V1_0 {
+namespace implementation {
+
+// TODO: disable passthrough allocator. It's much better if it's in a centralized location
+extern "C" IAllocator* HIDL_FETCH_IAllocator(const char* name);
+
+extern "C" IMapper* HIDL_FETCH_IMapper(const char* name);
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace memory
+}  // namespace hidl
+}  // namespace android
+
+#endif  // HIDL_FETCH_H
\ No newline at end of file
diff --git a/transport/memory/1.0/default/android.hidl.memory@1.0-service.rc b/transport/memory/1.0/default/android.hidl.memory@1.0-service.rc
new file mode 100644
index 0000000..1caaae9
--- /dev/null
+++ b/transport/memory/1.0/default/android.hidl.memory@1.0-service.rc
@@ -0,0 +1,4 @@
+service hidl_memory /system/bin/hw/android.hidl.memory@1.0-service
+    class hal
+    user system
+    group system
diff --git a/transport/memory/1.0/default/service.cpp b/transport/memory/1.0/default/service.cpp
new file mode 100644
index 0000000..ff3a8e8
--- /dev/null
+++ b/transport/memory/1.0/default/service.cpp
@@ -0,0 +1,12 @@
+#define LOG_TAG "android.hidl.memory@1.0-service"
+
+#include <android/hidl/memory/1.0/IAllocator.h>
+
+#include <hidl/LegacySupport.h>
+
+using android::hidl::memory::V1_0::IAllocator;
+using android::hardware::defaultPassthroughServiceImplementation;
+
+int main() {
+    return defaultPassthroughServiceImplementation<IAllocator>("ashmem");
+}
diff --git a/update-makefiles.sh b/update-makefiles.sh
new file mode 100755
index 0000000..4e17f64
--- /dev/null
+++ b/update-makefiles.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+# TODO(b/33276472)
+
+if [ ! -d system/libhidl ] ; then
+  echo "Where is system/libhidl?";
+  exit 1;
+fi
+
+#manager
+hidl-gen -Lmakefile -r android.hidl:system/libhidl/transport android.hidl.manager@1.0
+hidl-gen -Landroidbp -r android.hidl:system/libhidl/transport android.hidl.manager@1.0
+
+#memory
+hidl-gen -Lmakefile -r android.hidl:system/libhidl/transport android.hidl.memory@1.0
+hidl-gen -Landroidbp -r android.hidl:system/libhidl/transport android.hidl.memory@1.0