ClPtrT clMemPartRealloc(ClMemPartHandleT handle, ClPtrT memBase, ClUint32T size)
{
    MEM_PART_STATS memPartStats;
    ClPtrT mem = NULL;
    ClMemPartT *pMemPart = NULL;
    ClRcT rc = CL_OK;

    if(!(pMemPart = (ClMemPartT*)handle) )
        return NULL;

    if(!size) size = 16;

    clOsalMutexLock(&pMemPart->mutex);
    if(memPartInfoGet(pMemPart->partId, &memPartStats) == ERROR)
    {
        clOsalMutexUnlock(&pMemPart->mutex);
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("memPartInfoGet for size [%d] failed with [%s]\n", size, strerror(errno)));
        return mem;
    }
    if(size >= memPartStats.numBytesFree)
    {
        if( (rc = clMemPartExpand(pMemPart, CL_MEM_PART_EXPANSION_SIZE) ) != CL_OK)
        {
            /* do nothing now and fall back to realloc.*/
        }
    }
    mem = memPartRealloc(pMemPart->partId, memBase, size);
    clOsalMutexUnlock(&pMemPart->mutex);
    if(!mem)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Mem part realloc failure for size [%d]\n", size));
        CL_ASSERT(0);
    }
    return mem;
}
Exemplo n.º 2
0
static void read_mem_stats(void)
{
	time_t nt;
	time(&nt);

	if((nt-scan[MEMORY_TYPE].last_read_sec)>=scan[MEMORY_TYPE].rate_sec)
	{
		if(memPartInfoGet(memSysPartId,&meminfo)==OK)
			scan[MEMORY_TYPE].last_read_sec=nt;
	}
}
ClPtrT clMemPartAlloc(ClMemPartHandleT handle, ClUint32T size)
{
    ClPtrT mem = NULL;
    ClMemPartT *pMemPart = NULL;
    MEM_PART_STATS memStats;
    ClRcT rc = CL_OK;

    if(!(pMemPart = (ClMemPartT*)handle))
        return NULL;

    if(!size)
        size = 16;/*min size to alloc*/

    clOsalMutexLock(&pMemPart->mutex);
    if(memPartInfoGet(pMemPart->partId, &memStats) == ERROR)
    {
        clOsalMutexUnlock(&pMemPart->mutex);
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("memPartInfoGet failed with [%s]\n", strerror(errno)));
        return mem;
    }
    /*
     * Check if we are hittin the roof. Expand
     */
    if(size >= memStats.numBytesFree)
    {
        if((rc = clMemPartExpand(pMemPart, CL_MEM_PART_EXPANSION_SIZE)) != CL_OK)
        {
            /* ignore and fail on memPartAlloc failure*/
        }
    }
    mem = memPartAlloc(pMemPart->partId, size);
    clOsalMutexUnlock(&pMemPart->mutex);
    if(!mem)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Critical memPartAlloc error for size [%d]\n", size));
        CL_ASSERT(0);
    }
    memset(mem, 0, size);
    return mem;
}
Exemplo n.º 4
0
/**************************************************************
*
* mBufferGet - get the next available buffer 
*
*
*  This routine returns the next available buffer address.
*
*
* RETURNS:
*  The Address of a available buffer.
*
*       Author Greg Brissey 3/02/04
*/
char *mBufferGet(register MBUFFER_ID mBufferId, unsigned int size)
/* MBUFFER_ID mBufferId - Fast Buffer to allocate from */
{
  int i,stat;
  long *indxAddr;
  char *bufAddr;
  register int toP;
  MEM_PART_STATS  memStats;

#ifdef INSTRUMENT
     wvEvent(EVENT_FBUF_GET,NULL,NULL);
#endif

  taskLock();  /* critical code */

  bufAddr = 0;
  for ( i=0; i < mBufferId->numLists; i++)
  {
      DPRINT2(1,"mBufferGet: size %d, cluster size %d\n",size,mBufferId->listSizes[i]);
      if ( (size+16) < mBufferId->listSizes[i] )
      {
	stat = RNG_LONG_GET(mBufferId->freeList[i], (long*) &bufAddr,toP);
        DPRINT3(1," mBufferGet: stat %d, Cluster: %d, Addr: 0x%lx\n",stat, mBufferId->listSizes[i],bufAddr);
	if ( stat != 0)
        {
           DPRINT(1," mBufferGet(): Obtain cluster entry\n");
           mBufferId->clCnt[i] += 1;
           taskUnlock();
           return(bufAddr);
        }
        else
        {
           mBufferId->clFailCnt[i] += 1;
           continue;  /* if we want to try next size up */
           /* return(NULL); */
        }
      }
  }

  memPartInfoGet(memSysPartId,&memStats);
  DPRINT2(0," MemStat's Alloc'd: %lu bytes, Free: %lu bytes\n", memStats.numBytesAlloc, memStats.numBytesFree);
  /* DPRINT2(-1," Alloc: %lu blocks, Free: %lu blocks\n", memStats.numBlocksAlloc, memStats.numBlocksFree); */

  DPRINT(0," mBufferGet(): No clusters, Malloc buffer\n");
  /* bufAddr = malloc( size + sizeof(long) ); */
  /* bufAddr = (char *) memalign(4, size + sizeof(long) );  /* align to 4 bytes */
  /* if we are going over the limit i.e. will drive memory free below memReserveLevel, then FAIL */
  if ( (memStats.numBytesFree  - size) >  mBufferId->memReserveLevel)
  {
    bufAddr = (char *) memalign(_CACHE_ALIGN_SIZE, size + sizeof(long) );  /* align to 4 bytes */
    if (bufAddr != NULL)
    {
      indxAddr = (long *) bufAddr;
      *indxAddr = 0xbada110c;     /*99  in front of buffer place size index */
      bufAddr += sizeof(long);
      mBufferId->numMallocs++;
      DPRINT3(1,"Malloc: indx: 0x%lx, val: %ld, bufAddr: 0x%lx\n", indxAddr, *indxAddr, bufAddr);
      if (size > mBufferId->maxMalloc)
         mBufferId->maxMalloc = size;
    }
  }
  else
  {
    DPRINT1(-1," mBufferGet(): to malloc would drive free system memory below reserve of: %lu bytes\n", mBufferId->memReserveLevel);
    bufAddr = NULL;
  }

  taskUnlock();
  return(bufAddr);
}