00001 /* -*- mode:C++; c-basic-offset:4 -*- 00002 Shore-MT -- Multi-threaded port of the SHORE storage manager 00003 00004 Copyright (c) 2007-2009 00005 Data Intensive Applications and Systems Labaratory (DIAS) 00006 Ecole Polytechnique Federale de Lausanne 00007 00008 All Rights Reserved. 00009 00010 Permission to use, copy, modify and distribute this software and 00011 its documentation is hereby granted, provided that both the 00012 copyright notice and this permission notice appear in all copies of 00013 the software, derivative works or modified versions, and any 00014 portions thereof, and that both notices appear in supporting 00015 documentation. 00016 00017 This code is distributed in the hope that it will be useful, but 00018 WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS 00020 DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER 00021 RESULTING FROM THE USE OF THIS SOFTWARE. 00022 */ 00023 /*<std-header orig-src='shore' incl-file-exclusion='BLOCK_ALLOC_CPP'> 00024 00025 $Id: block_alloc.cpp,v 1.3 2010/07/07 21:43:45 nhall Exp $ 00026 00027 SHORE -- Scalable Heterogeneous Object REpository 00028 00029 Copyright (c) 1994-99 Computer Sciences Department, University of 00030 Wisconsin -- Madison 00031 All Rights Reserved. 00032 00033 Permission to use, copy, modify and distribute this software and its 00034 documentation is hereby granted, provided that both the copyright 00035 notice and this permission notice appear in all copies of the 00036 software, derivative works or modified versions, and any portions 00037 thereof, and that both notices appear in supporting documentation. 00038 00039 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY 00040 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS 00041 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND 00042 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 00043 00044 This software was developed with support by the Advanced Research 00045 Project Agency, ARPA order number 018 (formerly 8230), monitored by 00046 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518. 00047 Further funding for this work was provided by DARPA through 00048 Rome Research Laboratory Contract No. F30602-97-2-0247. 00049 00050 */ 00051 00052 #include "block_alloc.h" 00053 #include <cassert> 00054 00055 /**\cond skip */ 00056 00057 dynpool::dynpool(size_t chip_size, size_t chip_count, size_t log2_block_size, size_t max_bytes) 00058 : _chip_size(chip_size) 00059 , _chip_count(chip_count) 00060 , _log2_block_size(log2_block_size) 00061 , _arr_end(0) 00062 { 00063 pthread_mutex_init(&_lock, 0); 00064 int err = _arr.init(max_bytes, size_t(1) << _log2_block_size); 00065 if(err) throw std::bad_alloc(); 00066 } 00067 00068 dynpool::~dynpool() { 00069 pthread_mutex_destroy(&_lock); 00070 _arr.fini(); 00071 } 00072 00073 dynpool::mblock* dynpool::_acquire_block() { 00074 mblock* rval; 00075 pthread_mutex_lock(&_lock); 00076 if(_free_list.empty()) { 00077 size_t block_size = size_t(1) << _log2_block_size; 00078 size_t new_end = _arr_end+block_size; 00079 int err = _arr.ensure_capacity(new_end); 00080 if(err) throw std::bad_alloc(); 00081 rval = new (_arr+_arr_end) mblock(_chip_size, _chip_count, block_size); 00082 _arr_end = new_end; 00083 } 00084 else { 00085 rval = _free_list.front(); 00086 _free_list.pop_front(); 00087 } 00088 pthread_mutex_unlock(&_lock); 00089 00090 return rval; 00091 } 00092 00093 void dynpool::_release_block(mblock* b) { 00094 pthread_mutex_lock(&_lock); 00095 _free_list.push_back(b); 00096 pthread_mutex_unlock(&_lock); 00097 } 00098 00099 bool dynpool::validate_pointer(void* ptr) { 00100 // no need for the mutex... dynarray only grows 00101 union { void* v; char* c; } u={ptr}; 00102 size_t offset = u.c - _arr; 00103 return offset < _arr_end; 00104 } 00105 /**\endcond skip */ 00106