Move align functions to libcuttlefish_utils

These functions are needed for gralloc buffers as well so they belong
in a common library.

Bug: 110539298
Test: run on gce
Change-Id: Ib2237f19141f15505503807d5e06f2d293d03ed1
diff --git a/Android.bp b/Android.bp
index be209a0..c79a9c0 100644
--- a/Android.bp
+++ b/Android.bp
@@ -111,6 +111,7 @@
     header_libs: ["cuttlefish_glog"],
     shared_libs: [
         "libcuttlefish_fs",
+        "libcuttlefish_utils",
         "cuttlefish_auto_resources",
         "libbase",
         "liblog",
diff --git a/common/libs/utils/Android.bp b/common/libs/utils/Android.bp
index c30543b..0e41180 100644
--- a/common/libs/utils/Android.bp
+++ b/common/libs/utils/Android.bp
@@ -18,6 +18,7 @@
     srcs: [
         "subprocess.cpp",
         "environment.cpp",
+        "size_utils.cpp",
     ],
     header_libs: [
         "cuttlefish_glog",
diff --git a/common/libs/utils/size_utils.cpp b/common/libs/utils/size_utils.cpp
new file mode 100644
index 0000000..c43bf96
--- /dev/null
+++ b/common/libs/utils/size_utils.cpp
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 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 "common/libs/utils/size_utils.h"
+
+#include <unistd.h>
+
+namespace cvd {
+
+uint32_t AlignToPageSize(uint32_t val) {
+  static uint32_t page_size = sysconf(_SC_PAGESIZE);
+  return ((val + (page_size - 1)) / page_size) * page_size;
+}
+
+uint32_t RoundUpToNextPowerOf2(uint32_t val) {
+  uint32_t power_of_2 = 1;
+  while (power_of_2 < val) {
+    power_of_2 *= 2;
+  }
+  return power_of_2;
+}
+
+uint32_t AlignToPowerOf2(uint32_t val, uint8_t align_log) {
+  uint32_t align = 1 << align_log;
+  return ((val + (align - 1)) / align) * align;
+}
+
+}  // namespace cvd
diff --git a/common/libs/utils/size_utils.h b/common/libs/utils/size_utils.h
new file mode 100644
index 0000000..1d34493
--- /dev/null
+++ b/common/libs/utils/size_utils.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 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.
+ */
+#pragma once
+
+#include <stdint.h>
+
+namespace cvd {
+
+// Returns the smallest multiple of PAGE_SIZE greater than or equal to val.
+uint32_t AlignToPageSize(uint32_t val);
+
+// Returns the smallest power of two greater than or equal to val.
+uint32_t RoundUpToNextPowerOf2(uint32_t val);
+
+// Returns the smallest multiple of 2^align_log greater than or equal to val.
+uint32_t AlignToPowerOf2(uint32_t val, uint8_t align_log);
+
+}  // namespace cvd
diff --git a/common/vsoc/lib/vsoc_memory.cpp b/common/vsoc/lib/vsoc_memory.cpp
index 76ecb26..0053e98 100644
--- a/common/vsoc/lib/vsoc_memory.cpp
+++ b/common/vsoc/lib/vsoc_memory.cpp
@@ -24,6 +24,7 @@
 #include <type_traits>
 
 #include "common/libs/glog/logging.h"
+#include "common/libs/utils/size_utils.h"
 #include "common/vsoc/shm/audio_data_layout.h"
 #include "common/vsoc/shm/base.h"
 #include "common/vsoc/shm/e2e_test_region_layout.h"
@@ -38,19 +39,6 @@
 
 namespace {
 
-uint32_t AlignToPageSize(uint32_t val) {
-  static uint32_t page_size = sysconf(_SC_PAGESIZE);
-  return ((val + (page_size - 1)) / page_size) * page_size;
-}
-
-uint32_t AlignToPowerOf2(uint32_t val) {
-  uint32_t power_of_2 = 1;
-  while (power_of_2 < val) {
-    power_of_2 *= 2;
-  }
-  return power_of_2;
-}
-
 // Takes a vector of objects and returns a vector of pointers to those objects.
 template <typename T, typename R>
 std::vector<R*> GetConstPointers(const std::vector<T>& v) {
@@ -106,7 +94,7 @@
     auto size = GetOffsetOfRegionData();
     // Data section
     size += layout_size_;
-    size = AlignToPageSize(size);
+    size = cvd::AlignToPageSize(size);
     return size;
   }
 
@@ -151,7 +139,7 @@
     offset += sizeof(vsoc_shm_layout_descriptor);
     // and region descriptors
     offset += regions_.size() * sizeof(vsoc_device_region);
-    offset = AlignToPageSize(offset);
+    offset = cvd::AlignToPageSize(offset);
 
     // Calculate offsets for all regions and set the size of the device
     UpdateRegionOffsetsAndDeviceSize(offset);
@@ -189,7 +177,7 @@
     auto min_required_size = region.GetMinRegionSize();
 
     // Align to page size
-    new_min_size = AlignToPageSize(new_min_size);
+    new_min_size = cvd::AlignToPageSize(new_min_size);
     if (new_min_size < min_required_size) {
       LOG(ERROR) << "Requested resize of region " << region_name << " to "
                  << new_min_size << " (after alignment), it needs at least "
@@ -235,7 +223,7 @@
     }
 
     // Make the device's size the smaller power of two possible
-    device_size_ = AlignToPowerOf2(offset);
+    device_size_ = cvd::RoundUpToNextPowerOf2(offset);
   }
 
   std::vector<VSoCRegionLayoutImpl> regions_;