mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-12-29 15:04:05 +03:00
36 lines
822 B
C++
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
|