improved memory management
This commit is contained in:
@@ -31,8 +31,10 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <limits.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include <errno.h>
|
||||
|
||||
#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 );
|
||||
|
||||
Reference in New Issue
Block a user