2475
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2475
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2475
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <new> |
|
32 |
|
33 #include "oct-alloc.h" |
|
34 |
|
35 void * |
|
36 octave_allocator::alloc (size_t size) |
|
37 { |
|
38 if (size != item_size) |
|
39 return ::new char [size]; |
|
40 |
|
41 if (! head) |
|
42 { |
|
43 if (! grow ()) |
|
44 return 0; |
|
45 } |
|
46 |
|
47 link *tmp = head; |
|
48 head = head->next; |
|
49 return tmp; |
|
50 } |
|
51 |
5013
|
52 // XXX FIXME XXX -- if we free the last item on the list, shouldn't we |
|
53 // also free the underlying character array used for storage? |
|
54 |
2475
|
55 void |
|
56 octave_allocator::free (void *p, size_t size) |
|
57 { |
|
58 if (size != item_size) |
2800
|
59 ::delete [] (static_cast<char *> (p)); |
2475
|
60 else |
|
61 { |
2800
|
62 link *tmp = static_cast<link *> (p); |
2475
|
63 tmp->next = head; |
|
64 head = tmp; |
|
65 } |
|
66 } |
|
67 |
|
68 // Return TRUE for successful allocation, FALSE otherwise. |
|
69 |
|
70 bool |
|
71 octave_allocator::grow (void) |
|
72 { |
|
73 bool retval = true; |
|
74 |
|
75 char *start = new char [grow_size * item_size]; |
|
76 |
|
77 if (start) |
|
78 { |
|
79 char *last = &start[(grow_size - 1) * item_size]; |
|
80 |
|
81 char *p = start; |
|
82 while (p < last) |
|
83 { |
|
84 char *next = p + item_size; |
3145
|
85 (X_CAST (link *, p)) -> next = X_CAST (link *, next); |
2475
|
86 p = next; |
|
87 } |
|
88 |
3145
|
89 (X_CAST (link *, last)) -> next = 0; |
2475
|
90 |
3145
|
91 head = X_CAST (link *, start); |
2475
|
92 } |
|
93 else |
|
94 { |
|
95 typedef void (*error_handler_function) (void); |
|
96 |
3503
|
97 error_handler_function f = std::set_new_handler (0); |
|
98 std::set_new_handler (f); |
2475
|
99 |
|
100 if (f) |
|
101 f (); |
|
102 |
|
103 retval = false; |
|
104 } |
|
105 |
|
106 return retval; |
|
107 } |
|
108 |
|
109 /* |
|
110 ;;; Local Variables: *** |
|
111 ;;; mode: C++ *** |
|
112 ;;; End: *** |
|
113 */ |