Plugged a memory leak (thanks Matthijs!)

Removed the use of malloc() -- deprecated
Deprecated PortLink::FlushToSession()


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@7906 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
DarkWyrm
2004-06-11 13:38:08 +00:00
parent c73fb70eb7
commit 768ec791c9
2 changed files with 36 additions and 45 deletions
+17 -17
View File
@@ -21,7 +21,7 @@
// //
// File Name: PortLink.cpp // File Name: PortLink.cpp
// Author: DarkWyrm <[email protected]> // Author: DarkWyrm <[email protected]>
// Description: Class for low-overhead port-based messaging // Description: Class for low-overhead packet-style port-based messaging
// //
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include <stdlib.h> #include <stdlib.h>
@@ -29,18 +29,17 @@
#include <ServerProtocol.h> #include <ServerProtocol.h>
#include "PortLink.h" #include "PortLink.h"
#include "PortMessage.h" #include "PortMessage.h"
#include "Session.h"
PortLink::PortLink(port_id port) PortLink::PortLink(port_id port)
{ {
port_info pi; port_info pi;
port_ok = (get_port_info(port, &pi)==B_OK)? true: false; fPortValid = (get_port_info(port, &pi)==B_OK)? true: false;
fSendPort = port; fSendPort = port;
fReceivePort = create_port(30,"PortLink reply port"); fReceivePort = create_port(30,"PortLink reply port");
fSendCode = 0; fSendCode = 0;
fSendBuffer = (char*)malloc(4096);//new char[4096]; fSendBuffer = new char[SESSION_BUFFER_SIZE * 4];
fSendPosition = 8; fSendPosition = 8;
fDataSize = (int32*)(fSendBuffer+sizeof(int32)); fDataSize = (int32*)(fSendBuffer+sizeof(int32));
*fDataSize = 0; *fDataSize = 0;
@@ -48,13 +47,13 @@ PortLink::PortLink(port_id port)
PortLink::PortLink( const PortLink &link ) PortLink::PortLink( const PortLink &link )
{ {
port_ok = link.port_ok; fPortValid = link.fPortValid;
fSendPort = link.fSendPort; fSendPort = link.fSendPort;
fReceivePort = create_port(30,"PortLink reply port"); fReceivePort = create_port(30,"PortLink reply port");
fSendCode = 0; fSendCode = 0;
fSendBuffer = (char*)malloc(4096);//new char[4096]; fSendBuffer = new char[SESSION_BUFFER_SIZE * 4];
fSendPosition = 8; fSendPosition = 8;
fDataSize = (int32*)(fSendBuffer+sizeof(int32)); fDataSize = (int32*)(fSendBuffer+sizeof(int32));
*fDataSize = 0; *fDataSize = 0;
@@ -62,7 +61,7 @@ PortLink::PortLink( const PortLink &link )
PortLink::~PortLink(void) PortLink::~PortLink(void)
{ {
free(fSendBuffer);//delete [] fSendBuffer; delete [] fSendBuffer;
} }
void PortLink::SetOpCode( int32 code ) void PortLink::SetOpCode( int32 code )
@@ -77,7 +76,7 @@ void PortLink::SetPort( port_id port )
port_info pi; port_info pi;
fSendPort=port; fSendPort=port;
port_ok=(get_port_info(port, &pi) == B_OK)? true: false; fPortValid=(get_port_info(port, &pi) == B_OK)? true: false;
} }
port_id PortLink::GetPort() port_id PortLink::GetPort()
@@ -89,7 +88,7 @@ status_t PortLink::Flush(bigtime_t timeout)
{ {
status_t write_stat; status_t write_stat;
if(!port_ok) if(!fPortValid)
return B_BAD_VALUE; return B_BAD_VALUE;
if(timeout!=B_INFINITE_TIMEOUT) if(timeout!=B_INFINITE_TIMEOUT)
@@ -106,7 +105,7 @@ status_t PortLink::Flush(bigtime_t timeout)
status_t PortLink::FlushWithReply( PortMessage *msg,bigtime_t timeout ) status_t PortLink::FlushWithReply( PortMessage *msg,bigtime_t timeout )
{ {
if(!port_ok || !msg) if(!fPortValid || !msg)
return B_BAD_VALUE; return B_BAD_VALUE;
// attach our reply port_id at the end // attach our reply port_id at the end
@@ -123,12 +122,13 @@ status_t PortLink::FlushWithReply( PortMessage *msg,bigtime_t timeout )
return B_OK; return B_OK;
} }
status_t PortLink::FlushToSession(){ // Deprecated compatibility hack added to allow PortLink to send messages in a fashion
BSession ses(0, fSendPort); // like BSession. This was because originally there were differences in how they sent
// !!!!!!!!!!!!!!!!!!! // messages across ports. This is no longer the case, so this call should never need to be
// DW, if you modify PortLink, DON'T forget to modify // made. It remains only until current calls to it can be removed.
// BSession::CopyToSendBuffer() as well!!! status_t PortLink::FlushToSession()
// !!!!!!!!!!!!!!!!!!! {
BSession ses(0, fSendPort);
ses.CopyToSendBuffer(fSendBuffer, fSendPosition - 8); ses.CopyToSendBuffer(fSendBuffer, fSendPosition - 8);
ses.Sync(); ses.Sync();
return B_OK; return B_OK;
@@ -139,7 +139,7 @@ status_t PortLink::Attach(const void *data, size_t size)
if (size <= 0) if (size <= 0)
return B_ERROR; return B_ERROR;
if (4096 - fSendPosition > (int32)size) if (SESSION_BUFFER_SIZE - fSendPosition > (int32)size)
{ {
memcpy(fSendBuffer + fSendPosition, data, size); memcpy(fSendBuffer + fSendPosition, data, size);
fSendPosition += size; fSendPosition += size;
+19 -28
View File
@@ -36,8 +36,6 @@
#endif #endif
*/ */
#define BUFFER_SIZE 1024
/*! /*!
\brief Standard constructor \brief Standard constructor
\param receivePort ID of the port the BSession is to receive messages from \param receivePort ID of the port the BSession is to receive messages from
@@ -52,11 +50,11 @@ BSession::BSession(port_id receivePort, port_id sendPort)
fSendPosition = 0; fSendPosition = 0;
fReceiveBuffer = NULL; fReceiveBuffer = NULL;
fReceiveSize = BUFFER_SIZE; fReceiveSize = SESSION_BUFFER_SIZE;
fReceivePosition = BUFFER_SIZE; fReceivePosition = SESSION_BUFFER_SIZE;
fSendBuffer = new char[BUFFER_SIZE]; fSendBuffer = new char[SESSION_BUFFER_SIZE];
fReceiveBuffer = new char[BUFFER_SIZE]; fReceiveBuffer = new char[SESSION_BUFFER_SIZE];
} }
/*! /*!
@@ -74,18 +72,18 @@ BSession::BSession( const BSession &ses)
fSendPosition = 0; fSendPosition = 0;
fReceiveBuffer = NULL; fReceiveBuffer = NULL;
fReceiveSize = BUFFER_SIZE; fReceiveSize = SESSION_BUFFER_SIZE;
fReceivePosition = BUFFER_SIZE; fReceivePosition = SESSION_BUFFER_SIZE;
fSendBuffer = new char[BUFFER_SIZE]; fSendBuffer = new char[SESSION_BUFFER_SIZE];
fReceiveBuffer = new char[BUFFER_SIZE]; fReceiveBuffer = new char[SESSION_BUFFER_SIZE];
} }
//! Deletes the allocated messaging buffers. Ports used are not deleted. //! Deletes the allocated messaging buffers. Ports used are not deleted.
BSession::~BSession(void) BSession::~BSession(void)
{ {
delete fSendBuffer; delete [] fSendBuffer;
delete fReceiveBuffer; delete [] fReceiveBuffer;
} }
/*! /*!
@@ -344,23 +342,23 @@ void BSession::ReadData( void *data, int32 size)
if (portSize == B_BAD_PORT_ID) if (portSize == B_BAD_PORT_ID)
return; return;
else else
fReceiveSize= (portSize > BUFFER_SIZE)? BUFFER_SIZE: portSize; fReceiveSize= (portSize > SESSION_BUFFER_SIZE)? SESSION_BUFFER_SIZE: portSize;
ssize_t rv; ssize_t rv;
do do
{ {
rv = read_port(fReceivePort, &fRecvCode, fReceiveBuffer, BUFFER_SIZE); rv = read_port(fReceivePort, &fRecvCode, fReceiveBuffer, SESSION_BUFFER_SIZE);
#ifdef DEBUG_BSESSION #ifdef DEBUG_BSESSION
if (fRecvCode != AS_SERVER_SESSION) if (fRecvCode != AS_SERVER_SESSION && fRecvCodw != AS_SERVER_PORTLINK)
{ {
printf("BSession received a code DIFFERENT from AS_SERVER_SESSION. "); printf("BSession received a message not using BSession/PortLink protocol");
printf("offset: %ld", fRecvCode - SERVER_TRUE); printf("offset: %ld", fRecvCode - SERVER_TRUE);
} }
printf("did read something...\n"); printf("did read something...\n");
#endif #endif
} while( fRecvCode != AS_SERVER_SESSION ); } while( fRecvCode != AS_SERVER_SESSION && fRecvCode != AS_SERVER_PORTLINK );
fReceivePosition = 0; fReceivePosition = 0;
@@ -599,7 +597,7 @@ void BSession::WriteData(const void *data, int32 size)
if (size <= 0) if (size <= 0)
return; return;
if (BUFFER_SIZE - fSendPosition > size) if (SESSION_BUFFER_SIZE - fSendPosition > size)
{ {
memcpy(fSendBuffer + fSendPosition, data, size); memcpy(fSendBuffer + fSendPosition, data, size);
fSendPosition += size; fSendPosition += size;
@@ -609,7 +607,7 @@ void BSession::WriteData(const void *data, int32 size)
status_t rv; status_t rv;
do do
{ {
int32 copySize = BUFFER_SIZE - fSendPosition; int32 copySize = SESSION_BUFFER_SIZE - fSendPosition;
if (size < copySize) if (size < copySize)
copySize = size; copySize = size;
@@ -624,7 +622,7 @@ void BSession::WriteData(const void *data, int32 size)
size -= copySize; size -= copySize;
fSendPosition += copySize; fSendPosition += copySize;
if (fSendPosition != BUFFER_SIZE) if (fSendPosition != SESSION_BUFFER_SIZE)
return; return;
rv = write_port(fSendPort, AS_SERVER_SESSION, fSendBuffer, fSendPosition); rv = write_port(fSendPort, AS_SERVER_SESSION, fSendBuffer, fSendPosition);
@@ -649,13 +647,6 @@ void BSession::Sync(void)
fSendPosition = 0; fSendPosition = 0;
} }
//! Flushes the message cache and deletes the BSession object
void BSession::Close(void)
{
Sync();
delete this;
}
/*! /*!
\brief Directly copies the passed buffer to the message buffer \brief Directly copies the passed buffer to the message buffer
\param buffer The buffer to copy \param buffer The buffer to copy
@@ -679,7 +670,7 @@ void BSession::CopyToSendBuffer(void* buffer, int32 count)
fSendPosition = sizeof(int32); // = 4 fSendPosition = sizeof(int32); // = 4
int32 compensated_buffer_size=BUFFER_SIZE-sizeof(int32); int32 compensated_buffer_size=SESSION_BUFFER_SIZE-sizeof(int32);
memcpy( fSendBuffer + sizeof(int32), memcpy( fSendBuffer + sizeof(int32),
buf + 2*sizeof(int32), buf + 2*sizeof(int32),