root/juggler/branches/1.0/SharedMem/vjMemPool_alloc.h

Revision 8789, 4.1 kB (checked in by patrickh, 7 years ago)

Copyright update.

  • 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, 2001, 2002
4  *   by Iowa State University
5  *
6  * Original Authors:
7  *   Allen Bierbaum, Christopher Just,
8  *   Patrick Hartling, Kevin Meinert,
9  *   Carolina Cruz-Neira, Albert Baker
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * -----------------------------------------------------------------
27  * File:          $RCSfile$
28  * Date modified: $Date$
29  * Version:       $Revision$
30  * -----------------------------------------------------------------
31  *
32  *************** <auto-copyright.pl END do not edit this line> ***************/
33
34
35 // This file defines an STL allocator that works with
36 // vjMemPools.
37
38 #ifndef _MEMPOOL_ALLOC_H_
39 #define _MEMPOOL_ALLOC_H_
40
41 #include <vjConfig.h>
42 #include <alloc.h>
43 #include <SharedMem/vjMemPool.h>
44
45
46 // Malloc-based allocator.  Typically slower than default alloc below.
47 // Typically thread-safe and more storage efficient.
48 #ifdef __STL_STATIC_TEMPLATE_MEMBER_BUG
49 # ifdef __DECLARE_GLOBALS_HERE
50     void (* __mempool_alloc_oom_handler)() = 0;
51     // g++ 2.7.2 does not handle static template data members.
52 # else
53     extern void (* __mempool_alloc_oom_handler)();
54 # endif
55 #endif
56
57 template <int inst>
58 class __vj_mempool_alloc_template
59 {
60
61 private:
62
63    static void *oom_malloc(size_t);
64
65    static void *oom_realloc(void *, size_t);
66
67    #ifndef __STL_STATIC_TEMPLATE_MEMBER_BUG
68    static void (* __mempool_alloc_oom_handler)();
69    #endif
70
71 public:
72
73    static void * allocate(size_t n)
74    {
75       verifyMempoolAllocated();
76       void *result = STLMemPool->allocate(n);
77       if (0 == result) result = oom_malloc(n);
78       return result;
79    }
80
81    static void deallocate(void *p, size_t /* n */)
82    {
83       STLMemPool->deallocate(p);
84    }
85
86    static void * reallocate(void *p, size_t /* old_sz */, size_t new_sz)
87    {
88       void * result = STLMemPool->reallocate(p, new_sz);
89       if (0 == result) result = oom_realloc(p, new_sz);
90       return result;
91    }
92
93    static void (* set_malloc_handler(void (*f)()))()
94    {
95       void (* old)() = __mempool_alloc_oom_handler;
96       __mempool_alloc_oom_handler = f;
97       return (old);
98    }
99
100 public:
101
102    static void verifyMempoolAllocated()
103    {
104       if (STLMemPool == NULL)      // ASSERT: No allocated
105       {
106          STLMemPool = new vjSharedPool(1024*1024);
107       }
108    }
109
110    static vjMemPool* STLMemPool;   
111 };
112
113 MemPool* __vj_mempool_alloc_template<0>::STLMemPool = NULL;
114
115 // mempool_alloc out-of-memory handling
116
117 #ifndef __STL_STATIC_TEMPLATE_MEMBER_BUG
118 template <int inst>
119 void (* __vj_mempool_alloc_template<inst>::__mempool_alloc_oom_handler)() = 0;
120 #endif
121
122 template <int inst>
123 void * __vj_mempool_alloc_template<inst>::oom_malloc(size_t n)
124 {
125     void (* my_malloc_handler)();
126     void *result;
127
128     for (;;) {
129         my_malloc_handler = __mempool_alloc_oom_handler;
130         if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }
131         (*my_malloc_handler)();
132         result = malloc(n);
133         if (result) return(result);
134     }
135 }
136
137 template <int inst>
138 void * __vj_mempool_alloc_template<inst>::oom_realloc(void *p, size_t n)
139 {
140     void (* my_malloc_handler)();
141     void *result;
142
143     for (;;) {
144         my_malloc_handler = __mempool_alloc_oom_handler;
145         if (0 == my_malloc_handler) { __THROW_BAD_ALLOC; }
146         (*my_malloc_handler)();
147         result = realloc(p, n);
148         if (result) return(result);
149     }
150 }
151
152 typedef __vj_mempool_alloc_template<0> mempool_alloc;
153
154 #endif  // _MEMPOOL_ALLOC_H_
Note: See TracBrowser for help on using the browser.