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 #if !defined (octave_oct_alloc_h) |
|
24 #define octave_oct_alloc_h 1 |
|
25 |
|
26 class |
|
27 octave_allocator |
|
28 { |
|
29 public: |
|
30 |
|
31 octave_allocator (size_t item_sz, int grow_sz = 256) |
|
32 : head (0), grow_size (grow_sz), |
|
33 item_size (item_sz > sizeof (link *) ? item_sz : sizeof (link *)) |
|
34 { } |
|
35 |
|
36 // Get an object from the free list, possibly increasing the size of |
|
37 // the free list. |
|
38 void *alloc (size_t size); |
|
39 |
|
40 // Put objects back on the free list. |
|
41 void free (void *p, size_t size); |
|
42 |
|
43 private: |
|
44 |
|
45 // Structure for internal free list management. |
|
46 struct link { link *next; }; |
|
47 |
|
48 // Front of the free list. |
|
49 link *head; |
|
50 |
|
51 // How many objects to get each time we call the global operator new. |
|
52 int grow_size; |
|
53 |
|
54 // The size of each item on the list (or, if that is smaller than |
|
55 // the size of list*, the size of list*. |
|
56 size_t item_size; |
|
57 |
|
58 // How to grow the free list. |
|
59 bool grow (void); |
|
60 }; |
|
61 |
3219
|
62 #define DECLARE_OCTAVE_ALLOCATOR \ |
|
63 public: \ |
|
64 void *operator new (size_t size) { return allocator.alloc (size); } \ |
|
65 void operator delete (void *p, size_t size) { allocator.free (p, size); } \ |
|
66 private: \ |
|
67 static octave_allocator allocator; |
|
68 |
|
69 #define DEFINE_OCTAVE_ALLOCATOR(t) \ |
|
70 octave_allocator t::allocator (sizeof (t)) |
|
71 |
|
72 #define DEFINE_OCTAVE_ALLOCATOR2(t, s) \ |
|
73 octave_allocator t::allocator (sizeof (t), s) |
|
74 |
2475
|
75 #endif |
|
76 |
|
77 /* |
|
78 ;;; Local Variables: *** |
|
79 ;;; mode: C++ *** |
|
80 ;;; End: *** |
|
81 */ |