Esempio n. 1
0
void *malloc 
    (
    size_t nBytes             /* number of bytes to allocate */
    )
    {
    return (memPartAlloc (&memSysPartition, (unsigned) nBytes));
    }
Esempio n. 2
0
int _tmain(int argc, _TCHAR* argv[])
{
	classLibInit();
	memPartLibInit(memBuf, MEM_LEN);

	char* a1 = (char*)memPartAlloc(memSysPartId, 10);
	char* a2 = (char*)memPartAlloc(memSysPartId, 45);
	memPartFree(memSysPartId, a1);
	memPartFree(memSysPartId, a2);

	a1 = (char*)memPartAlloc(memSysPartId, 10);
	a2 = (char*)memPartAlloc(memSysPartId, 45);

	memPartFree(memSysPartId, a2);
	memPartFree(memSysPartId, a1);

	a1 = (char*)memPartAlloc(memSysPartId, 10);
	a2 = (char*)memPartAlloc(memSysPartId, 12);
	char* a3 = (char*)memPartAlloc(memSysPartId, 45);

	memPartFree(memSysPartId, a2);

	char* a4 = (char*)memPartAlloc(memSysPartId, 12);

	testQueue();

	SEM_ID semId = semMCreate(0);

	int c = 0;
	semTake(semId, WAIT_FOREVER);
	c++;
	semGive(semId);

	semDelete(semId);

	gets(a1);
	return 0;
}
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;
}