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 #include "lib/common.h"
00016
00017
00018 void* operator new(size_t size) throw (std::bad_alloc)
00019 {
00020 void *p=malloc(size);
00021 if (!p)
00022 {
00023 const size_t buf_len=128;
00024 char buf[buf_len];
00025 size_t written=snprintf(buf, buf_len,
00026 "Out of memory error, tried to allocate %lld bytes using new().\n", (long long int) size);
00027 if (written<buf_len)
00028 throw ShogunException(buf);
00029 else
00030 throw ShogunException("Out of memory error using new.\n");
00031 }
00032
00033 return p;
00034 }
00035
00036 void operator delete(void *p)
00037 {
00038 if (p)
00039 free(p);
00040 }
00041
00042 void* operator new[](size_t size)
00043 {
00044 void *p=malloc(size);
00045
00046 if (!p)
00047 {
00048 const size_t buf_len=128;
00049 char buf[buf_len];
00050 size_t written=snprintf(buf, buf_len,
00051 "Out of memory error, tried to allocate %lld bytes using new[].\n", (long long int) size);
00052 if (written<buf_len)
00053 throw ShogunException(buf);
00054 else
00055 throw ShogunException("Out of memory error using new[].\n");
00056 }
00057
00058 return p;
00059 }
00060
00061 void operator delete[](void *p)
00062 {
00063 if (p)
00064 free(p);
00065 }