Merge pull request #17 from apache/serialize_returns_vector

use new version that returns vector from serialize
diff --git a/src/cpc_sketch_c_adapter.cpp b/src/cpc_sketch_c_adapter.cpp
index 3d18828..0972ba2 100644
--- a/src/cpc_sketch_c_adapter.cpp
+++ b/src/cpc_sketch_c_adapter.cpp
@@ -97,9 +97,11 @@
 struct ptr_with_size cpc_sketch_serialize(const void* sketchptr, unsigned header_size) {
   try {
     ptr_with_size p;
-    auto data = static_cast<const cpc_sketch_pg*>(sketchptr)->serialize(header_size);
-    p.ptr = data.first.release();
-    p.size = data.second;
+    auto bytes = new (palloc(sizeof(cpc_sketch_pg::vector_bytes))) cpc_sketch_pg::vector_bytes(
+      static_cast<const cpc_sketch_pg*>(sketchptr)->serialize(header_size)
+    );
+    p.ptr = bytes->data();
+    p.size = bytes->size();
     return p;
   } catch (std::exception& e) {
     pg_error(e.what());
diff --git a/src/frequent_strings_sketch_c_adapter.cpp b/src/frequent_strings_sketch_c_adapter.cpp
index c2687d8..ee3705b 100644
--- a/src/frequent_strings_sketch_c_adapter.cpp
+++ b/src/frequent_strings_sketch_c_adapter.cpp
@@ -59,26 +59,26 @@
   size_t size_of_item(const string& item) {
     return sizeof(uint32_t) + item.size();
   }
-  size_t serialize(char* ptr, const string* items, unsigned num) {
+  size_t serialize(void* ptr, const string* items, unsigned num) {
     size_t size = sizeof(uint32_t) * num;
     for (unsigned i = 0; i < num; i++) {
       uint32_t length = items[i].size();
       memcpy(ptr, &length, sizeof(length));
-      ptr += sizeof(uint32_t);
+      ptr = static_cast<char*>(ptr) + sizeof(uint32_t);
       memcpy(ptr, items[i].c_str(), length);
-      ptr += length;
+      ptr = static_cast<char*>(ptr) + length;
       size += length;
     }
     return size;
   }
-  size_t deserialize(const char* ptr, string* items, unsigned num) {
+  size_t deserialize(const void* ptr, string* items, unsigned num) {
     size_t size = sizeof(uint32_t) * num;
     for (unsigned i = 0; i < num; i++) {
       uint32_t length;
       memcpy(&length, ptr, sizeof(length));
-      ptr += sizeof(uint32_t);
-      new (&items[i]) string(ptr, length);
-      ptr += length;
+      ptr = static_cast<const char*>(ptr) + sizeof(uint32_t);
+      new (&items[i]) string(static_cast<const char*>(ptr), length);
+      ptr = static_cast<const char*>(ptr) + length;
       size += length;
     }
     return size;
@@ -138,9 +138,11 @@
 ptr_with_size frequent_strings_sketch_serialize(const void* sketchptr, unsigned header_size) {
   try {
     ptr_with_size p;
-    auto data = static_cast<const frequent_strings_sketch*>(sketchptr)->serialize(header_size);
-    p.ptr = data.first.release();
-    p.size = data.second;
+    auto bytes = new (palloc(sizeof(frequent_strings_sketch::vector_bytes))) frequent_strings_sketch::vector_bytes(
+      static_cast<const frequent_strings_sketch*>(sketchptr)->serialize(header_size)
+    );
+    p.ptr = bytes->data();
+    p.size = bytes->size();;
     return p;
   } catch (std::exception& e) {
     pg_error(e.what());
diff --git a/src/hll_sketch_c_adapter.cpp b/src/hll_sketch_c_adapter.cpp
index b082d88..9d580ad 100644
--- a/src/hll_sketch_c_adapter.cpp
+++ b/src/hll_sketch_c_adapter.cpp
@@ -101,9 +101,11 @@
 ptr_with_size hll_sketch_serialize(const void* sketchptr, unsigned header_size) {
   try {
     ptr_with_size p;
-    auto data = static_cast<const hll_sketch_pg*>(sketchptr)->serialize_compact(header_size);
-    p.ptr = data.first.release();
-    p.size = data.second;
+    auto bytes = new (palloc(sizeof(hll_sketch_pg::vector_bytes))) hll_sketch_pg::vector_bytes(
+      static_cast<const hll_sketch_pg*>(sketchptr)->serialize_compact(header_size)
+    );
+    p.ptr = bytes->data();
+    p.size = bytes->size();
     return p;
   } catch (std::exception& e) {
     pg_error(e.what());
diff --git a/src/kll_float_sketch_c_adapter.cpp b/src/kll_float_sketch_c_adapter.cpp
index ef81b6d..7228b0d 100644
--- a/src/kll_float_sketch_c_adapter.cpp
+++ b/src/kll_float_sketch_c_adapter.cpp
@@ -101,9 +101,11 @@
 ptr_with_size kll_float_sketch_serialize(const void* sketchptr, unsigned header_size) {
   try {
     ptr_with_size p;
-    auto data = static_cast<const kll_float_sketch*>(sketchptr)->serialize(header_size);
-    p.ptr = data.first.release();
-    p.size = data.second;
+    auto bytes = new (palloc(sizeof(kll_float_sketch::vector_bytes))) kll_float_sketch::vector_bytes(
+      static_cast<const kll_float_sketch*>(sketchptr)->serialize(header_size)
+    );
+    p.ptr = bytes->data();
+    p.size = bytes->size();
     return p;
   } catch (std::exception& e) {
     pg_error(e.what());
@@ -131,12 +133,12 @@
 
 Datum* kll_float_sketch_get_pmf_or_cdf(const void* sketchptr, const float* split_points, unsigned num_split_points, bool is_cdf) {
   try {
-    auto ptr = is_cdf ?
+    auto array = is_cdf ?
       static_cast<const kll_float_sketch*>(sketchptr)->get_CDF(split_points, num_split_points) :
       static_cast<const kll_float_sketch*>(sketchptr)->get_PMF(split_points, num_split_points);
     Datum* pmf = (Datum*) palloc(sizeof(Datum) * (num_split_points + 1));
     for (unsigned i = 0; i < num_split_points + 1; i++) {
-      pmf[i] = pg_float8_get_datum(ptr[i]);
+      pmf[i] = pg_float8_get_datum(array[i]);
     }
     return pmf;
   } catch (std::exception& e) {
@@ -147,10 +149,10 @@
 
 Datum* kll_float_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions) {
   try {
-    auto ptr = static_cast<const kll_float_sketch*>(sketchptr)->get_quantiles(fractions, num_fractions);
+    auto array = static_cast<const kll_float_sketch*>(sketchptr)->get_quantiles(fractions, num_fractions);
     Datum* quantiles = (Datum*) palloc(sizeof(Datum) * num_fractions);
     for (unsigned i = 0; i < num_fractions; i++) {
-      quantiles[i] = pg_float4_get_datum(ptr[i]);
+      quantiles[i] = pg_float4_get_datum(array[i]);
     }
     return quantiles;
   } catch (std::exception& e) {
diff --git a/src/theta_sketch_c_adapter.cpp b/src/theta_sketch_c_adapter.cpp
index 4056759..5622c48 100644
--- a/src/theta_sketch_c_adapter.cpp
+++ b/src/theta_sketch_c_adapter.cpp
@@ -126,9 +126,11 @@
 ptr_with_size theta_sketch_serialize(const void* sketchptr, unsigned header_size) {
   try {
     ptr_with_size p;
-    auto data = static_cast<const theta_sketch_pg*>(sketchptr)->serialize(header_size);
-    p.ptr = data.first.release();
-    p.size = data.second;
+    auto bytes = new (palloc(sizeof(theta_sketch_pg::vector_bytes))) theta_sketch_pg::vector_bytes(
+      static_cast<const theta_sketch_pg*>(sketchptr)->serialize(header_size)
+    );
+    p.ptr = bytes->data();
+    p.size = bytes->size();
     return p;
   } catch (std::exception& e) {
     pg_error(e.what());