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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2475
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <new> |
|
29 |
|
30 #include "oct-alloc.h" |
|
31 |
|
32 void * |
|
33 octave_allocator::alloc (size_t size) |
|
34 { |
|
35 if (size != item_size) |
|
36 return ::new char [size]; |
|
37 |
|
38 if (! head) |
|
39 { |
|
40 if (! grow ()) |
|
41 return 0; |
|
42 } |
|
43 |
|
44 link *tmp = head; |
|
45 head = head->next; |
|
46 return tmp; |
|
47 } |
|
48 |
5013
|
49 // XXX FIXME XXX -- if we free the last item on the list, shouldn't we |
|
50 // also free the underlying character array used for storage? |
|
51 |
2475
|
52 void |
|
53 octave_allocator::free (void *p, size_t size) |
|
54 { |
|
55 if (size != item_size) |
2800
|
56 ::delete [] (static_cast<char *> (p)); |
2475
|
57 else |
|
58 { |
2800
|
59 link *tmp = static_cast<link *> (p); |
2475
|
60 tmp->next = head; |
|
61 head = tmp; |
|
62 } |
|
63 } |
|
64 |
|
65 // Return TRUE for successful allocation, FALSE otherwise. |
|
66 |
|
67 bool |
|
68 octave_allocator::grow (void) |
|
69 { |
|
70 bool retval = true; |
|
71 |
|
72 char *start = new char [grow_size * item_size]; |
|
73 |
|
74 if (start) |
|
75 { |
|
76 char *last = &start[(grow_size - 1) * item_size]; |
|
77 |
|
78 char *p = start; |
|
79 while (p < last) |
|
80 { |
|
81 char *next = p + item_size; |
3145
|
82 (X_CAST (link *, p)) -> next = X_CAST (link *, next); |
2475
|
83 p = next; |
|
84 } |
|
85 |
3145
|
86 (X_CAST (link *, last)) -> next = 0; |
2475
|
87 |
3145
|
88 head = X_CAST (link *, start); |
2475
|
89 } |
|
90 else |
|
91 { |
|
92 typedef void (*error_handler_function) (void); |
|
93 |
3503
|
94 error_handler_function f = std::set_new_handler (0); |
|
95 std::set_new_handler (f); |
2475
|
96 |
|
97 if (f) |
|
98 f (); |
|
99 |
|
100 retval = false; |
|
101 } |
|
102 |
|
103 return retval; |
|
104 } |
|
105 |
|
106 /* |
|
107 ;;; Local Variables: *** |
|
108 ;;; mode: C++ *** |
|
109 ;;; End: *** |
|
110 */ |