ClRcT clMemPartInitialize(ClMemPartHandleT *pMemPartHandle, ClUint32T memPartSize) { ClCharT *pool = NULL; ClInt32T i; ClRcT rc = CL_OK; ClMemPartT *pMemPart = NULL; if(!pMemPartHandle) return CL_ERR_INVALID_PARAMETER; if(!memPartSize) memPartSize = CL_MEM_PART_SIZE; pMemPart = calloc(1, sizeof(*pMemPart)); CL_ASSERT(pMemPart != NULL); rc= clOsalMutexInit(&pMemPart->mutex); CL_ASSERT(rc == CL_OK); for(i = 0; i < CL_MEM_PART_EXPANSION_SLOTS; ++i) pMemPart->partitions[i] = NULL; pMemPart->index = 0; pool = malloc(memPartSize); if(!pool) { CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("CALLOC failed for size [%d] while trying to create MEM partition\n", memPartSize)); return CL_ERR_NO_MEMORY; } pMemPart->partId = memPartCreate(pool, memPartSize); if(!pMemPart->partId) { free(pool); CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("memPartCreate for size [%d] failed\n", memPartSize)); return CL_ERR_NO_MEMORY; } pMemPart->partitions[pMemPart->index++] = pool; *pMemPartHandle = (ClMemPartHandleT)pMemPart; return CL_OK; }
void * ossPartMalloc ( UINT32 numBytes /* Size of buffer to allocate */ ) { void * pBfr; void * pMoreMemory; UINT32 growSize; /* Create the USB memory partition from cache safe memory */ if (ossPartInitFlag == TRUE) { /* on the first pass create 64k chunk of cache safe memory for usb */ pUsbMemSpace = (char *) cacheDmaMalloc (usbMemPartSize); if (pUsbMemSpace == NULL) { printf ("ossLib.c: unable to allocate USB memory space.\n"); return NULL; } /* make this chunk a partition */ usbMemPartId = memPartCreate (pUsbMemSpace, usbMemPartSize); if (usbMemPartId == NULL) { printf ("ossLib.c: unable to create USB memory partition.\n"); return NULL; } ossPartInitFlag = FALSE; } /* * If this call to ossMalloc is going to allocate more memory than is * available in the partition, then grow the partition by 8k, or if * numBytes is larger than 4k, then grow the partition by numBytes + 4k. */ if ((usbMemCount + numBytes) > (usbMemPartSize - 0x1000)) { growSize = 0x2000; if (numBytes > 0x1000) growSize += numBytes; pMoreMemory = cacheDmaMalloc (growSize); if (pMoreMemory == NULL) { printf ("ossLib.c: ossPartMalloc could not cacheDmaMalloc() new" \ " memory.\n"); return NULL; } if (memPartAddToPool (usbMemPartId, pMoreMemory, growSize) == ERROR) { printf ("ossLib.c: ossPartMalloc could not add new memory to" \ " pool.\n"); return NULL; } usbMemPartSize += growSize; } /* From now on, use this partition for all USB mallocs */ pBfr = memPartAlignedAlloc (usbMemPartId, ROUND_UP(numBytes, _CACHE_ALIGN_SIZE), _CACHE_ALIGN_SIZE); if (pBfr == NULL) { printf ("ossLib.c: unable to malloc USB memory.\n"); return NULL; } /* Update the amount of memory USB is currently using. */ usbMemCount += ROUND_UP(numBytes, _CACHE_ALIGN_SIZE); return pBfr; }