Back to index

lightning-sunbird  0.9+nobinonly
ipcMessageWriter.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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 Mozilla IPC.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Conrad Carlen <ccarlen@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "ipcMessageWriter.h"
00040 #include "prmem.h"
00041 #include <string.h>
00042 
00043 //*****************************************************************************
00044 // ipcMessageWriter
00045 //*****************************************************************************   
00046 
00047 ipcMessageWriter::~ipcMessageWriter()
00048 {
00049   if (mBuf)
00050     free(mBuf);
00051 }
00052   
00053 void ipcMessageWriter::PutInt8(PRUint8 val)
00054 {
00055   if (EnsureCapacity(sizeof(PRUint8))) 
00056     *mBufPtr++ = val;
00057 }
00058 
00059 // PutInt16 and PutInt32 go to pains to avoid unaligned memory accesses that
00060 // are larger than a byte. On some platforms, that causes a performance penalty.
00061 // On other platforms, Tru64 for instance, it's an error.
00062   
00063 void ipcMessageWriter::PutInt16(PRUint16 val)
00064 {
00065   if (EnsureCapacity(sizeof(PRUint16))) {
00066     PRUint8 temp[2];
00067     *(PRUint16*)temp = val;
00068     *mBufPtr++ = temp[0];
00069     *mBufPtr++ = temp[1];
00070   }
00071 }
00072   
00073 void ipcMessageWriter::PutInt32(PRUint32 val)
00074 {
00075   if (EnsureCapacity(sizeof(PRUint32))) {
00076     PRUint8 temp[4];
00077     *(PRUint32*)temp = val;
00078     *mBufPtr++ = temp[0];
00079     *mBufPtr++ = temp[1];
00080     *mBufPtr++ = temp[2];
00081     *mBufPtr++ = temp[3];    
00082   }
00083 }
00084   
00085 PRUint32 ipcMessageWriter::PutBytes(const void* src, PRUint32 n)
00086 {
00087   if (EnsureCapacity(n)) {
00088     memcpy(mBufPtr, src, n);
00089     mBufPtr += n;
00090     return n;
00091   }
00092   return 0;
00093 }
00094     
00095 PRBool ipcMessageWriter::GrowCapacity(PRInt32 sizeNeeded)
00096 {
00097   if (sizeNeeded < 0)
00098     return PR_FALSE;
00099   PRInt32 newCapacity = (mBufPtr - mBuf) + sizeNeeded;
00100   if (mCapacity == 0)
00101     mCapacity = newCapacity;
00102   else
00103   {
00104     while (newCapacity > mCapacity && (mCapacity << 1) > 0)
00105       mCapacity <<= 1;
00106     if (newCapacity > mCapacity) // if we broke out because of rollover
00107       return PR_FALSE;
00108   }
00109     
00110   PRInt32 curPos = mBufPtr - mBuf;
00111   mBuf = (PRUint8*)realloc(mBuf, mCapacity);
00112   if (!mBuf) {
00113     mError = PR_TRUE;
00114     return PR_FALSE;
00115   }
00116   mBufPtr = mBuf + curPos;
00117   mBufEnd = mBuf + mCapacity;
00118   return PR_TRUE;
00119 }