root/juggler/tags/1.0.7/SharedMem/vjMemory.h
| Revision 8789, 4.4 kB (checked in by patrickh, 7 years ago) | |
|---|---|
| |
| 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 | #ifndef _VJMemory_h_ |
| 36 | #define _VJMemory_h_ |
| 37 | |
| 38 | //class vjMemory; |
| 39 | |
| 40 | #include <vjConfig.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <stdlib.h> |
| 43 | |
| 44 | #include <SharedMem/vjMemPool.h> |
| 45 | |
| 46 | class vjMemory; |
| 47 | |
| 48 | // -- Just allocate to much memory |
| 49 | // -- Then move pointer over by siezeof(memPool) |
| 50 | |
| 51 | // vjMemory |
| 52 | // |
| 53 | // Purpose: |
| 54 | //: Base Class for all objects that want memory abilities. |
| 55 | // This class allows object to be allocated/deallocated |
| 56 | // from memory "pools". It takes care of the new and delete |
| 57 | // as well as tracking the pool. |
| 58 | // |
| 59 | // Author: |
| 60 | // Allen Bierbaum |
| 61 | // |
| 62 | // Date: 1-9-97 |
| 63 | class vjMemory { |
| 64 | public: |
| 65 | vjMemory() |
| 66 | { |
| 67 | memPool = NULL; |
| 68 | } |
| 69 | public: |
| 70 | // ----------------------------------------------------------------------- |
| 71 | //: Allows access to be allocate on default heap rather than the memPool. |
| 72 | // ----------------------------------------------------------------------- |
| 73 | void* operator new(size_t sz); |
| 74 | |
| 75 | // ----------------------------------------------------------------------- |
| 76 | //: Allocates memory of size from the given memPool. |
| 77 | // ----------------------------------------------------------------------- |
| 78 | void* operator new(size_t sz, vjMemPool* mp); |
| 79 | |
| 80 | // ----------------------------------------------------------------------- |
| 81 | //: Same as operator delete, reverses effects of new. |
| 82 | // ----------------------------------------------------------------------- |
| 83 | void localDelete(void* ptr) |
| 84 | { |
| 85 | if (memPool != NULL) |
| 86 | memPool->deallocate(ptr); |
| 87 | else |
| 88 | #ifdef VJ_OS_HPUX |
| 89 | free(ptr); |
| 90 | #else |
| 91 | delete ptr; |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | // ----------------------------------------------------------------------- |
| 96 | //: Reverses the effects of either new operator. |
| 97 | // ----------------------------------------------------------------------- |
| 98 | void operator delete(void* ptr) |
| 99 | { |
| 100 | static_cast<vjMemory*>(ptr)->localDelete(ptr); |
| 101 | } |
| 102 | |
| 103 | // ----------------------------------------------------------------------- |
| 104 | //: Return the memPool that the derived object is living. |
| 105 | // This MUST be used for vjMemory derived classes to dynamically create |
| 106 | // other vjMemory derivced classes in the form: |
| 107 | // |
| 108 | // myClass *mc = new ( getMyMemPool() ) myClass; |
| 109 | // ----------------------------------------------------------------------- |
| 110 | vjMemPool* getMyMemPool() |
| 111 | { |
| 112 | return memPool; |
| 113 | } |
| 114 | |
| 115 | // ----------------------------------------------------------------------- |
| 116 | //: An object should use allocate to get more memory from its memPool. |
| 117 | // ----------------------------------------------------------------------- |
| 118 | void* allocate(size_t size) |
| 119 | { |
| 120 | return getMyMemPool()->allocate(size); |
| 121 | } |
| 122 | |
| 123 | // ----------------------------------------------------------------------- |
| 124 | //: Memory allocated using allocate should be deallocated with this |
| 125 | //+ function. |
| 126 | // |
| 127 | //! PRE: ptr was allocated with allocate(size) by the same object making |
| 128 | //+ this call. |
| 129 | // ----------------------------------------------------------------------- |
| 130 | void deallocate(void* ptr) |
| 131 | { |
| 132 | getMyMemPool()->deallocate(ptr); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | vjMemPool* memPool; |
| 137 | }; |
| 138 | |
| 139 | #endif |
Note: See TracBrowser for help on using the browser.
