mosesdecoder/util/scoped.cc
2013-01-24 12:07:46 +00:00

36 lines
822 B
C++

#include "util/scoped.hh"
#include <cstdlib>
namespace util {
MallocException::MallocException(std::size_t requested) throw() {
*this << "for " << requested << " bytes ";
}
MallocException::~MallocException() throw() {}
void *MallocOrThrow(std::size_t requested) {
void *ret;
UTIL_THROW_IF_ARG(!(ret = std::malloc(requested)), MallocException, (requested), "in malloc");
return ret;
}
void *CallocOrThrow(std::size_t requested) {
void *ret;
UTIL_THROW_IF_ARG(!(ret = std::calloc(1, requested)), MallocException, (requested), "in calloc");
return ret;
}
scoped_malloc::~scoped_malloc() {
std::free(p_);
}
void scoped_malloc::call_realloc(std::size_t to) {
void *ret;
UTIL_THROW_IF_ARG(!(ret = std::realloc(p_, to)) && to, MallocException, (to), "in realloc");
p_ = ret;
}
} // namespace util