memory.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012
00013 #include "lib/ShogunException.h"
00014 #include "lib/memory.h"
00015
00016
00017 void* operator new(size_t size) throw (std::bad_alloc)
00018 {
00019 void *p=malloc(size);
00020 if (!p)
00021 {
00022 const size_t buf_len=128;
00023 char buf[buf_len];
00024 size_t written=snprintf(buf, buf_len,
00025 "Out of memory error, tried to allocate %lld bytes using new().\n", (unsigned long long int) size);
00026 if (written<buf_len)
00027 throw ShogunException(buf);
00028 else
00029 throw ShogunException("Out of memory error using new.\n");
00030 }
00031
00032 return p;
00033 }
00034
00035 void operator delete(void *p)
00036 {
00037 if (p)
00038 free(p);
00039 }
00040
00041 void* operator new[](size_t size)
00042 {
00043 void *p=malloc(size);
00044
00045 if (!p)
00046 {
00047 const size_t buf_len=128;
00048 char buf[buf_len];
00049 size_t written=snprintf(buf, buf_len,
00050 "Out of memory error, tried to allocate %lld bytes using new[].\n", (unsigned long long int) size);
00051 if (written<buf_len)
00052 throw ShogunException(buf);
00053 else
00054 throw ShogunException("Out of memory error using new[].\n");
00055 }
00056
00057 return p;
00058 }
00059
00060 void operator delete[](void *p)
00061 {
00062 if (p)
00063 free(p);
00064 }