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

View File

@ -21,7 +21,7 @@
//
// File Name: PortLink.cpp
// Author: DarkWyrm <bpmagic@columbus.rr.com>
// Description: Class for low-overhead port-based messaging
// Description: Class for low-overhead packet-style port-based messaging
//
//------------------------------------------------------------------------------
#include <stdlib.h>
@ -29,18 +29,17 @@
#include <ServerProtocol.h>
#include "PortLink.h"
#include "PortMessage.h"
#include "Session.h"
PortLink::PortLink(port_id port)
{
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;
fReceivePort = create_port(30,"PortLink reply port");
fSendCode = 0;
fSendBuffer = (char*)malloc(4096);//new char[4096];
fSendBuffer = new char[SESSION_BUFFER_SIZE * 4];
fSendPosition = 8;
fDataSize = (int32*)(fSendBuffer+sizeof(int32));
*fDataSize = 0;
@ -48,13 +47,13 @@ PortLink::PortLink(port_id port)
PortLink::PortLink( const PortLink &link )
{
port_ok = link.port_ok;
fPortValid = link.fPortValid;
fSendPort = link.fSendPort;
fReceivePort = create_port(30,"PortLink reply port");
fSendCode = 0;
fSendBuffer = (char*)malloc(4096);//new char[4096];
fSendBuffer = new char[SESSION_BUFFER_SIZE * 4];
fSendPosition = 8;
fDataSize = (int32*)(fSendBuffer+sizeof(int32));
*fDataSize = 0;
@ -62,7 +61,7 @@ PortLink::PortLink( const PortLink &link )
PortLink::~PortLink(void)
{
free(fSendBuffer);//delete [] fSendBuffer;
delete [] fSendBuffer;
}
void PortLink::SetOpCode( int32 code )
@ -77,7 +76,7 @@ void PortLink::SetPort( port_id port )
port_info pi;
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()
@ -89,7 +88,7 @@ status_t PortLink::Flush(bigtime_t timeout)
{
status_t write_stat;
if(!port_ok)
if(!fPortValid)
return B_BAD_VALUE;
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 )
{
if(!port_ok || !msg)
if(!fPortValid || !msg)
return B_BAD_VALUE;
// attach our reply port_id at the end
@ -123,12 +122,13 @@ status_t PortLink::FlushWithReply( PortMessage *msg,bigtime_t timeout )
return B_OK;
}
status_t PortLink::FlushToSession(){
BSession ses(0, fSendPort);
// !!!!!!!!!!!!!!!!!!!
// DW, if you modify PortLink, DON'T forget to modify
// BSession::CopyToSendBuffer() as well!!!
// !!!!!!!!!!!!!!!!!!!
// Deprecated compatibility hack added to allow PortLink to send messages in a fashion
// 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
// made. It remains only until current calls to it can be removed.
status_t PortLink::FlushToSession()
{
BSession ses(0, fSendPort);
ses.CopyToSendBuffer(fSendBuffer, fSendPosition - 8);
ses.Sync();
return B_OK;
@ -139,7 +139,7 @@ status_t PortLink::Attach(const void *data, size_t size)
if (size <= 0)
return B_ERROR;
if (4096 - fSendPosition > (int32)size)
if (SESSION_BUFFER_SIZE - fSendPosition > (int32)size)
{
memcpy(fSendBuffer + fSendPosition, data, size);
fSendPosition += size;

View File

@ -36,8 +36,6 @@
#endif
*/
#define BUFFER_SIZE 1024
/*!
\brief Standard constructor
\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;
fReceiveBuffer = NULL;
fReceiveSize = BUFFER_SIZE;
fReceivePosition = BUFFER_SIZE;
fReceiveSize = SESSION_BUFFER_SIZE;
fReceivePosition = SESSION_BUFFER_SIZE;
fSendBuffer = new char[BUFFER_SIZE];
fReceiveBuffer = new char[BUFFER_SIZE];
fSendBuffer = new char[SESSION_BUFFER_SIZE];
fReceiveBuffer = new char[SESSION_BUFFER_SIZE];
}
/*!
@ -74,18 +72,18 @@ BSession::BSession( const BSession &ses)
fSendPosition = 0;
fReceiveBuffer = NULL;
fReceiveSize = BUFFER_SIZE;
fReceivePosition = BUFFER_SIZE;
fReceiveSize = SESSION_BUFFER_SIZE;
fReceivePosition = SESSION_BUFFER_SIZE;
fSendBuffer = new char[BUFFER_SIZE];
fReceiveBuffer = new char[BUFFER_SIZE];
fSendBuffer = new char[SESSION_BUFFER_SIZE];
fReceiveBuffer = new char[SESSION_BUFFER_SIZE];
}
//! Deletes the allocated messaging buffers. Ports used are not deleted.
BSession::~BSession(void)
{
delete fSendBuffer;
delete fReceiveBuffer;
delete [] fSendBuffer;
delete [] fReceiveBuffer;
}
/*!
@ -344,23 +342,23 @@ void BSession::ReadData( void *data, int32 size)
if (portSize == B_BAD_PORT_ID)
return;
else
fReceiveSize= (portSize > BUFFER_SIZE)? BUFFER_SIZE: portSize;
fReceiveSize= (portSize > SESSION_BUFFER_SIZE)? SESSION_BUFFER_SIZE: portSize;
ssize_t rv;
do
{
rv = read_port(fReceivePort, &fRecvCode, fReceiveBuffer, BUFFER_SIZE);
rv = read_port(fReceivePort, &fRecvCode, fReceiveBuffer, SESSION_BUFFER_SIZE);
#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("did read something...\n");
#endif
} while( fRecvCode != AS_SERVER_SESSION );
} while( fRecvCode != AS_SERVER_SESSION && fRecvCode != AS_SERVER_PORTLINK );
fReceivePosition = 0;
@ -599,7 +597,7 @@ void BSession::WriteData(const void *data, int32 size)
if (size <= 0)
return;
if (BUFFER_SIZE - fSendPosition > size)
if (SESSION_BUFFER_SIZE - fSendPosition > size)
{
memcpy(fSendBuffer + fSendPosition, data, size);
fSendPosition += size;
@ -609,7 +607,7 @@ void BSession::WriteData(const void *data, int32 size)
status_t rv;
do
{
int32 copySize = BUFFER_SIZE - fSendPosition;
int32 copySize = SESSION_BUFFER_SIZE - fSendPosition;
if (size < copySize)
copySize = size;
@ -624,7 +622,7 @@ void BSession::WriteData(const void *data, int32 size)
size -= copySize;
fSendPosition += copySize;
if (fSendPosition != BUFFER_SIZE)
if (fSendPosition != SESSION_BUFFER_SIZE)
return;
rv = write_port(fSendPort, AS_SERVER_SESSION, fSendBuffer, fSendPosition);
@ -649,13 +647,6 @@ void BSession::Sync(void)
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
\param buffer The buffer to copy
@ -679,7 +670,7 @@ void BSession::CopyToSendBuffer(void* buffer, int32 count)
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),
buf + 2*sizeof(int32),