Moved validation to the specific classes.

It's hard to keep track of what's validated when the validations
are found in two files (NeuralNetworks.cpp + one of the other).
Move all the validations except for the initial nullptr check
to the specific file.

Moved some code from Memory.h into the .cpp file.

Adjusted some type size for setInput/Output/*

Adding new validations will be in the future CLs to make
review easier.

Bug: 63905942
Test: Ran local and VTS tests.
Change-Id: I2c448c9b0d3f2e878278f24eafce5720590403f3
diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp
index f1fb124..9b05dbf 100644
--- a/runtime/Memory.cpp
+++ b/runtime/Memory.cpp
@@ -34,6 +34,80 @@
     return ANEURALNETWORKS_NO_ERROR;
 }
 
+bool Memory::validateSize(uint32_t offset, uint32_t length) const {
+    if (offset + length > mHidlMemory.size()) {
+        LOG(ERROR) << "Request size larger than the memory size.";
+        return false;
+    } else {
+        return true;
+    }
+}
+
+MemoryFd::~MemoryFd() {
+    // Delete the native_handle.
+    if (mHandle) {
+        int fd = mHandle->data[0];
+        if (fd != -1) {
+            close(fd);
+        }
+        native_handle_delete(mHandle);
+    }
+}
+
+int MemoryFd::set(size_t size, int prot, int fd, size_t offset) {
+    if (fd < 0) {
+        LOG(ERROR) << "ANeuralNetworksMemory_createFromFd invalid fd " << fd;
+        return ANEURALNETWORKS_UNEXPECTED_NULL;
+    }
+    if (size == 0 || fd < 0) {
+        LOG(ERROR) << "Invalid size or fd";
+        return ANEURALNETWORKS_BAD_DATA;
+    }
+    int dupfd = dup(fd);
+    if (dupfd == -1) {
+        LOG(ERROR) << "Failed to dup the fd";
+        return ANEURALNETWORKS_UNEXPECTED_NULL;
+    }
+
+    if (mHandle) {
+        native_handle_delete(mHandle);
+    }
+    mHandle = native_handle_create(1, 3);
+    if (mHandle == nullptr) {
+        LOG(ERROR) << "Failed to create native_handle";
+        return ANEURALNETWORKS_UNEXPECTED_NULL;
+    }
+    mHandle->data[0] = dupfd;
+    mHandle->data[1] = prot;
+    mHandle->data[2] = (int32_t)(uint32_t)(offset & 0xffffffff);
+#if defined(__LP64__)
+    mHandle->data[3] = (int32_t)(uint32_t)(offset >> 32);
+#else
+    mHandle->data[3] = 0;
+#endif
+    mHidlMemory = hidl_memory("mmap_fd", mHandle, size);
+    return ANEURALNETWORKS_NO_ERROR;
+}
+
+int MemoryFd::getPointer(uint8_t** buffer) const {
+    if (mHandle == nullptr) {
+        LOG(ERROR) << "Memory not initialized";
+        return ANEURALNETWORKS_UNEXPECTED_NULL;
+    }
+
+    int fd = mHandle->data[0];
+    int prot = mHandle->data[1];
+    size_t offset = getSizeFromInts(mHandle->data[2], mHandle->data[3]);
+    void* data = mmap(nullptr, mHidlMemory.size(), prot, MAP_SHARED, fd, offset);
+    if (data == MAP_FAILED) {
+        LOG(ERROR) << "Can't mmap the file descriptor.";
+        return ANEURALNETWORKS_UNMAPPABLE;
+    } else {
+        *buffer = static_cast<uint8_t*>(data);
+        return ANEURALNETWORKS_NO_ERROR;
+    }
+}
+
 uint32_t MemoryTracker::add(const Memory* memory) {
     // See if we already have this memory. If so,
     // return its index.