Commit e289a805 by whitequark Committed by Miodrag Milanovic

Add WASI platform support to bsat2 and glucose.

Abort on OOM since there are no C++ exceptions yet.

Signed-off-by: Miodrag Milanovic <mmicko@gmail.com>
parent d2d6bbd9
...@@ -97,7 +97,11 @@ void RegionAllocator<T>::capacity(uint32_t min_cap) ...@@ -97,7 +97,11 @@ void RegionAllocator<T>::capacity(uint32_t min_cap)
cap += delta; cap += delta;
if (cap <= prev_cap) if (cap <= prev_cap)
#ifdef __wasm
abort();
#else
throw OutOfMemoryException(); throw OutOfMemoryException();
#endif
} }
// printf(" .. (%p) cap = %u\n", this, cap); // printf(" .. (%p) cap = %u\n", this, cap);
...@@ -119,7 +123,11 @@ RegionAllocator<T>::alloc(int size) ...@@ -119,7 +123,11 @@ RegionAllocator<T>::alloc(int size)
// Handle overflow: // Handle overflow:
if (sz < prev_sz) if (sz < prev_sz)
#ifdef __wasm
abort();
#else
throw OutOfMemoryException(); throw OutOfMemoryException();
#endif
return prev_sz; return prev_sz;
} }
......
...@@ -97,7 +97,11 @@ void vec<T>::capacity(int min_cap) { ...@@ -97,7 +97,11 @@ void vec<T>::capacity(int min_cap) {
if (cap >= min_cap) return; if (cap >= min_cap) return;
int add = imax((min_cap - cap + 1) & ~1, ((cap >> 1) + 2) & ~1); // NOTE: grow by approximately 3/2 int add = imax((min_cap - cap + 1) & ~1, ((cap >> 1) + 2) & ~1); // NOTE: grow by approximately 3/2
if (add > INT_MAX - cap || (((data = (T*)::realloc(data, (cap += add) * sizeof(T))) == NULL) && errno == ENOMEM)) if (add > INT_MAX - cap || (((data = (T*)::realloc(data, (cap += add) * sizeof(T))) == NULL) && errno == ENOMEM))
#ifdef __wasm
abort();
#else
throw OutOfMemoryException(); throw OutOfMemoryException();
#endif
} }
......
...@@ -34,7 +34,11 @@ static inline void* xrealloc(void *ptr, size_t size) ...@@ -34,7 +34,11 @@ static inline void* xrealloc(void *ptr, size_t size)
{ {
void* mem = realloc(ptr, size); void* mem = realloc(ptr, size);
if (mem == NULL && errno == ENOMEM){ if (mem == NULL && errno == ENOMEM){
#ifdef __wasm
abort();
#else
throw OutOfMemoryException(); throw OutOfMemoryException();
#endif
}else }else
return mem; return mem;
} }
......
...@@ -100,7 +100,11 @@ void RegionAllocator<T>::capacity(uint32_t min_cap) ...@@ -100,7 +100,11 @@ void RegionAllocator<T>::capacity(uint32_t min_cap)
cap += delta; cap += delta;
if (cap <= prev_cap) if (cap <= prev_cap)
#ifdef __wasm
abort();
#else
throw OutOfMemoryException(); throw OutOfMemoryException();
#endif
} }
//printf(" .. (%p) cap = %u\n", this, cap); //printf(" .. (%p) cap = %u\n", this, cap);
...@@ -122,7 +126,11 @@ RegionAllocator<T>::alloc(int size) ...@@ -122,7 +126,11 @@ RegionAllocator<T>::alloc(int size)
// Handle overflow: // Handle overflow:
if (sz < prev_sz) if (sz < prev_sz)
#ifdef __wasm
abort();
#else
throw OutOfMemoryException(); throw OutOfMemoryException();
#endif
return prev_sz; return prev_sz;
} }
......
...@@ -100,7 +100,11 @@ void vec<T>::capacity(int min_cap) { ...@@ -100,7 +100,11 @@ void vec<T>::capacity(int min_cap) {
if (cap >= min_cap) return; if (cap >= min_cap) return;
int add = imax((min_cap - cap + 1) & ~1, ((cap >> 1) + 2) & ~1); // NOTE: grow by approximately 3/2 int add = imax((min_cap - cap + 1) & ~1, ((cap >> 1) + 2) & ~1); // NOTE: grow by approximately 3/2
if (add > INT_MAX - cap || (((data = (T*)::realloc(data, (cap += add) * sizeof(T))) == NULL) && errno == ENOMEM)) if (add > INT_MAX - cap || (((data = (T*)::realloc(data, (cap += add) * sizeof(T))) == NULL) && errno == ENOMEM))
#ifdef __wasm
abort();
#else
throw OutOfMemoryException(); throw OutOfMemoryException();
#endif
} }
......
...@@ -39,7 +39,11 @@ static inline void* xrealloc(void *ptr, size_t size) ...@@ -39,7 +39,11 @@ static inline void* xrealloc(void *ptr, size_t size)
{ {
void* mem = realloc(ptr, size); void* mem = realloc(ptr, size);
if (mem == NULL && errno == ENOMEM){ if (mem == NULL && errno == ENOMEM){
#ifdef __wasm
abort();
#else
throw OutOfMemoryException(); throw OutOfMemoryException();
#endif
}else { }else {
return mem; return mem;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment