root/juggler/tags/1.0.5/SharedMem/vjMemPool_malloc.h

Revision 7539, 7.9 kB (checked in by anonymous, 7 years ago)

This commit was manufactured by cvs2svn to create tag
'RELENG_1_0_5_RELEASE'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * -----------------------------------------------------------------
26  * File:          $RCSfile$
27  * Date modified: $Date$
28  * Version:       $Revision$
29  * -----------------------------------------------------------------
30  *
31  *************** <auto-copyright.pl END do not edit this line> ***************/
32
33
34 /*
35  * --------------------------------------------------------------------------
36  * Author:
37  *   Patrick Hartling
38  * --------------------------------------------------------------------------
39  * NOTES:
40  *    - It is *required* that one of the following be used depending on the
41  *      platform:
42  *         HP-UX 10.20 --> -DVJ_OS_HPUX
43  *         IRIX 6.x    --> -DVJ_OS_IRIX
44  * --------------------------------------------------------------------------
45  */
46
47 #ifndef _VJ_MEM_POOL_MALLOC_H_
48 #define _VJ_MEM_POOL_MALLOC_H_
49
50 #include <vjConfig.h>
51 #include <sys/types.h>
52
53 #include <Kernel/vjDebug.h>
54
55
56 //: Shared memory pool for multi-threaded code.
57 //
58 // It is used by vjMemory (base class) to control allocation and deallocation
59 // from a "memory pool."  This implementation is what must be used on HP-UX
60 // (at least for now).
61 //
62 // Clients should create memory pools as needed.  Then when objects are
63 // created, they can pass a pool as a parameter to the new (if the object
64 // is a derived from vjMemory).
65
66 class vjMemPool_malloc : public vjMemPool {
67 public:
68     // -----------------------------------------------------------------------
69     //: Constructor for vjMemPool_malloc class.
70     //
71     //! PRE: None.
72     //! POST: None (for now).
73     //
74     //! ARGS: initialSize - The initial size of the arena.  (Not currently
75     //+                     used.)
76     //! ARGS: numProcs - The number of threads that will share this arena.
77     //+                  (Not currently used.)
78     //! ARGS: staticTempName - The name of the arena file.  (Not currently
79     //+                        used.)
80     // -----------------------------------------------------------------------
81     vjMemPool_malloc (size_t initialSize = 65536,  int numProcs = 8,
82                       char* staticTempName = "/var/tmp/memPool_mallocXXXXXX")
83     {
84         vjDEBUG(vjDBG_ALL,3)
85             << "vjMemPool_malloc: Allocating arena ("
86             << initialSize << " bytes)\n" << vjDEBUG_FLUSH;
87     }
88
89     // -----------------------------------------------------------------------
90     //: Destructor for vjMemPool_malloc class.
91     //
92     //! PRE: None.
93     //! POST: None (for now).
94     // -----------------------------------------------------------------------
95     virtual
96     ~vjMemPool_malloc () {
97         vjDEBUG(vjDBG_ALL,6) << "vjMemPool_malloc::~vjMemPool_malloc() entered\n"
98                    << vjDEBUG_FLUSH;
99     }
100
101     // -----------------------------------------------------------------------
102     //: Allocate a chunk of memory of the specified size.
103     //
104     //! PRE: None.
105     //! POST: A piece of memory is returned to the caller that, in a
106     //+       multi-threaded environment, can be shared among threads.
107     //
108     //! ARGS: size - The size of the memory block to allocate.
109     //
110     //! RETURNS: pointer - A pointer to the newly allocated memory.
111     // -----------------------------------------------------------------------
112     virtual void*
113     allocate (size_t size) {
114         void* retval;
115
116         retval = malloc(size);
117
118         if ( retval == NULL ) {
119             std::cerr << "vjMemPool_malloc: Out of memory!!!\n";
120         }
121
122         return retval;
123     }
124
125     // -----------------------------------------------------------------------
126     //: Deallocate a chunk of user-specified memory.
127     //
128     //! PRE: None.
129     //! POST: The memory pointed to by ptr is freed.
130     //
131     //! ARGS: ptr - The chunk of memory to be deallocated.
132     // -----------------------------------------------------------------------
133     inline virtual void
134     deallocate (void* ptr) {
135         free(ptr);
136     }
137
138     // -----------------------------------------------------------------------
139     //: Reallocate a specified chunk of memory using the given size.
140     //
141     //! PRE: None.
142     //! POST: The memory pointed to by ptr is reallocated at the given
143     //+       size and returned to the caller.
144     //
145     //! ARGS: ptr - The chunk of memory to be reallocated.
146     //! ARGS: new_size - The new size to be reallocated.
147     //
148     //! RETURNS: pointer - A pointer to the newly reallocated memory.
149     // -----------------------------------------------------------------------
150     virtual void*
151     reallocate (void* ptr, size_t new_size) {
152         void* retval;
153
154         retval = realloc(ptr, new_size);
155
156         if ( retval == NULL ) {
157             std::cerr << "vjMemPool_malloc: Out of memory!!!\n";
158         }
159
160         return ptr;
161     }
162
163     // Functions that initialize, allocate and deallocate memory from the
164     // class-wide memory "arena."
165
166     // -----------------------------------------------------------------------
167     //: Initialize the class-wide arena.
168     //
169     //! PRE: None.
170     //! POST: None (for now).
171     //
172     //! ARGS: initialSize - The initial size of the arena.  (Not currently
173     //+                     used.)
174     //! ARGS: numProcs - The number of threads that will share this arena.
175     //+                  (Not currently used.)
176     //! ARGS: staticTempName - The name of the arena file.  (Not currently
177     //+                        used.)
178     // -----------------------------------------------------------------------
179     static void
180     init (size_t initialSize = 32768, int numProcs = 64,
181           char* staticTempName = "/var/tmp/memPoolsArenaXXXXXX")
182     {
183         vjDEBUG(vjDBG_ALL,3)
184             << "\nvjMemPool_malloc: Allocating Base Arena for ALL "
185             << "vjMemPool_malloc's.\n  "
186             << initialSize << " bytes, "
187             << numProcs << " procs"
188             << "\n" << vjDEBUG_FLUSH;
189     }
190
191     // -----------------------------------------------------------------------
192     //: Allocate a new piece of memory of the given size.
193     //
194     //! PRE: None.
195     //! POST: A piece of memory is returned to the caller that, in a
196     //+       multi-threaded environment, can be shared among threads.
197     //
198     //! ARGS: size - The size of the memory block to allocate.
199     // -----------------------------------------------------------------------
200     void*
201     operator new (size_t size) {
202         vjDEBUG(vjDBG_ALL,6) << "vjMemPool_malloc::new called.\n"
203                              << vjDEBUG_FLUSH;
204
205         init();
206
207         return malloc(sizeof(vjMemPool_malloc));
208     }
209
210     // -----------------------------------------------------------------------
211     //: Delete a chunk of user-specified memory.
212     //
213     //! PRE: None.
214     //! POST: The memory pointed to by ptr is freed.
215     //
216     //! ARGS: ptr - A pointer to previously allocated memory.
217     // -----------------------------------------------------------------------
218     void
219     operator delete (void* ptr) {
220         free(ptr);
221     }
222 };
223
224 #endif /* _VJ_MEM_POOL_MALLOC_H_ */
Note: See TracBrowser for help on using the browser.