00001 /******************************************************************** 00002 00003 Copyright 2006, ACCESS Systems Americas, Inc. All rights reserved. 00004 00005 The contents of this file are subject to the Mozilla Public License Version 00006 1.1 (the "License"); you may not use this file except in compliance with 00007 the License. You may obtain a copy of the License at 00008 http://www.mozilla.org/MPL/ 00009 00010 Software distributed under the License is distributed on an "AS IS" basis, 00011 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 00012 for the specific language governing rights and limitations under the 00013 License. 00014 00015 The Original Code is the entire contents of this file. 00016 00017 The Initial Developer of the Original Code is ACCESS Systems Americas, Inc. 00018 00019 Portions created by ACCESS Systems Americas, Inc. are Copyright © 2006. All 00020 Rights Reserved. 00021 00022 Contributor(s): none. 00023 00024 ********************************************************************/ 00025 /* 00026 * Copyright (c) 2005 Palmsource, Inc. 00027 * 00028 * This software is licensed as described in the file LICENSE, which 00029 * you should have received as part of this distribution. The terms 00030 * are also available at http://www.openbinder.org/license.html. 00031 * 00032 * This software consists of voluntary contributions made by many 00033 * individuals. For the exact contribution history, see the revision 00034 * history and logs, available at http://www.openbinder.org 00035 */ 00036 00037 #ifndef _SUPPORT_SHARED_BUFFER_H_ 00038 #define _SUPPORT_SHARED_BUFFER_H_ 00039 00046 //#include <support/Atom.h> 00047 //#include <support/SupportDefs.h> 00048 //#include <support/atomic.h> 00049 #include <hiker/types.h> 00050 00051 namespace alp { 00052 00057 00058 00061 enum { 00062 00063 ALP_BUFFER_USERS_SHIFT = 4, 00064 ALP_BUFFER_LENGTH_SHIFT = 1, 00065 00067 00068 ALP_STATIC_USERS = 0x00000001, 00069 00071 00072 ALP_POOLED_USERS = 0x00000002, 00073 00075 00076 ALP_EXTENDED_BUFFER = 0x00000001 00077 }; 00078 00079 // ************************************************************************* 00081 00095 class SharedBuffer 00096 { 00097 public: 00099 00100 static SharedBuffer* Alloc(size_t length); 00101 00103 inline const void* Data() const; 00105 00109 inline void* Data(); 00111 inline size_t Length() const; 00112 00114 00121 static inline const SharedBuffer* BufferFromData(const void *data); 00122 00124 void IncUsers() const; 00126 00127 void DecUsers() const; 00129 00133 inline int32_t Users() const; 00134 00136 00139 SharedBuffer* Edit(size_t newLength) const; 00140 00142 SharedBuffer* Edit() const; 00143 00145 00150 const SharedBuffer* Pool() const; 00151 00153 00156 int32_t Compare(const SharedBuffer* other) const; 00157 00159 00162 SharedBuffer* BeginBuffering(); 00164 SharedBuffer* EndBuffering(); 00165 00167 bool Buffering() const; 00169 size_t BufferSize() const; 00170 00172 void IncStrong(const void* ) const { IncUsers(); } 00174 void DecStrong(const void* ) const { DecUsers(); } 00176 void IncStrongFast() const { IncUsers(); } 00178 void DecStrongFast() const { DecUsers(); } 00179 00180 // These are used for static values. See StaticValue.h. 00181 typedef void (*inc_ref_func)(); 00182 typedef void (*dec_ref_func)(); 00183 00184 private: 00185 inline SharedBuffer() { } 00186 inline ~SharedBuffer() { } 00187 00188 struct extended_info; 00189 extended_info* get_extended_info() const; 00190 bool unpool() const; 00191 void do_delete() const; 00192 00193 SharedBuffer(const SharedBuffer& o); 00194 00195 static SharedBuffer* AllocExtended(extended_info* prototype, size_t length); 00196 SharedBuffer* Extend(size_t length) const; 00197 00198 void SetBufferSize(size_t size); 00199 void SetLength(size_t len); 00200 00201 /* ******************************************************* 00202 * WARNING 00203 * 00204 * The size or layout of SharedBuffer cannot be changed. 00205 * In order to do some important optimizations, some code 00206 * assumes this layout. For eg, see support/StaticValue.h. 00207 * 00208 * *******************************************************/ 00209 00210 mutable int32_t m_users; 00211 size_t m_length; 00212 #if defined(_MSC_VER) 00213 char m_data[0]; 00214 #endif 00215 }; 00216 00219 /*-------------------------------------------------------------*/ 00220 /*---- No user serviceable parts after this -------------------*/ 00221 00222 inline const void* SharedBuffer::Data() const 00223 { 00224 return this+1; 00225 } 00226 00227 inline void* SharedBuffer::Data() 00228 { 00229 return this+1; 00230 } 00231 00232 inline const SharedBuffer* SharedBuffer::BufferFromData(const void *data) 00233 { 00234 return ((static_cast<const SharedBuffer *>(data)) - 1); 00235 } 00236 00237 inline size_t SharedBuffer::Length() const 00238 { 00239 return m_length>>ALP_BUFFER_LENGTH_SHIFT; 00240 } 00241 00242 inline int32_t SharedBuffer::Users() const 00243 { 00244 return m_users>>ALP_BUFFER_USERS_SHIFT; 00245 } 00246 00247 } // namespace alp 00248 00249 #endif /* _SUPPORT_SHARED_BUFFER_H_ */