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