예제 #1
0
/* Function     : CyU3PMemAlloc
 * Description  : This function allocates memory required for various OS objects in the
 *                firmware application. This function is used by the SDK internal drivers
 *                in addition to the application code itself.
 *                The default implementation makes use of the ThreadX byte pool services.
 *                If memory leak and corruption checking is enabled, the implementation
 *                adds a 20 byte header and a 4 byte footer around the memory block.
 * Parameters   :
 *                size : Size of memory required in bytes.
 * Return Value : Pointer to the allocated memory block.
 */
void *
CyU3PMemAlloc (
        uint32_t size)
{
    void         *ret_p;
    uint32_t      status;

#ifdef CYFXTX_ERRORDETECTION
    MemBlockInfo *block_p;
#endif

    /* Round size up to a multiple of 4 bytes. */
    size = ROUND_UP (size, 4);

#ifdef CYFXTX_ERRORDETECTION
    /* If memory checks are enabled, add memory for the header and footer. */
    if (glMemEnableChecks)
        size += sizeof (MemBlockInfo) + sizeof (uint32_t);
#endif

    /* Cannot wait in interrupt context */
    if (CyU3PThreadIdentify ())
    {
        status = CyU3PByteAlloc (&glMemBytePool, (void **)&ret_p, size, CY_U3P_MEM_ALLOC_TIMEOUT);
    }
    else
    {
        status = CyU3PByteAlloc (&glMemBytePool, (void **)&ret_p, size, CYU3P_NO_WAIT);
    }

    if (status == CY_U3P_SUCCESS)
    {
#ifdef CYFXTX_ERRORDETECTION
        if (glMemEnableChecks)
        {
            /* Store the header information used for leak and corruption checks. */
            block_p = (MemBlockInfo *)ret_p;
            block_p->alloc_id        = glMemAllocCnt++;
            block_p->alloc_size      = size;
            block_p->prev_blk        = glMemInUseList;
            block_p->next_blk        = 0;
            block_p->start_sig       = CY_U3P_MEM_START_SIG;
            if (glMemInUseList != 0)
                glMemInUseList->next_blk = block_p;
            glMemInUseList           = block_p;

            /* Add the end block signature as a footer. */
            ((uint32_t *)block_p)[BYTE_TO_DWORD (size) - 1] = CY_U3P_MEM_END_SIG;

            /* Update the return pointer to skip the header created. */
            ret_p = (void *)((uint8_t *)block_p + sizeof (MemBlockInfo));
        }
#endif

        return ret_p;
    }

    return (NULL);
}
예제 #2
0
void *CyU3PMemAlloc(uint32_t size) {
	void *ret_p;
	uint32_t status;

	// Cannot wait in interrupt context
	if (CyU3PThreadIdentify()) {
		status = CyU3PByteAlloc(&glMemBytePool, (void **)&ret_p, size, CY_U3P_MEM_ALLOC_TIMEOUT);
	} else {
		status = CyU3PByteAlloc(&glMemBytePool, (void **)&ret_p, size, CYU3P_NO_WAIT);
	}

	if (status == CY_U3P_SUCCESS) {
		return ret_p;
	}

	return (NULL);
}