メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

oct-alloc.cc

解説を見る。
00001 /*
00002 
00003 Copyright (C) 1996, 1997 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 2, or (at your option) any
00010 later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, write to the Free
00019 Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 */
00022 
00023 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION)
00024 #pragma implementation
00025 #endif
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030 
00031 #include <new>
00032 
00033 #include "oct-alloc.h"
00034 
00035 void *
00036 octave_allocator::alloc (size_t size)
00037 {
00038   if (size != item_size)
00039     return ::new char [size];
00040 
00041   if (! head)
00042     {
00043       if (! grow ())
00044         return 0;
00045     }
00046 
00047   link *tmp = head;
00048   head = head->next;
00049   return tmp;
00050 }
00051 
00052 // XXX FIXME XXX -- if we free the last item on the list, shouldn't we
00053 // also free the underlying character array used for storage?
00054 
00055 void
00056 octave_allocator::free (void *p, size_t size)
00057 {
00058   if (size != item_size)
00059     ::delete [] (static_cast<char *> (p));
00060   else
00061     {
00062       link *tmp = static_cast<link *> (p);
00063       tmp->next = head;
00064       head = tmp;
00065     }
00066 }
00067 
00068 // Return TRUE for successful allocation, FALSE otherwise.
00069 
00070 bool
00071 octave_allocator::grow (void)
00072 {
00073   bool retval = true;
00074 
00075   char *start = new char [grow_size * item_size];
00076 
00077   if (start)
00078     {
00079       char *last = &start[(grow_size - 1) * item_size];
00080 
00081       char *p = start;
00082       while (p < last)
00083         {
00084           char *next = p + item_size;
00085           (X_CAST (link *, p)) -> next = X_CAST (link *, next);
00086           p = next;
00087         }
00088 
00089       (X_CAST (link *, last)) -> next = 0;
00090 
00091       head = X_CAST (link *, start);
00092     }
00093   else
00094     {
00095       typedef void (*error_handler_function) (void);
00096 
00097       error_handler_function f = std::set_new_handler (0);
00098       std::set_new_handler (f);
00099 
00100       if (f)
00101         f ();
00102 
00103       retval = false;
00104     }
00105 
00106   return retval;
00107 }
00108 
00109 /*
00110 ;;; Local Variables: ***
00111 ;;; mode: C++ ***
00112 ;;; End: ***
00113 */

Wed Dec 29 11:52:06 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送