📄 memmap.cpp
字号:
//####COPYRIGHTBEGIN####// // ----------------------------------------------------------------------------// Copyright (C) 1998, 1999, 2000 Red Hat, Inc.//// This program is part of the eCos host tools.//// This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free // Software Foundation; either version 2 of the License, or (at your option) // any later version.// // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for // more details.// // You should have received a copy of the GNU General Public License along with// this program; if not, write to the Free Software Foundation, Inc., // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// ----------------------------------------------------------------------------// //####COPYRIGHTEND####//=================================================================//// memmap.cpp//// Memory Layout Tool map data structure manipulation class////=================================================================//#####DESCRIPTIONBEGIN####//// Author(s): John Dallaway// Contact(s): jld// Date: 1998/07/29 $RcsDate$ {or whatever}// Version: 0.00+ $RcsVersion$ {or whatever}// Purpose: Provides functions to create and destroy memory regions// and sections within the memory map.// Description: Each function manipulates data structures representing// memory regions, memory sections and the view of memory// sections as presented to the user. The section view// structure organises the sections by region and // will contain two instances of each relocated section // Requires: memmap.h// Provides: create_memory_region()// delete_memory_region()// edit_memory_region()// create_memory_section()// delete_memory_section()// edit_memory_section()// delete_all_memory_sections()// set_map_size()// section_list// region_list// See also: memmap.h// Known bugs: <UPDATE_ME_AT_RELEASE_TIME>// WARNING: Do not modify data structures other than by using the// provided functions// Usage: #include "memmap.h"// ...// status = set_map_size (0x8000);////####DESCRIPTIONEND#####pragma warning (disable:4514) /* unreferenced inline function */#pragma warning (disable:4710) /* function not inlined */#include "memmap.h"using namespace std;#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;//define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////mem_map::mem_map(){ map_modified_flag = true; map_size = (mem_address) 0;}mem_map::~mem_map(){}mem_section::mem_section(){}mem_section::~mem_section(){}///////////////////////////////////////////////////////////////////////// get_memory_region() retrieves the parameters of a memory regionbool mem_map::get_memory_region (string region_name, mem_address * region_address, mem_address * region_size, mem_type * region_type, string * note){ for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region) if (region->name == region_name) { *region_address = region->address; *region_size = region->size; *region_type = region->type; *note = region->note; return true; } return false;}///////////////////////////////////////////////////////////////////////// create_memory_region() inserts a new item into the memory region list// in order of memory addressint mem_map::create_memory_region (string new_region_name, mem_address new_region_address, mem_address new_region_size, mem_type new_region_type, string note){ const mem_address new_region_end = new_region_address + new_region_size; // the byte after the new region end // check that the new region name is specified if (new_region_name == "") return ERR_MEMMAP_REGION_NONAME; // the new region name must be specified // check that the new region lies within the memory map if (new_region_end > map_size) return ERR_MEMMAP_REGION_MAPSIZE; // the new region does not lie within the memory map // check that the region end address hasn't exceeded the storage size if (new_region_end < new_region_address) return ERR_MEMMAP_REGION_MAPSIZE; // the new region does not lie within the memory map // initialise the insertion point for the new region list <mem_region>::iterator insertion_point = region_list.end (); // check that the new region does not overlap existing regions and does not already exist for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region) { const mem_address region_end = region->address + region->size; // the byte after the region end if ((new_region_address >= region->address) && (new_region_address < region_end)) { error_info = region->name; return ERR_MEMMAP_REGION_INTERSECT; // the start of the new region is within an existing region } if ((new_region_end > region->address) && (new_region_end <= region_end)) { error_info = region->name; return ERR_MEMMAP_REGION_INTERSECT; // the end of the new region is within an existing region } if ((new_region_address < region->address) && (new_region_end > region_end)) { error_info = region->name; return ERR_MEMMAP_REGION_INTERSECT; // an existing region lies within the new region } if (region->name == new_region_name) return ERR_MEMMAP_REGION_NAMEINUSE; // the new region name is not unique if ((insertion_point == region_list.end ()) && (region->address > new_region_address)) insertion_point = region; // insert the new region here } // add the new region to the region list list <mem_region>::iterator new_region = region_list.insert (insertion_point); new_region->name = new_region_name; new_region->address = new_region_address; new_region->size = new_region_size; new_region->type = new_region_type; new_region->note = note; // initialise the section list for the new region calc_section_list (new_region); map_modified_flag = true; return 0;}///////////////////////////////////////////////////////////////////////// edit_memory_region() edits an item in the memory region listint mem_map::edit_memory_region (string old_region_name, string new_region_name, mem_address new_region_address, mem_address new_region_size, mem_type new_region_type, string note){ list <mem_region>::iterator edit_region = find_memory_region (old_region_name); if (edit_region == NULL) return ERR_MEMMAP_REGION_NOTFOUND; // the region to be modified does not exist // check that the new region name is specified if (new_region_name == "") return ERR_MEMMAP_REGION_NONAME; // the new region name must be specified // check that the region end address hasn't exceeded the storage size if (new_region_address + new_region_size < new_region_address) return ERR_MEMMAP_REGION_MAPSIZE; // the new region does not lie within the memory map // check region name change if ((old_region_name != new_region_name) && (find_memory_region (new_region_name) != NULL)) return ERR_MEMMAP_REGION_NAMEINUSE; // new region name is not unique // check region address/size change wrt other regions const mem_address new_region_end = new_region_address + new_region_size; if ((new_region_address != edit_region->address) || (new_region_size != edit_region->size)) { for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region) if (region != edit_region) { const mem_address region_end = region->address + region->size; // the byte after the region end if ((new_region_address >= region->address) && (new_region_address < region_end)) { error_info = region->name; return ERR_MEMMAP_REGION_INTERSECT; // the start of the modified region is within another region } if ((new_region_end > region->address) && (new_region_end <= region_end)) { error_info = region->name; return ERR_MEMMAP_REGION_INTERSECT; // the end of the modified region is within an existing region } if ((new_region_address < region->address) && (new_region_end > region_end)) { error_info = region->name; return ERR_MEMMAP_REGION_INTERSECT; // another region lies within the modified region } } } // check region size change wrt sections within region (if any) for (list <mem_section_view>::iterator section_view = edit_region->section_view_list.begin (); section_view != edit_region->section_view_list.end (); ++section_view) if (section_view->section != NULL) { if ((section_view->section_location == final_location) || (section_view->section_location == fixed_location)) if (section_view->section->final_location->anchor == absolute) if (section_view->section->final_location->address + section_view->section->size - edit_region->address > new_region_size) return ERR_MEMMAP_REGION_SIZE; // region is now too small if (section_view->section_location == initial_location) if (section_view->section->initial_location->anchor == absolute) if (section_view->section->initial_location->address + section_view->section->size - edit_region->address > new_region_size) return ERR_MEMMAP_REGION_SIZE; // region is now too small } // check region read-only change FIXME // move sections within the region having absolute anchors for (section_view = edit_region->section_view_list.begin (); section_view != edit_region->section_view_list.end (); ++section_view) if (section_view->section != NULL) { if ((section_view->section_location == final_location) || (section_view->section_location == fixed_location)) if (section_view->section->final_location->anchor == absolute) section_view->section->final_location->address += (new_region_address - edit_region->address); if ((section_view->section_location == initial_location) || (section_view->section_location == fixed_location)) if (section_view->section->initial_location->anchor == absolute) section_view->section->initial_location->address += (new_region_address - edit_region->address); } // deleteZ(the region and recreate it to make sure the region list is ordered correctly) region_list.erase (edit_region); if (create_memory_region (new_region_name, new_region_address, new_region_size, new_region_type, note)) return ERR_MEMMAP_ALLOC; map_modified_flag = true; return 0;}//////////////////////////////////////////////////////////////////// delete_memory_region() removes an existing item from the memory// region listbool mem_map::delete_memory_region (string name){ // make sure that there are no used sections in this region before deleting it for (list <mem_region>::iterator region = region_list.begin (); region != region_list.end (); ++region) { if ((region->name == name) && (region->section_view_list.size () == 1) && (region->section_view_list.front ().section == NULL)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -