noise: Throw exception on noise allocation failure

This commit is contained in:
kwolekr 2014-11-29 16:50:18 -05:00
parent a3e019c4f6
commit 25945dc539
3 changed files with 38 additions and 9 deletions

View File

@ -125,6 +125,18 @@ public:
Some "old-style" interrupts: Some "old-style" interrupts:
*/ */
class InvalidNoiseParamsException : public BaseException {
public:
InvalidNoiseParamsException():
BaseException("One or more noise parameters were invalid or require "
"too much memory")
{}
InvalidNoiseParamsException(const std::string &s):
BaseException(s)
{}
};
class InvalidPositionException : public BaseException class InvalidPositionException : public BaseException
{ {
public: public:

View File

@ -29,6 +29,7 @@
#include <string.h> // memset #include <string.h> // memset
#include "debug.h" #include "debug.h"
#include "util/numeric.h" #include "util/numeric.h"
#include "exceptions.h"
#define NOISE_MAGIC_X 1619 #define NOISE_MAGIC_X 1619
#define NOISE_MAGIC_Y 31337 #define NOISE_MAGIC_Y 31337
@ -336,8 +337,12 @@ Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz)
this->noisebuf = NULL; this->noisebuf = NULL;
resizeNoiseBuf(sz > 1); resizeNoiseBuf(sz > 1);
this->buf = new float[sx * sy * sz]; try {
this->result = new float[sx * sy * sz]; this->buf = new float[sx * sy * sz];
this->result = new float[sx * sy * sz];
} catch (std::bad_alloc &e) {
throw InvalidNoiseParamsException();
}
} }
@ -360,8 +365,12 @@ void Noise::setSize(int sx, int sy, int sz)
delete[] buf; delete[] buf;
delete[] result; delete[] result;
this->buf = new float[sx * sy * sz]; try {
this->result = new float[sx * sy * sz]; this->buf = new float[sx * sy * sz];
this->result = new float[sx * sy * sz];
} catch (std::bad_alloc &e) {
throw InvalidNoiseParamsException();
}
} }
@ -399,7 +408,11 @@ void Noise::resizeNoiseBuf(bool is3d)
if (noisebuf) if (noisebuf)
delete[] noisebuf; delete[] noisebuf;
noisebuf = new float[nlx * nly * nlz]; try {
noisebuf = new float[nlx * nly * nlz];
} catch (std::bad_alloc &e) {
throw InvalidNoiseParamsException();
}
} }

View File

@ -231,7 +231,11 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
} }
LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size) { LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size) {
noise = new Noise(np, seed, size.X, size.Y, size.Z); try {
noise = new Noise(np, seed, size.X, size.Y, size.Z);
} catch (InvalidNoiseParamsException &e) {
throw LuaError(e.what());
}
} }
LuaPerlinNoiseMap::~LuaPerlinNoiseMap() LuaPerlinNoiseMap::~LuaPerlinNoiseMap()