?? pooled_allocator.cc
字號:
//////////////////////////////////////////////////////////////////// ////// Chunk::mark() //////////////////////////////////////////////////////////////////// ////// ////// global_start_element - first element of found block ////// num_of_elements - memory for num_of_elements ////// ////// mark as used in the bit vector ////// //////////////////////////////////////////////////////////////////// void Chunk::mark(int global_start_element,int num_of_elems) { // mark num_of_elems starting at start_element // as assigned. This function is used to link // newly freed memory with a previous free block. if ((first_elem_num <= global_start_element)&& (global_start_element < (first_elem_num+num_of_elements)) ) { int local_start_element=global_start_element-first_elem_num; bit_vec.mark_items(local_start_element,num_of_elems); } else { std::clog << __FILE__ << ':' << __LINE__ << ':' << " mark(): "; std::clog << "requested start_element outside of chunk.\n"; std::clog << "first_elem_num: " << first_elem_num << std::endl; } return; }; // Chunk::mark() //////////////////////////////////////////////////////////////////// ////// Chunk::clear() //////////////////////////////////////////////////////////////////// ////// ////// start_element - first element of found block ////// num_of_elems - memory for num_of_elements ////// ////// clear set blocks as unused in the bit vector ////// //////////////////////////////////////////////////////////////////// void Chunk::clear(int global_start_element,int num_of_elems) { // clear num_of_elems starting at start_element // as available. if ((first_elem_num<=global_start_element)&& (global_start_element<(first_elem_num+num_of_elements))) { int local_start_element=global_start_element-first_elem_num; bit_vec.clear_items(local_start_element,num_of_elems); } else { std::clog << __FILE__ << ':' << __LINE__ << ':' << " clear(): "; std::clog << "requested start_element outside of chunk.\n"; std::clog << " first_elem_num: " << first_elem_num << " num_of_elements: " << num_of_elements << std::endl; } return; }; // Chunk::clear() //////////////////////////////////////////////////////////////////// ////// Chunk::element_num_to_pointer(int global_start_element) //////////////////////////////////////////////////////////////////// ////// ////// global_start_element - compute the memory address of ////// this global element number. ////// ////// find and return the memory address, p, for this global ////// element number. Used to free blocks which are in use. ////// //////////////////////////////////////////////////////////////////// unsigned char* Chunk::element_num_to_pointer(int global_start_element) { // Calculate and return the global block(element) number of the // block residing at p. int local_start_element = global_start_element - first_elem_num; unsigned char* p = 0; if ((0 <= local_start_element) && (local_start_element < num_of_elements)) { p = mem+(local_start_element*element_size); } else { std::clog << __FILE__ << ':' << __LINE__ << ':' << " element_num_to_pointer(): "; std::clog << "requested block outside of chunk.\n"; } return p; }; // Chunk::element_num_to_pointer() //////////////////////////////////////////////////////////////////// ////// Chunk::get_page_offset() //////////////////////////////////////////////////////////////////// ////// ////// Calculate the offset from the beginning of the Chunk ////// to the specified page number. ////// //////////////////////////////////////////////////////////////////// const int Chunk::get_element_offset(int global_start_element) { int element_num = global_start_element - first_elem_num; if (element_num < 0) { std::clog << "Error: " << __FILE__ << ':' << __LINE__ ; std::clog << ": element number out of range: " << element_num << std::endl; throw (Alloc_Exception::alloc_exception(0)); } return (sizeof(Chunk) + // chunk header bit_vec_size + // bit_vec space element_num*element_size); // count to element }; // const int Chunk::get_page_offset(int page_num) //////////////////////////////////////////////////////////////////// ////// Chunk::find_element_num() //////////////////////////////////////////////////////////////////// ////// ////// p - pointer to allocated memory. ////// ////// Returns element number corresponding to p if p is from ////// this chunk, or -1 if p is not from the chunk. The ////// element number returned is the local element number, ////// not the global element number. ////// //////////////////////////////////////////////////////////////////// int Chunk::pointer_to_element_num(const unsigned char* p) { int element_num = -1; // set chunk memory lower bound const unsigned char* lower_bound = mem; // set chunk memory upper bound const unsigned char* upper_bound = lower_bound + element_size*num_of_elements; if ((lower_bound<=p)&&(p<upper_bound)) { // if the pointer is in the memory range of this chunk element_num = (static_cast<int>(p-lower_bound))/element_size; } // if ((lower_bound<=p)&&(p<upper_bound)) return element_num; }; //////////////////////////////////////////////////////////////////// ////// Chunk::allocate() //////////////////////////////////////////////////////////////////// ////// ////// global_start_element - first element of found block ////// num_of_elements - memory for num_of_elements ////// ////// allocate adjusts pointers in the memory chunk and ////// returns a pointer to the start of the allocated ////// memory. ////// //////////////////////////////////////////////////////////////////// mem_space::memory_index_t Chunk::allocate(int global_start_element, int num_of_elems) { // allocate num_of_elems starting with element start_element // assumes that: // 1. global_start_element has already been located by find() // as a free block, // 2. That the memory at global_start_element has been marked // as allocated by mark() and is now ready to be returned // mark the elements as in use. mark(global_start_element,num_of_elems); unsigned char* p = element_num_to_pointer(global_start_element); // The segment_page_num can be used to determine global offset // from beginning of a memory segment to the start of a shared // memory segment page. Chunks are allocated so that they reside // a the beginnings of these pages. The global offset = // chunk_offset + element_offset within the chunk. These values // are used to compute the offset from segment start to beginning // of this chunk, to a specific element if desired.// int segment_page_num = get_segment_page_num(); // compute offset from beginning of this chunk to element number. int element_offset = get_element_offset(global_start_element); // return p; return mem_space::memory_index_t(p, pathname, proj_id, segment_page_num, element_offset, global_start_element); }; // Chunk::allocate() //////////////////////////////////////////////////////////////////// ////// Chunk::free() //////////////////////////////////////////////////////////////////// ////// ////// num_of_elems - memory for num_of_elems ////// ////// free adjusts pointers in the memory chunk ////// ////// //////////////////////////////////////////////////////////////////// // release chunk elements void Chunk::free(int global_start_element, int num_of_elems) { clear(global_start_element,num_of_elems); return; }; // Chunk::free() } // namespace pooled_allocator
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -