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

Revision 2828, 4.1 kB (checked in by patrickh, 8 years ago)

Updated the copyright to what ISU's lawyers decided they want now.
The vast majority of this was done using Kevin's auto-copyright.pl script
which definitely made this easier. All the copyright blocks now have
begin and end tags so that if and when we have to update the copyright
information again, it will be even simpler.

  • 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 // This file defines an STL allocator that works with
35 // vjMemPools.
36
37 #ifndef _MEMPOOL_ALLOC_H_
38 #define _MEMPOOL_ALLOC_H_
39
40 #include <vjConfig.h>
41 #include <alloc.h>
42 #include <SharedMem/vjMemPool.h>
43
44
45 // Malloc-based allocator.  Typically slower than default alloc below.
46 // Typically thread-safe and more storage efficient.
47 #ifdef __STL_STATIC_TEMPLATE_MEMBER_BUG
48 # ifdef __DECLARE_GLOBALS_HERE
49     void (* __mempool_alloc_oom_handler)() = 0;
50     // g++ 2.7.2 does not handle static template data members.
51 # else
52     extern void (* __mempool_alloc_oom_handler)();
53 # endif
54 #endif
55
56 template <int inst>
57 class __vj_mempool_alloc_template
58 {
59
60 private:
61
62    static void *oom_malloc(size_t);
63
64    static void *oom_realloc(void *, size_t);
65
66    #ifndef __STL_STATIC_TEMPLATE_MEMBER_BUG
67    static void (* __mempool_alloc_oom_handler)();
68    #endif
69
70 public:
71
72    static void * allocate(size_t n)
73    {
74       verifyMempoolAllocated();
75       void *result = STLMemPool->allocate(n);
76       if (0 == result) result = oom_malloc(n);
77       return result;
78    }
79
80    static void deallocate(void *p, size_t /* n */)
81    {
82       STLMemPool->deallocate(p);
83    }
84
85    static void * reallocate(void *p, size_t /* old_sz */, size_t new_sz)
86    {
87       void * result = STLMemPool->reallocate(p, new_sz);
88       if (0 == result) result = oom_realloc(p, new_sz);
89       return result;
90    }
91
92    static void (* set_malloc_handler(void (*f)()))()
93    {
94       void (* old)() = __mempool_alloc_oom_handler;
95       __mempool_alloc_oom_handler = f;
96       return (old);
97    }
98
99 public:
100
101    static void verifyMempoolAllocated()
102    {
103       if (STLMemPool == NULL)      // ASSERT: No allocated
104       {
105          STLMemPool = new vjSharedPool(1024*1024);
106       }
107    }
108
109    static vjMemPool* STLMemPool;   
110 };
111
112 MemPool* __vj_mempool_alloc_template<0>::STLMemPool = NULL;
113
114 // mempool_alloc out-of-memory handling
115
116 #ifndef __STL_STATIC_TEMPLATE_MEMBER_BUG
117 template <int inst>
118 void (* __vj_mempool_alloc_template<inst>::__mempool_alloc_oom_handler)() = 0;
119 #endif
120
121 template <int inst>
122 void * __vj_mempool_alloc_template<inst>::oom_malloc(size_t n)
123 {
124     void (* my_malloc_handler)();
125     void *result;
126
127     for (;;) {
128         my_malloc_handler = __mempool_alloc_oom_handler;
129         if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }
130         (*my_malloc_handler)();
131         result = malloc(n);
132         if (result) return(result);
133     }
134 }
135
136 template <int inst>
137 void * __vj_mempool_alloc_template<inst>::oom_realloc(void *p, size_t n)
138 {
139     void (* my_malloc_handler)();
140     void *result;
141
142     for (;;) {
143         my_malloc_handler = __mempool_alloc_oom_handler;
144         if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }
145         (*my_malloc_handler)();
146         result = realloc(p, n);
147         if (result) return(result);
148     }
149 }
150
151 typedef __vj_mempool_alloc_template<0> mempool_alloc;
152
153 #endif  // _MEMPOOL_ALLOC_H_
Note: See TracBrowser for help on using the browser.