memory.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2008 Soeren Sonnenburg
00008  * Copyright (C) 2008 Fraunhofer Institute FIRST and Max-Planck-Society
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 }

SHOGUN Machine Learning Toolbox - Documentation