00001 // See the end of this file for license information. 00002 00003 #ifndef TORSION_PAGEALLOC_H 00004 #define TORSION_PAGEALLOC_H 00005 00006 #include "list.h" 00007 #include "arch.h" 00008 #include "types.h" 00009 #include "pagespan.h" 00010 00015 00016 class Page_allocator { 00017 protected: 00019 unsigned memory_begin; 00021 unsigned memory_end; 00022 Page* pages; 00023 unsigned page_count; 00024 unsigned free_list_count; 00025 00027 List<Page>* free_lists; 00028 00031 Page* 00032 find_free_page(unsigned alignment, unsigned begin, unsigned end); 00033 00037 Page* 00038 try_to_split_page(unsigned requested_alignment, unsigned begin, unsigned end); 00039 00043 Page* 00044 split_page(Page* page, unsigned requested_alignment, unsigned begin, 00045 unsigned end); 00046 00050 Page* 00051 try_to_coalesce_page(unsigned requested_alignment, unsigned begin, 00052 unsigned end); 00053 00057 Page* 00058 coalesce_page(Page* page, unsigned requested_alignment, unsigned begin, 00059 unsigned end); 00060 00061 public: 00067 void 00068 init(void* start, Size memory_size, unsigned max_alignment, 00069 void* metadata_end = NULL, bool managed_pages_are_physical = false); 00070 00073 Page* alloc_page_within(unsigned alignment, void* owner, unsigned begin, 00074 unsigned end); 00075 00078 Page* alloc_page_above(unsigned alignment, void* owner, unsigned begin); 00079 00082 Page* alloc_page_below(unsigned alignment, void* owner, unsigned end); 00083 00085 Page* alloc_page(unsigned alignment, void* owner); 00086 00088 void 00089 free_page(Page* page); 00090 00092 Page* 00093 get_page_info(void* memory) const; 00094 00096 inline void* 00097 get_metadata_start() { 00098 return (void*)pages; 00099 } 00100 00103 void* 00104 get_max_metadata_start(); 00105 00107 inline bool 00108 manages(void* memory) { 00109 unsigned page_shifted = (unsigned)memory >> PAGE_ALIGNMENT; 00110 if (page_shifted >= memory_begin && page_shifted <= memory_end) 00111 return true; 00112 return false; 00113 } 00114 00116 void 00117 print(); 00118 }; 00119 00120 inline Page* 00121 Page_allocator::alloc_page(unsigned alignment, void* owner) { 00122 return alloc_page_within(alignment, owner, memory_begin, memory_end - 1); 00123 } 00124 00125 inline Page* 00126 Page_allocator::alloc_page_above(unsigned alignment, void* owner, 00127 unsigned begin) { 00128 return alloc_page_within(alignment, owner, begin, memory_end - 1); 00129 } 00130 00131 inline Page* 00132 Page_allocator::alloc_page_below(unsigned alignment, void* owner, 00133 unsigned end) { 00134 return alloc_page_within(alignment, owner, memory_begin, end - 1); 00135 } 00136 00137 inline Page* 00138 Page_allocator::get_page_info(void* memory) const { 00139 return &pages[(unsigned(memory) >> PAGE_ALIGNMENT) - memory_begin]; 00140 } 00141 00142 #endif 00143 00144 /* Torsion Operating System, Copyright (C) 2000-2004 Dan Helfman 00145 * 00146 * This program is free software; you can redistribute it and/or modify it 00147 * under the terms of the GNU General Public License as published by the 00148 * Free Software Foundation; either version 2 of the License, or (at your 00149 * option) any later version. 00150 * 00151 * This program is distributed in the hope that it will be useful, but 00152 * WITHOUT ANY WARRANTY; without even the implied warranty of 00153 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00154 * General Public License for more details (in the COPYING file). 00155 * 00156 * You should have received a copy of the GNU General Public License along 00157 * with this program; if not, write to the Free Software Foundation, Inc., 00158 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00159 */
Torsion Operating System, Copyright (C) 2000-2004 Dan Helfman