00001 /* Copyright 1994, 1995 LongView Technologies L.L.C. $Revision: 1.4 */ 00002 /* Copyright (c) 2006, Sun Microsystems, Inc. 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 00006 following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 00010 disclaimer in the documentation and/or other materials provided with the distribution. 00011 * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 00012 from this software without specific prior written permission. 00013 00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00020 00021 00022 */ 00023 00024 // ReservedSpace is a data strucure for reserving a contiguous chunk of memory. 00025 00026 class ReservedSpace : public ValueObj { 00027 private: 00028 char* _base; 00029 int _size; 00030 public: 00031 ReservedSpace(int size); 00032 ReservedSpace(char* base, int size) { 00033 _base = base; 00034 _size = size; 00035 } 00036 00037 // Accessors 00038 char* base() { return _base; } 00039 int size() { return _size; } 00040 00041 bool is_reserved() { return _base != NULL; } 00042 00043 // Splitting 00044 ReservedSpace first_part(int partition_size); 00045 ReservedSpace last_part(int partition_size); 00046 00047 // Alignment 00048 static int page_align_size(int size); 00049 }; 00050 00051 // VirtualSpace is data structure for reserving a contiguous chunk of memory and 00052 // then commit to the reserved chunk bit by bit. 00053 // Perfect for implementing growable stack without relocation. 00054 00055 class VirtualSpace : public ValueObj { 00056 private: 00057 // Reserved area 00058 char* _low_boundary; 00059 char* _high_boundary; 00060 00061 // Committed area 00062 char* _low; 00063 char* _high; 00064 00065 // Grow direction 00066 bool _low_to_high; 00067 00068 VirtualSpace* next; 00069 friend class VirtualSpaces; 00070 00071 public: 00072 char* low() const { return _low; } 00073 char* high() const { return _high; } 00074 00075 char* low_boundary() const { return _low_boundary; } 00076 char* high_boundary() const { return _high_boundary; } 00077 00078 public: 00079 VirtualSpace(int reserved_size, int committed_size, bool low_to_high = true); 00080 VirtualSpace(ReservedSpace reserved, int committed_size, bool low_to_high = true); 00081 VirtualSpace(); 00082 00083 void initialize(ReservedSpace reserved, int committed_size, bool low_to_high = true); 00084 00085 ~VirtualSpace(); 00086 00087 // testers 00088 int committed_size() const; 00089 int reserved_size() const; 00090 int uncommitted_size() const; 00091 bool contains(void* p) const; 00092 bool low_to_high() const; 00093 00094 // operations 00095 void expand(int size); 00096 void shrink(int size); 00097 void release(); 00098 00099 // debugging 00100 void print(); 00101 00102 // page faults 00103 virtual void page_fault() {} 00104 }; 00105 00106 class VirtualSpaces : AllStatic { 00107 private: 00108 static VirtualSpace* head; 00109 static void add(VirtualSpace* sp); 00110 static void remove(VirtualSpace* sp); 00111 friend class VirtualSpace; 00112 public: 00113 static int committed_size(); 00114 static int reserved_size(); 00115 static int uncommitted_size(); 00116 static void print(); 00117 static void test(); 00118 };