From cecb3c2ef93336b152fe95c6b6674d139a4be17b Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Thu, 28 Feb 2002 00:01:07 +0000 Subject: [PATCH] improved memory management --- src/rsrv/camessage.c | 23 +++++++++++++++++------ src/rsrv/caservertask.c | 9 ++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/rsrv/camessage.c b/src/rsrv/camessage.c index ecb2c6e46..385e6654d 100644 --- a/src/rsrv/camessage.c +++ b/src/rsrv/camessage.c @@ -31,8 +31,10 @@ #include #include #include +#include #include "osiSock.h" +#include "osiPoolStatus.h" #include "epicsEvent.h" #include "epicsThread.h" #include "epicsMutex.h" @@ -69,7 +71,7 @@ logBadIdWithFileAndLineno(CLIENT, MP, PPL, __FILE__, __LINE__) */ LOCAL void *casMalloc(size_t size) { - if (!casSufficentSpaceInPool) { + if (!osiSufficentSpaceInPool(size)) { return NULL; } return malloc(size); @@ -83,10 +85,15 @@ LOCAL void *casMalloc(size_t size) */ LOCAL void *casCalloc(size_t count, size_t size) { - if (!casSufficentSpaceInPool) { + if ( UINT_MAX / size >= count ) { + if (!osiSufficentSpaceInPool(size*count)) { + return NULL; + } + return calloc(count, size); + } + else { return NULL; } - return calloc(count, size); } /* @@ -1628,7 +1635,7 @@ LOCAL int event_add_action (caHdrLargeArray *mp, void *pPayload, struct client * * stop further use of server if memory becomes scarse */ spaceAvailOnFreeList = freeListItemsAvail ( rsrvEventFreeList ) > 0; - if ( casSufficentSpaceInPool || spaceAvailOnFreeList ) { + if ( osiSufficentSpaceInPool(sizeof(*pevext)) || spaceAvailOnFreeList ) { pevext = (struct event_ext *) freeListCalloc (rsrvEventFreeList); } else { @@ -1951,6 +1958,8 @@ LOCAL int search_reply ( caHdrLargeArray *mp, void *pPayload, struct client *cli ca_uint16_t count; ca_uint16_t type; int spaceAvailOnFreeList; + size_t spaceNeeded; + size_t reasonableMonitorSpace = 10; /* * check the sanity of the message @@ -1975,8 +1984,10 @@ LOCAL int search_reply ( caHdrLargeArray *mp, void *pPayload, struct client *cli * stop further use of server if memory becomes scarse */ spaceAvailOnFreeList = freeListItemsAvail ( rsrvChanFreeList ) > 0 - && freeListItemsAvail ( rsrvEventFreeList ) > 10; - if ( ! ( casSufficentSpaceInPool || spaceAvailOnFreeList ) ) { + && freeListItemsAvail ( rsrvEventFreeList ) > reasonableMonitorSpace; + spaceNeeded = sizeof (struct channel_in_use) + + reasonableMonitorSpace * sizeof (struct event_ext); + if ( ! ( osiSufficentSpaceInPool(spaceNeeded) || spaceAvailOnFreeList ) ) { SEND_LOCK(client); send_err ( mp, ECA_ALLOCMEM, client, "Server memory exhausted" ); SEND_UNLOCK(client); diff --git a/src/rsrv/caservertask.c b/src/rsrv/caservertask.c index 051ce2697..b9978ad5e 100644 --- a/src/rsrv/caservertask.c +++ b/src/rsrv/caservertask.c @@ -37,6 +37,7 @@ #include #include "osiSock.h" +#include "osiPoolStatus.h" #include "epicsEvent.h" #include "epicsMutex.h" #include "epicsTime.h" @@ -630,13 +631,15 @@ struct client * create_client ( SOCKET sock, int proto ) { struct client *client; int spaceAvailOnFreeList; + size_t spaceNeeded; /* * stop further use of server if memory becomes scarse */ spaceAvailOnFreeList = freeListItemsAvail ( rsrvClientFreeList ) > 0 && freeListItemsAvail ( rsrvSmallBufFreeListTCP ) > 0; - if ( ! ( casSufficentSpaceInPool || spaceAvailOnFreeList ) ) { + spaceNeeded = sizeof (struct client) + MAX_TCP; + if ( ! ( osiSufficentSpaceInPool(spaceNeeded) || spaceAvailOnFreeList ) ) { epicsPrintf ("CAS: no space in pool for a new client (below max block thresh)\n"); return NULL; } @@ -714,7 +717,7 @@ void casExpandSendBuffer ( struct client *pClient, ca_uint32_t size ) if ( pClient->send.type == mbtSmallTCP && rsrvSizeofLargeBufTCP > MAX_TCP && size <= rsrvSizeofLargeBufTCP ) { int spaceAvailOnFreeList = freeListItemsAvail ( rsrvLargeBufFreeListTCP ) > 0; - if ( casSufficentSpaceInPool || spaceAvailOnFreeList ) { + if ( osiSufficentSpaceInPool(rsrvSizeofLargeBufTCP) || spaceAvailOnFreeList ) { char *pNewBuf = ( char * ) freeListCalloc ( rsrvLargeBufFreeListTCP ); if ( pNewBuf ) { memcpy ( pNewBuf, pClient->send.buf, pClient->send.stk ); @@ -732,7 +735,7 @@ void casExpandRecvBuffer ( struct client *pClient, ca_uint32_t size ) if ( pClient->recv.type == mbtSmallTCP && rsrvSizeofLargeBufTCP > MAX_TCP && size <= rsrvSizeofLargeBufTCP) { int spaceAvailOnFreeList = freeListItemsAvail ( rsrvLargeBufFreeListTCP ) > 0; - if ( casSufficentSpaceInPool || spaceAvailOnFreeList ) { + if ( osiSufficentSpaceInPool(rsrvSizeofLargeBufTCP) || spaceAvailOnFreeList ) { char *pNewBuf = ( char * ) freeListCalloc ( rsrvLargeBufFreeListTCP ); if ( pNewBuf ) { assert ( pClient->recv.cnt >= pClient->recv.stk );