Mercurial > hg > octave-nkf
annotate liboctave/util/oct-alloc.cc @ 16660:cbb1bb7a5c3d
Added tag ss-3-7-5 for changeset 608e307b4914
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 14 May 2013 05:23:53 -0400 |
parents | 648dabbb4c6b |
children | d63878346099 |
rev | line source |
---|---|
2475 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
3 Copyright (C) 1996-2012 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2475 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2475 | 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) | |
15020
560317fd5977
maint: Cuddle open bracket used for indexing C++ arrays in source code.
Rik <rik@octave.org>
parents:
15018
diff
changeset
|
35 return ::new char [size]; |
2475 | 36 |
37 if (! head) | |
38 { | |
39 if (! grow ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
40 return 0; |
2475 | 41 } |
42 | |
43 link *tmp = head; | |
44 head = head->next; | |
45 return tmp; | |
46 } | |
47 | |
5775 | 48 // FIXME -- if we free the last item on the list, shouldn't we |
5013 | 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) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
79 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
80 char *next = p + item_size; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
81 (reinterpret_cast<link *> (p)) -> next |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
82 = reinterpret_cast<link *> (next); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
83 p = next; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
84 } |
2475 | 85 |
5760 | 86 (reinterpret_cast<link *> (last)) -> next = 0; |
2475 | 87 |
5760 | 88 head = reinterpret_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) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
98 f (); |
2475 | 99 |
100 retval = false; | |
101 } | |
102 | |
103 return retval; | |
104 } |