Sirikata
|
00001 /* Sirikata Transfer -- Content Transfer management system 00002 * CachePolicy.hpp 00003 * 00004 * Copyright (c) 2008, Patrick Reiter Horn 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are 00009 * met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the name of Sirikata nor the names of its contributors may 00017 * be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00021 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00022 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00023 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00024 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 /* Created on: Jan 5, 2009 */ 00033 00034 #ifndef SIRIKATA_CachePolicy_HPP__ 00035 #define SIRIKATA_CachePolicy_HPP__ 00036 00037 #include <sirikata/core/transfer/Defs.hpp> 00038 00039 namespace Sirikata { 00040 namespace Transfer { 00041 00042 static const cache_usize_type tebibyte = 0x10000000000LL, terabyte = 1000000000000LL; 00043 static const cache_usize_type gibibyte = 0x40000000, gigabyte = 1000000000; 00044 static const cache_usize_type mebibyte = 0x100000, megabyte = 1000000; 00045 static const cache_usize_type kibibyte = 0x400, kilobyte = 1000; 00046 static const cache_usize_type byte = 1; 00047 00049 class CachePolicy { 00050 00051 protected: 00052 cache_usize_type mTotalSize; 00053 float mMaxSizePct; 00054 cache_ssize_type mFreeSpace; 00055 00056 inline void updateSpace(cache_usize_type oldsize, cache_usize_type newsize) { 00057 std::ostringstream oss; 00058 oss << "[CachePolicy] "; 00059 if (oldsize) { 00060 if (newsize) { 00061 oss << "Resizing cache data from " << oldsize << " to " << newsize; 00062 } else { 00063 oss << "Deallocating cache data of " << oldsize; 00064 } 00065 } else { 00066 oss << "Allocating cache data of " << newsize; 00067 } 00068 mFreeSpace += oldsize; 00069 mFreeSpace -= newsize; 00070 oss << "; free space is now " << mFreeSpace << "."; 00071 SILOG(cachepolicy,insane,oss.str()); 00072 } 00073 00074 public: 00075 00076 struct Data { 00077 }; 00078 00079 CachePolicy(cache_usize_type allocatedSpace, float maxSizePct) 00080 : mTotalSize(allocatedSpace), 00081 mMaxSizePct(maxSizePct), 00082 mFreeSpace((cache_ssize_type)allocatedSpace) { 00083 } 00084 00086 virtual ~CachePolicy() { 00087 } 00088 00095 virtual void use(const Fingerprint &id, Data* data, cache_usize_type size) = 0; 00096 00104 virtual void useAndUpdate(const Fingerprint &id, Data* data, cache_usize_type oldsize, cache_usize_type newsize) = 0; 00105 00113 virtual void destroy(const Fingerprint &id, Data* data, cache_usize_type size) = 0; 00114 00125 virtual Data* create(const Fingerprint &id, cache_usize_type size) = 0; 00126 00133 virtual bool cachable(cache_usize_type requiredSpace) { 00134 if (((cache_ssize_type)requiredSpace) < 0) { 00135 // overflows a ssize_t. 00136 return false; 00137 } 00138 if ((double)requiredSpace >= (double)mTotalSize * mMaxSizePct) { 00139 SILOG(cachepolicy,insane,"Rejecting allocation for " << requiredSpace << " bytes of " << mFreeSpace << " free"); 00140 return false; 00141 } 00142 SILOG(cachepolicy,insane,"Need to allocate " << requiredSpace << " bytes of " << mFreeSpace << " free"); 00143 return true; 00144 } 00145 00146 virtual bool nextItem(cache_usize_type requiredSpace, Fingerprint &myprint) = 0; 00147 }; 00148 00149 00150 } 00151 } 00152 00153 #include "CacheLayer.hpp" 00154 00155 #endif /* SIRIKATA_CachePolicy_HPP__ */