2475
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 2002, 2003, 2005, 2006, 2007 |
|
4 John W. Eaton |
2475
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2475
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2475
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_oct_alloc_h) |
|
25 #define octave_oct_alloc_h 1 |
|
26 |
|
27 class |
6108
|
28 OCTAVE_API |
2475
|
29 octave_allocator |
|
30 { |
|
31 public: |
|
32 |
|
33 octave_allocator (size_t item_sz, int grow_sz = 256) |
|
34 : head (0), grow_size (grow_sz), |
|
35 item_size (item_sz > sizeof (link *) ? item_sz : sizeof (link *)) |
|
36 { } |
|
37 |
|
38 // Get an object from the free list, possibly increasing the size of |
|
39 // the free list. |
|
40 void *alloc (size_t size); |
|
41 |
|
42 // Put objects back on the free list. |
|
43 void free (void *p, size_t size); |
|
44 |
|
45 private: |
|
46 |
|
47 // Structure for internal free list management. |
|
48 struct link { link *next; }; |
|
49 |
|
50 // Front of the free list. |
|
51 link *head; |
|
52 |
|
53 // How many objects to get each time we call the global operator new. |
|
54 int grow_size; |
|
55 |
|
56 // The size of each item on the list (or, if that is smaller than |
|
57 // the size of list*, the size of list*. |
|
58 size_t item_size; |
|
59 |
|
60 // How to grow the free list. |
|
61 bool grow (void); |
|
62 }; |
|
63 |
4352
|
64 #if defined (HAVE_PLACEMENT_DELETE) |
|
65 #define DECLARE_OCTAVE_ALLOCATOR_PLACEMENT_DELETE \ |
|
66 void operator delete (void *p, void *) \ |
|
67 { ::operator delete (p, static_cast<void*> (0)); } |
|
68 #else |
|
69 #define DECLARE_OCTAVE_ALLOCATOR_PLACEMENT_DELETE \ |
|
70 void operator delete (void *p, void *) \ |
|
71 { ::operator delete (p); } |
|
72 #endif |
|
73 |
3219
|
74 #define DECLARE_OCTAVE_ALLOCATOR \ |
|
75 public: \ |
4219
|
76 void *operator new (size_t size, void *p) \ |
|
77 { return ::operator new (size, p); } \ |
4352
|
78 DECLARE_OCTAVE_ALLOCATOR_PLACEMENT_DELETE \ |
3219
|
79 void *operator new (size_t size) { return allocator.alloc (size); } \ |
|
80 void operator delete (void *p, size_t size) { allocator.free (p, size); } \ |
|
81 private: \ |
|
82 static octave_allocator allocator; |
|
83 |
|
84 #define DEFINE_OCTAVE_ALLOCATOR(t) \ |
|
85 octave_allocator t::allocator (sizeof (t)) |
|
86 |
|
87 #define DEFINE_OCTAVE_ALLOCATOR2(t, s) \ |
|
88 octave_allocator t::allocator (sizeof (t), s) |
|
89 |
2475
|
90 #endif |