Add functions to allocate/free raw aligned buffers
Change-Id: Ia42d562657154f85fa8062585a676f40746f6624
diff --git a/android-emu/Android.mk b/android-emu/Android.mk
index 239204b..0e02fc1 100644
--- a/android-emu/Android.mk
+++ b/android-emu/Android.mk
@@ -11,6 +11,7 @@
-fstrict-aliasing \
LOCAL_SRC_FILES := \
+ android/base/AlignedBuf.cpp \
android/base/files/MemStream.cpp \
android/base/files/Stream.cpp \
android/base/files/StreamSerializing.cpp \
diff --git a/android-emu/CMakeLists.txt b/android-emu/CMakeLists.txt
index ad4e20d..432b3b8 100644
--- a/android-emu/CMakeLists.txt
+++ b/android-emu/CMakeLists.txt
@@ -1,7 +1,7 @@
# This is an autogenerated file! Do not edit!
# instead run make from .../device/generic/goldfish-opengl
# which will re-generate this file.
-set(androidemu_src android/base/files/MemStream.cpp android/base/files/Stream.cpp android/base/files/StreamSerializing.cpp android/base/Pool.cpp android/base/StringFormat.cpp android/utils/debug.c)
+set(androidemu_src android/base/AlignedBuf.cpp android/base/files/MemStream.cpp android/base/files/Stream.cpp android/base/files/StreamSerializing.cpp android/base/Pool.cpp android/base/StringFormat.cpp android/utils/debug.c)
android_add_library(androidemu)
target_include_directories(androidemu PRIVATE ${GOLDFISH_DEVICE_ROOT}/android-emu ${GOLDFISH_DEVICE_ROOT}/./host/include/libOpenglRender ${GOLDFISH_DEVICE_ROOT}/./system/include ${GOLDFISH_DEVICE_ROOT}/./../../../external/qemu/android/android-emugl/guest)
target_compile_definitions(androidemu PRIVATE "-DWITH_GLES2" "-DPLATFORM_SDK_VERSION=29" "-DGOLDFISH_HIDL_GRALLOC" "-DEMULATOR_OPENGL_POST_O=1" "-DHOST_BUILD" "-DANDROID" "-DGL_GLEXT_PROTOTYPES" "-DPAGE_SIZE=4096" "-DGOLDFISH_VULKAN" "-DLOG_TAG=\"androidemu\"")
diff --git a/android-emu/android/base/AlignedBuf.cpp b/android-emu/android/base/AlignedBuf.cpp
new file mode 100644
index 0000000..a1bc6f3
--- /dev/null
+++ b/android-emu/android/base/AlignedBuf.cpp
@@ -0,0 +1,45 @@
+// Copyright 2018 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 "AlignedBuf.h"
+
+namespace android {
+
+// Convenience function for aligned malloc across platforms
+void* aligned_buf_alloc(size_t align, size_t size) {
+ size_t actualAlign = std::max(align, sizeof(void*));
+#ifdef _WIN32
+ void* res = _aligned_malloc(size, actualAlign);
+ if (!res) {
+ abort();
+ }
+ return res;
+#else
+ void* res;
+ if (posix_memalign(&res, actualAlign, size)) {
+ abort();
+ }
+ return res;
+#endif
+}
+
+void aligned_buf_free(void* buf) {
+#ifdef _WIN32
+ _aligned_free(buf);
+#else
+ free(buf);
+#endif
+}
+
+} // namespace android
\ No newline at end of file
diff --git a/android-emu/android/base/AlignedBuf.h b/android-emu/android/base/AlignedBuf.h
index faebb2f..fc74e32 100644
--- a/android-emu/android/base/AlignedBuf.h
+++ b/android-emu/android/base/AlignedBuf.h
@@ -142,4 +142,8 @@
size_t mSize = 0;
};
+// Convenience function for aligned malloc across platforms
+void* aligned_buf_alloc(size_t align, size_t size);
+void aligned_buf_free(void* buf);
+
} // namespace android
diff --git a/system/vulkan_enc/VkEncoder.cpp b/system/vulkan_enc/VkEncoder.cpp
index 9520fd6..360d0ee 100644
--- a/system/vulkan_enc/VkEncoder.cpp
+++ b/system/vulkan_enc/VkEncoder.cpp
@@ -28,6 +28,8 @@
#include "IOStream.h"
#include "VulkanStream.h"
+#include "android/base/AlignedBuf.h"
+
#include "goldfish_vk_marshaling_guest.h"
@@ -38,6 +40,9 @@
using goldfish_vk::VulkanCountingStream;
using goldfish_vk::VulkanStream;
+using android::aligned_buf_alloc;
+using android::aligned_buf_free;
+
class VkEncoder::Impl {
public:
Impl(IOStream* stream) : m_stream(stream) { }