summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2019-01-09 13:05:08 -0800
committerGitHub <noreply@github.com>2019-01-09 13:05:08 -0800
commit5f114452cd73fcad861660b2b715af726c925084 (patch)
tree3744e9aa60e25d8a4b0853ab027ff18181c0832a /src
parentaf5adae23e7cfcc2c933d72142a3d58576af769d (diff)
downloadbinaryen-5f114452cd73fcad861660b2b715af726c925084.tar.gz
binaryen-5f114452cd73fcad861660b2b715af726c925084.tar.bz2
binaryen-5f114452cd73fcad861660b2b715af726c925084.zip
Aligned allocation fixes. Fixes #1845 (#1846)
The error in #1845 shows: /<<PKGBUILDDIR>>/src/mixed_arena.h: In member function 'void* MixedArena::allocSpace(size_t, size_t)': /<<PKGBUILDDIR>>/src/mixed_arena.h:125:43: error: 'new' of type 'MixedArena::Chunk' {aka 'std::aligned_storage<32768, 16>::type'} with extended alignment 16 [-Werror=aligned-new=] chunks.push_back(new Chunk[numChunks]); ^ /<<PKGBUILDDIR>>/src/mixed_arena.h:125:43: note: uses 'void* operator new [](std::size_t)', which does not have an alignment parameter /<<PKGBUILDDIR>>/src/mixed_arena.h:125:43: note: use '-faligned-new' to enable C++17 over-aligned new support It turns out I had misread the aligned_storage docs, and they don't actually do what we need, which is a convenient cross-platform way to do aligned allocation, since new itself doesn't support that. Sadly it seems there is no cross-platform way to do it right now, so I added a header in support which abstracts over the windows and everything-else ways. Also add some ctest testing, which runs on windows, so we get basic windows coverage in our CI.
Diffstat (limited to 'src')
-rw-r--r--src/mixed_arena.h17
-rw-r--r--src/support/alloc.h55
2 files changed, 64 insertions, 8 deletions
diff --git a/src/mixed_arena.h b/src/mixed_arena.h
index 4c62514d1..46487b7fc 100644
--- a/src/mixed_arena.h
+++ b/src/mixed_arena.h
@@ -19,13 +19,14 @@
#include <atomic>
#include <cassert>
-#include <cstdlib>
#include <memory>
#include <mutex>
#include <thread>
#include <type_traits>
#include <vector>
+#include <support/alloc.h>
+
//
// Arena allocation for mixed-type data.
//
@@ -63,11 +64,9 @@ struct MixedArena {
static const size_t CHUNK_SIZE = 32768;
static const size_t MAX_ALIGN = 16; // allow 128bit SIMD
- typedef std::aligned_storage<CHUNK_SIZE, MAX_ALIGN>::type Chunk;
-
- // Each pointer in chunks is to an array of Chunk structs; typically 1,
+ // Each pointer in chunks is to a multiple of CHUNK_SIZE - typically 1,
// but possibly more.
- std::vector<Chunk*> chunks;
+ std::vector<void*> chunks;
size_t index = 0; // in last chunk
@@ -122,10 +121,12 @@ struct MixedArena {
// Allocate a new chunk.
auto numChunks = (size + CHUNK_SIZE - 1) / CHUNK_SIZE;
assert(size <= numChunks * CHUNK_SIZE);
- chunks.push_back(new Chunk[numChunks]);
+ auto* allocation = wasm::aligned_malloc(MAX_ALIGN, numChunks * CHUNK_SIZE);
+ if (!allocation) abort();
+ chunks.push_back(allocation);
index = 0;
}
- uint8_t* ret = static_cast<uint8_t*>(static_cast<void*>(chunks.back()));
+ uint8_t* ret = static_cast<uint8_t*>(chunks.back());
ret += index;
index += size; // TODO: if we allocated more than 1 chunk, reuse the remainder, right now we allocate another next time
return static_cast<void*>(ret);
@@ -141,7 +142,7 @@ struct MixedArena {
void clear() {
for (auto* chunk : chunks) {
- delete[] chunk;
+ wasm::aligned_free(chunk);
}
chunks.clear();
}
diff --git a/src/support/alloc.h b/src/support/alloc.h
new file mode 100644
index 000000000..86c49d2f5
--- /dev/null
+++ b/src/support/alloc.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2019 WebAssembly Community Group participants
+ *
+ * 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.
+ */
+
+//
+// Allocation helpers
+//
+
+#ifndef wasm_support_alloc_h
+#define wasm_support_alloc_h
+
+#include <stdlib.h>
+
+#if defined(WIN32) || defined(_WIN32)
+#include <malloc.h>
+#endif
+
+namespace wasm {
+
+// An allocation of a specific size and a minimum alignment. Must be freed
+// with aligned_free. Returns nullptr on failure.
+inline void* aligned_malloc(size_t align, size_t size) {
+#if defined(WIN32) || defined(_WIN32)
+ _set_errno(0);
+ void* ret = _aligned_malloc(size, align);
+ if (errno == ENOMEM) ret = nullptr;
+ return ret;
+#else
+ return aligned_alloc(align, size);
+#endif
+}
+
+inline void aligned_free(void* ptr) {
+#if defined(WIN32) || defined(_WIN32)
+ _aligned_free(ptr);
+#else
+ free(ptr);
+#endif
+}
+
+} // namespace wasm
+
+#endif // wasm_support_alloc_h