00001 #include "util/scoped.hh"
00002
00003 #include <cstdlib>
00004 #if !defined(_WIN32) && !defined(_WIN64)
00005 #include <sys/mman.h>
00006 #endif
00007
00008 namespace util {
00009
00010
00011
00012 MallocException::MallocException(std::size_t requested) throw() {
00013 *this << "for " << requested << " bytes ";
00014 }
00015
00016 MallocException::~MallocException() throw() {}
00017
00018 namespace {
00019 void *InspectAddr(void *addr, std::size_t requested, const char *func_name) {
00020 UTIL_THROW_IF_ARG(!addr && requested, MallocException, (requested), "in " << func_name);
00021 return addr;
00022 }
00023 }
00024
00025 void *MallocOrThrow(std::size_t requested) {
00026 return InspectAddr(std::malloc(requested), requested, "malloc");
00027 }
00028
00029 void *CallocOrThrow(std::size_t requested) {
00030 return InspectAddr(std::calloc(requested, 1), requested, "calloc");
00031 }
00032
00033 void scoped_malloc::call_realloc(std::size_t requested) {
00034 p_ = InspectAddr(std::realloc(p_, requested), requested, "realloc");
00035 }
00036
00037 void AdviseHugePages(const void *addr, std::size_t size) {
00038 #if MADV_HUGEPAGE
00039 madvise((void*)addr, size, MADV_HUGEPAGE);
00040 #endif
00041 }
00042
00043 }