diff options
Diffstat (limited to 'src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common')
7 files changed, 839 insertions, 0 deletions
diff --git a/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/.cvsignore b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/.cvsignore new file mode 100644 index 00000000..df8a43a2 --- /dev/null +++ b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/.cvsignore @@ -0,0 +1,5 @@ +Makefile +tmTransaction.obj +tmVector.obj +transmgrcom_s.pdb +transmgrcom_s.lib diff --git a/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/Makefile.in b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/Makefile.in new file mode 100644 index 00000000..4521c39a --- /dev/null +++ b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/Makefile.in @@ -0,0 +1,61 @@ +# ***** BEGIN LICENSE BLOCK ***** +# Version: MPL 1.1/GPL 2.0/LGPL 2.1 +# +# The contents of this file are subject to the Mozilla Public License Version +# 1.1 (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. +# +# The Original Code is Mozilla Transaction Manager. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corp. +# Portions created by the Initial Developer are Copyright (C) 2003 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# John Gaunt <jgaunt@netscape.com> +# +# Alternatively, the contents of this file may be used under the terms of +# either the GNU General Public License Version 2 or later (the "GPL"), or +# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), +# in which case the provisions of the GPL or the LGPL are applicable instead +# of those above. If you wish to allow use of your version of this file only +# under the terms of either the GPL or the LGPL, and not to allow others to +# use your version of this file under the terms of the MPL, indicate your +# decision by deleting the provisions above and replace them with the notice +# and other provisions required by the GPL or the LGPL. If you do not delete +# the provisions above, a recipient may use your version of this file under +# the terms of any one of the MPL, the GPL or the LGPL. +# +# ***** END LICENSE BLOCK ***** + +DEPTH = ../../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = ipcd +LIBRARY_NAME = transmgrcom_s +FORCE_STATIC_LIB = 1 +MODULE_NAME = ipcd + +FORCE_USE_PIC = 1 + +REQUIRES = \ + xpcom \ + $(NULL) + +CPPSRCS = \ + tmTransaction.cpp \ + tmVector.cpp \ + $(NULL) + +include $(topsrcdir)/config/rules.mk diff --git a/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmTransaction.cpp b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmTransaction.cpp new file mode 100644 index 00000000..5fb7e5bf --- /dev/null +++ b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmTransaction.cpp @@ -0,0 +1,107 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Transaction Manager. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * John Gaunt <jgaunt@netscape.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include <stdlib.h> +#include "tmTransaction.h" +#ifdef VBOX_USE_IPRT_IN_XPCOM +# include <iprt/mem.h> +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Constructor(s) & Destructor + +tmTransaction::~tmTransaction() { + if (mHeader) +#ifdef VBOX_USE_IPRT_IN_XPCOM + RTMemFree(mHeader); +#else + free(mHeader); +#endif +} + +// call only once per lifetime of object. does not reclaim the +// raw message, only sets it. +nsresult +tmTransaction::Init(PRUint32 aOwnerID, + PRInt32 aQueueID, + PRUint32 aAction, + PRInt32 aStatus, + const PRUint8 *aMessage, + PRUint32 aLength) { + nsresult rv = NS_OK; + tmHeader *header = nsnull; + + // indicates the message is the entire raw message + if (aQueueID == TM_INVALID_ID) { +#ifdef VBOX_USE_IPRT_IN_XPCOM + header = (tmHeader*) RTMemAlloc(aLength); +#else + header = (tmHeader*) malloc(aLength); +#endif + if (header) { + mRawMessageLength = aLength; + memcpy(header, aMessage, aLength); + } + else + rv = NS_ERROR_OUT_OF_MEMORY; + } + else { // need to create the tmHeader and concat the message +#ifdef VBOX_USE_IPRT_IN_XPCOM + header = (tmHeader*) RTMemAlloc (sizeof(tmHeader) + aLength); +#else + header = (tmHeader*) malloc (sizeof(tmHeader) + aLength); +#endif + if (header) { + mRawMessageLength = sizeof(tmHeader) + aLength; + header->action = aAction; + header->queueID = aQueueID; + header->status = aStatus; + header->reserved = 0x00000000; + if (aLength > 0) // add the message if it exists + memcpy((header + 1), aMessage, aLength); + } + else + rv = NS_ERROR_OUT_OF_MEMORY; + } + + if (NS_SUCCEEDED(rv)) { + mOwnerID = aOwnerID; + mHeader = header; + } + + return rv; +} diff --git a/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmTransaction.h b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmTransaction.h new file mode 100644 index 00000000..b86f20f0 --- /dev/null +++ b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmTransaction.h @@ -0,0 +1,234 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Transaction Manager. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * John Gaunt <jgaunt@netscape.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef _tmTransaction_H_ +#define _tmTransaction_H_ + +#include "tmUtils.h" + +////////////////////////////////////////////////////////////////////////////// +// +// Message format +// +// |------------------------------------|-- +// |QueueID | | +// |------------------------------------| | +// | Action - Post/Flush/Attach etc | |- this is the tmHeader struct +// |------------------------------------| | +// |Status | | +// |------------------------------------| | +// |Padding | | +// |------------------------------------|-- +// |Message Data (payload) | +// |------------------------------------| +// +// The Attach call is a special case in that it doesn't have a QueueID yet. A +// QueueID will be 0's. The message Data will be the Queue Name String which +// will be the profile name with a domain attached, a domain being +// [prefs|cookies|etc] +// +////////////////////////////////////////////////////////////////////////////// + +/** + * tmHeader contains various flags identifying + */ +struct tmHeader { + PRInt32 queueID; // will be the index of the queue in the TM, can be < 0 + PRUint32 action; // defined by tmUtils.h will be > 0 + PRInt32 status; // return values from methods, could be < 0 + PRUint32 reserved; // not currently used, maintaining word alignment +}; + +/** + * Using tmTransaction: + * + * After creating a tmTransaction either through new or as a member + * or local variable a process must call Init() with the proper set of + * arguements to initialize the state of the transaction. tmTransaction is + * set up to accept 3 types of initialization. + * + * 1) Raw message - All data is carried in the byte pointer aMessage, + * args 2,3,4 should be set to TM_NO_ID and aLength + * must be set to the full length of aMessage, including null + * termination if the payload is a null-term string and the size of the + * tmHeader struct preceeding the message. Currently this + * format is used at the IPC boundary, where we receive a byte pointer + * from the IPC Daemon. + * + * 2) Flags only - aQueueID, aAction and aStatus are all set. aMessage + * should be set to nsnull and aLength to 0. This format is used when + * sending reply messages (except for ATTACH_REPLY) and when the TS + * Transaction Service is sending "control" messages to the Manager - + * flush, detach, etc... + * + * 3) Flags and message - All arguements are set. The aMessage is only + * the message for the client app. aLength should be set to the length + * of aMessage and not include the length of the tmHeader struct. + * + * The only data member you can set post initialization is the QueueID. + * You should only call Init() once in the lifetime of a tmTransaction + * as it doesn't clean up the exisiting data before assigning the new + * data. Therefore it would leak most heinously if Init() were to be + * called twice. + * + * mOwnerID only has relevance on the IPC daemon side of things. The + * Transaction Service has no knowledge of this ID and makes no use + * of it. + */ +class tmTransaction +{ + +public: + + //////////////////////////////////////////////////////////////////////////// + // Constructor(s) & Destructor + + tmTransaction(): mHeader(nsnull), mRawMessageLength(0), mOwnerID(0) { } + + virtual ~tmTransaction(); + + //////////////////////////////////////////////////////////////////////////// + // Public Member Functions + + // Initializer //////////// + + /** + * Sets up the internal data of the transaction. Allows for 3 basic ways + * to call this function: No flags and just one big raw message, Just + * flags and no message, and finally flags and message. If the message + * exists it is copied into the transaction. + * + * @param aOwnerID is given to us by the IPC Daemon and is specific + * to that transport layer. It is only set when transactions + * are sent from the TM to the TS. + * + * @param aQueueID is the either the destination queue, or the queue from + * where this transaction is eminating + * + * @param aAction is the action that occured to generate this transaction + * + * @param aStatus is the success state of the action. + * + * @param aMessage can be a raw message including the 3 flags above or it + * can be just the "payload" of the transaction that the destination + * process is going deal with. + * + * @param aLength is the length of the message. If there is a null + * terminated string in the message make sure the length includes + * the null termination. + * + * @returns NS_OK if everything was successful + * @returns NS_ERROR_OUT_OF_MEMORY if allocation of space for the + * copy of the message fails. + */ + nsresult Init(PRUint32 aOwnerID, + PRInt32 aQueueID, + PRUint32 aAction, + PRInt32 aStatus, + const PRUint8 *aMessage, + PRUint32 aLength); + + // Data Accessors ///////// + + /** + * @returns a const byte pointer to the message + */ + const PRUint8* GetMessage() const { return (PRUint8*)(mHeader + 1); } + + /** + * @returns the length of the message + */ + PRUint32 GetMessageLength() const { + return (mRawMessageLength > sizeof(tmHeader)) ? + (mRawMessageLength - sizeof(tmHeader)) : 0; + } + + /** + * @returns a const pointer to the memory containing the + * flag information followed immediately by the message + * data. + */ + const PRUint8* GetRawMessage() const { return (PRUint8*) mHeader; } + + /** + * @returns the length of the flags and message combined + */ + PRUint32 GetRawMessageLength() const { return mRawMessageLength; } + + /** + * @returns the id of the destination or sending queue, depending on the + * direction of the transaction. + */ + PRInt32 GetQueueID() const { return mHeader->queueID; } + + /** + * @returns the action represented by this transaction + */ + PRUint32 GetAction() const { return mHeader->action; } + + /** + * @returns the success state, if applicable of the action leading + * up to this message + */ + PRInt32 GetStatus() const { return mHeader->status; } + + /** + * @returns the client ID (in IPC daemon terms) of the client who initiated + * the exchange that generated this transaction. + */ + PRUint32 GetOwnerID() const { return mOwnerID; } + + // Data Mutator /////////// + + /** + * Sets the ID of the destination or source queue. Init should have been + * called before the call to this function. + */ + void SetQueueID(PRInt32 aID) { mHeader->queueID = aID; } + +protected: + + //////////////////////////////////////////////////////////////////////////// + // Protected Member Variables + + tmHeader* mHeader; // points to beginning of entire message + PRUint32 mRawMessageLength; // length of entire message, incl tmHeader + PRUint32 mOwnerID; // client who sent this trans. - a IPC ClientID + +}; + +#endif diff --git a/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmUtils.h b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmUtils.h new file mode 100644 index 00000000..f85184d3 --- /dev/null +++ b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmUtils.h @@ -0,0 +1,93 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Transaction Manager. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * John Gaunt <jgaunt@netscape.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef _tmUtils_H_ +#define _tmUtils_H_ + +#include "nscore.h" +#include "nsError.h" +#include "nsID.h" +#include "prlog.h" +#include <stdio.h> + +// UUID used to identify the Transaction Module in both daemon and client +// not part of the XPCOM hooks, but rather a means of identifying +// modules withing the IPC daemon. +#define TRANSACTION_MODULE_ID \ +{ /* c3dfbcd5-f51d-420b-abf4-3bae445b96a9 */ \ + 0xc3dfbcd5, \ + 0xf51d, \ + 0x420b, \ + {0xab, 0xf4, 0x3b, 0xae, 0x44, 0x5b, 0x96, 0xa9} \ +} + +//static const nsID kTransModuleID = TRANSACTION_MODULE_ID; + +/////////////////////////////////////////////////////////////////////////////// +// match NS_ERROR_FOO error code formats +// +// only create new errors for those errors that are specific to TM + +#define NS_ERROR_MODULE_TM 27 /* XXX goes in nserror.h -- integrating with ns error codes */ + +#define TM_ERROR NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 1) +#define TM_ERROR_WRONG_QUEUE NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 2) +#define TM_ERROR_NOT_POSTED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 3) +#define TM_ERROR_QUEUE_EXISTS NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 4) +#define TM_SUCCESS_DELETE_QUEUE NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_TM, 6) + + +// XXX clean up: +#define TM_INVALID_ID 0xFFFFFFFF +#define TM_INVALID 0xFFFFFFFF +#define TM_NO_ID 0xFFFFFFFE + +// Transaction Actions +enum { + TM_ATTACH = 0, + TM_ATTACH_REPLY, + TM_POST, + TM_POST_REPLY, + TM_NOTIFY, + TM_FLUSH, + TM_FLUSH_REPLY, + TM_DETACH, + TM_DETACH_REPLY +}; + +#endif + diff --git a/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmVector.cpp b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmVector.cpp new file mode 100644 index 00000000..99213f27 --- /dev/null +++ b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmVector.cpp @@ -0,0 +1,179 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Transaction Manager. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * John Gaunt <jgaunt@netscape.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include <stdlib.h> +#include "tmVector.h" +#ifdef VBOX_USE_IPRT_IN_XPCOM +# include <iprt/mem.h> +#endif + +//////////////////////////////////////////////////////////////////////////// +// Constructor(s) & Destructor + +// can not be responsible for reclaiming memory pointed to by the void*s in +// the collection - how would we reclaim, don't know how they were allocated +tmVector::~tmVector() { + if (mElements) +#ifdef VBOX_USE_IPRT_IN_XPCOM + RTMemFree((void*)mElements); +#else + free((void*)mElements); +#endif +} + +/////////////////////////////////////////////////////////////////////////////// +// Public Member Functions + +nsresult +tmVector::Init() { + +#ifdef VBOX_USE_IPRT_IN_XPCOM + mElements = (void**) RTMemAllocZ (mCapacity * sizeof(void*)); +#else + mElements = (void**) calloc (mCapacity, sizeof(void*)); +#endif + if (!mElements) + return NS_ERROR_OUT_OF_MEMORY; + return NS_OK; +} + +/////////////////////////////////////////////////////////////////////////////// +// mutators + +PRInt32 +tmVector::Append(void *aElement){ + PR_ASSERT(aElement); + + // make sure there is room + if (mNext == mCapacity) + if (NS_FAILED(Grow())) + return -1; + + // put the element in the array + mElements[mNext] = aElement; + mCount++; + + // encapsulates the index into a success value + return mNext++; // post increment. +} + +void +tmVector::Remove(void *aElement) { + PR_ASSERT(aElement); + + for (PRUint32 index = 0; index < mNext; index++) { + if (mElements[index] == aElement) { + mElements[index] = nsnull; + mCount--; + if (index == mNext-1) { // if we removed the last element + mNext--; + // don't test for success of the shrink + Shrink(); + } + } + } +} + +void +tmVector::RemoveAt(PRUint32 aIndex) { + PR_ASSERT(aIndex < mNext); + + // remove the element if it isn't already nsnull + if (mElements[aIndex] != nsnull) { + mElements[aIndex] = nsnull; + mCount--; + if (aIndex == mNext-1) { // if we removed the last element + mNext--; + // don't test for success of the shrink + Shrink(); + } + } +} + +//void* +//tmVector::operator[](int index) { +// if (index < mNext && index >= 0) +// return mElements[index]; +// return nsnull; +//} + +// Does not delete any of the data, merely removes references to them +void +tmVector::Clear(){ + memset(mElements, 0, mCapacity); + mCount = 0; + mNext = 0; +} + +////////////////////////////////////////////////////////////////////////////// +// Protected Member Functions + +// increases the capacity by the growth increment +nsresult +tmVector::Grow() { + + PRUint32 newcap = mCapacity + GROWTH_INC; +#ifdef VBOX_USE_IPRT_IN_XPCOM + mElements = (void**) RTMemRealloc(mElements, (newcap * sizeof(void*))); +#else + mElements = (void**) realloc(mElements, (newcap * sizeof(void*))); +#endif + if (mElements) { + mCapacity = newcap; + return NS_OK; + } + return NS_ERROR_FAILURE; +} + +// reduces the capacity by the growth increment. leaves room +// for one more add before needing to Grow(). +nsresult +tmVector::Shrink() { + + PRUint32 newcap = mCapacity - GROWTH_INC; + if (mNext < newcap) { +#ifdef VBOX_USE_IPRT_IN_XPCOM + mElements = (void**) RTMemRealloc(mElements, newcap * sizeof(void*)); +#else + mElements = (void**) realloc(mElements, newcap * sizeof(void*)); +#endif + if (!mElements) + return NS_ERROR_OUT_OF_MEMORY; + mCapacity = newcap; + } + return NS_OK; +} diff --git a/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmVector.h b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmVector.h new file mode 100644 index 00000000..4ddb68ef --- /dev/null +++ b/src/libs/xpcom18a4/ipc/ipcd/extensions/transmngr/common/tmVector.h @@ -0,0 +1,160 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Transaction Manager. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2003 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * John Gaunt <jgaunt@netscape.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef _tmVector_H_ +#define _tmVector_H_ + +#include "tmUtils.h" + +#define GROWTH_INC 5 + +/** + * A simple, clear, self-growing, collection of objects. typed independant + * basically a growing array. Useful in situations where you need an + * indexed collection but do not know the size in advance and need the + * ability for increase and decrease in size. Not optimized for anything + * in particular, or any size in particular. + * + * Is able to guarantee the index of an item will + * not change due to removals of a lower indexed item. The growing, + * and shrinking all happens to the end of the collection + * + * Does not backfill, adds to the end. At some point this should be + * changed to make best use of space. + */ +class tmVector +{ +public: + + //////////////////////////////////////////////////////////////////////////// + // Constructor(s) & Destructor + + /** + * Set some sane default values to set up the internal storage. Init() + * must be called after construction of the object to allcate the + * backing store. + */ + tmVector() : mNext(0), mCount(0), mCapacity(10), mElements(nsnull) {;} + + /** + * Reclaim the memory allocated in the Init() method. + */ + virtual ~tmVector(); + + //////////////////////////////////////////////////////////////////////////// + // Public Member Functions + + /** + * Allocates the storage back-end + * + * @returns NS_OK if allocation succeeded + * @returns NS_ERROR_OUT_OF_MEMORY if the allocation failed + */ + nsresult Init(); + + // mutators + + /** + * @returns the index of the element added, if successful + * @returns -1 if an error occured during allocation of space + */ + PRInt32 Append(void *aElement); + + /** + * This does not collapse the collection, it leaves holes. Note, it also + * doesn't delete the element, it merely removes it from the collection + */ + void Remove(void *aElement); + + /** + * This does not collapse the collection, it leaves holes. Note, it also + * doesn't delete the element, it merely removes it from the collection + */ + void RemoveAt(PRUint32 aIndex); + + /** + * Does not call delete on the elements since we have no idea how to + * reclaim the memory. Sets all array slots to 0. + */ + void Clear(); + + /** + * @returns the element at the index indicated, including nsnull if the + * slot is empty. + */ + void* operator[](PRUint32 index) { + PR_ASSERT(index < mNext); + return mElements[index]; + } + + /** + * @returns the number of elements stored + */ + PRUint32 Count() { return mCount; } + + /** + * Meant to be used as the conditional in a loop. |index < size| should + * reach all elements of the collection and not run out of bounds. If + * slots 0,1,4,5,6 contain elements Size() will return 7, Count() will + * return 5. + * + * @returns the number of slots in the array taken, irrespective of + * holes in the collection. + */ + PRUint32 Size() { return mNext; } + +protected: + + nsresult Grow(); // mCapacity += GROWTH_INC - realloc()s + nsresult Shrink(); // mCapacity -= GROWTH_INC - dumb, free, malloc + + //////////////////////////////////////////////////////////////////////////// + // Protected Member Variables + + // bookkeeping variables + PRUint32 mNext; // next element insertion slot (0 based) + PRUint32 mCount; // how many elements in the Vector (1 based) + PRUint32 mCapacity; // current capacity of the Vector (1 based) + + // the actual array of objects being stored + void **mElements; + +private: + +}; + +#endif |