📄 osslib.c
字号:
* S_ossLib_GENERAL_FAULT*/STATUS ossMutexDestroy ( MUTEX_HANDLE mutexHandle /* Handle of mutex to destroy */ ) { if (semDelete ((SEM_ID) mutexHandle) != OK) return ossStatus (S_ossLib_GENERAL_FAULT); return OK; }/***************************************************************************** ossMutexTake - Attempts to take a mutex** ossMutexTake() attempts to "take" the specified mutex. The attempt will* succeed if the mutex is not owned by any other threads. If a thread* attempts to take a mutex which it already owns, the attempt will succeed.* <blockFlag> specifies the blocking behavior. OSS_BLOCK blocks indefinitely* waiting for the mutex to be released. OSS_DONT_BLOCK does not block and* returns an error if the mutex is not in the released state. Other values* of <blockFlag> are interpreted as a count of milliseconds to wait for the* mutex to be released before declaring an error.** RETURNS: OK or ERROR*/STATUS ossMutexTake ( MUTEX_HANDLE mutexHandle, /* Mutex to take */ UINT32 blockFlag /* specifies blocking action */ ) { if (semTake ((SEM_ID) mutexHandle, TIMEOUT (blockFlag)) != OK) return ossStatus (translateError ()); return OK; }/***************************************************************************** ossMutexRelease - Releases (gives) a mutex** Release the mutex specified by <mutexHandle>. This function will fail* if the calling thread is not the owner of the mutex.** RETURNS: OK or ERROR** ERRNO:* S_ossLib_BAD_HANDLE*/STATUS ossMutexRelease ( MUTEX_HANDLE mutexHandle /* Mutex to be released */ ) { if (semGive ((SEM_ID) mutexHandle) != OK) return ossStatus (S_ossLib_BAD_HANDLE); return OK; }/***************************************************************************** ossPartSizeGet - Retrieves the size of the USB memory partition.** Returns the size of the USB memory partition. ** RETURNS: Size of partition.*/UINT32 ossPartSizeGet (void) { return usbMemPartSize; }/***************************************************************************** ossPartSizeSet - Sets the the initial size of the USB memory partition.** Sets the size of the USB memory partition. This must be called prior to * the first call to ossMalloc. This will set the size that ossMalloc will* use to do its allocation. Once ossMalloc has been called, the partition * size has been already allocated. To add more memory to the USB partition, * you must retrieve the USB partition ID and add more memory via the * memPartLib routines.** RETURNS: OK or ERROR** SEE ALSO: memPartLib**/STATUS ossPartSizeSet ( UINT32 numBytes ) { /* ossMalloc has already initialized the USB memory partition */ if (ossPartInitFlag == FALSE) return ERROR; /* ossMalloc has not been called yet, so set the partition size. */ else usbMemPartSize = numBytes; return usbMemPartSize; }/***************************************************************************** ossPartIdGet - Retrieves the partition ID of USB memory partition.** Returns the partition ID of the USB memory partition. ** RETURNS: The partition ID.*/PART_ID ossPartIdGet (void) { return usbMemPartId; }/***************************************************************************** ossMemUsedGet - Retrieves amount of memory currently in use by USB.** Returns the amount, in bytes, currently being used by USB.** RETURNS: Number of bytes of memory in use.*/UINT32 ossMemUsedGet (void) { return usbMemCount; }/***************************************************************************** ossMalloc - Master USB memory allocation routine.** ossMalloc() calls the malloc routine installed in the global variable* <ossMallocFuncPtr>. These default to ossPartMalloc(), but can be changed* by the user to their own defined malloc routine or to a non-partition* method of malloc / free by calling ossOldInstall().** RETURNS: Pointer to allocated buffer, or NULL **/void * ossMalloc ( UINT32 numBytes ) { return (void *) ((*ossMallocFuncPtr) (numBytes)); }/***************************************************************************** ossPartMalloc - USB memory allocation.** ossPartMalloc() allocates cache-safe buffer of size <numBytes> out of the USB * partition and returns a pointer to this buffer. The buffer is allocated * from a local USB partition. The size of this partition is defaulted to * 64k but can be modified to suit the users needs. This partition will * dynamically grow based on additional need. Memory allocated by this * function must be freed by calling ossFree().** RETURNS: Pointer to allocated buffer, or NULL ** INTERNAL:** ossPartMalloc() is responsible for creating the USB partition. This is * to allow full user control over the USB memory system. The decision to* put the partition creation here as opposed to ossInitialize() was because* ossInitialize() is called from usbdInitialize(). Implementations of both* these routines are not shipped in source. Therefore the order in which* ossInitialize() is called cannot be modified by users who do not have * source code. Putting the partition creation in ossPartMalloc() allows* the user to set the malloc / free pointers with a call to ossOldInstall()* such that the partition is not created if need be.**/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; }/***************************************************************************** ossOldMalloc - Global memory allocation** ossOldMalloc() allocates a buffer of <numBytes> in length and returns a* pointer to the allocated buffer. The buffer i allocated from a global* pool which can be made visible to all processes or drivers in the* system. Memory allocated by this function must be freed by calling* ossFree().** RETURNS: Pointer to allocated buffer, or NULL**/ void * ossOldMalloc ( UINT32 numBytes /* Size of buffer to allocate */ ) {#ifdef FILL_MEMORY pVOID pBfr = memalign (_CACHE_ALIGN_SIZE, ROUND_UP(numBytes, _CACHE_ALIGN_SIZE)); memset (pBfr, 0xdd, numBytes); return pBfr;#else return memalign (_CACHE_ALIGN_SIZE, ROUND_UP(numBytes, _CACHE_ALIGN_SIZE));#endif }/***************************************************************************** ossCalloc - Allocates memory initialized to zeros** ossCalloc() uses ossMalloc() to allocate a block of memory and then* initializes it to zeros. Memory allocated using this function should* be freed using ossFree().** RETURNS: Pointer to allocated buffer, or NULL. **/pVOID ossCalloc ( UINT32 numBytes /* size of buffer to allocate */ ) { pVOID mem; if ((mem = ossMalloc (numBytes)) == NULL) return NULL; memset (mem, 0, numBytes); return mem; }/***************************************************************************** ossFree - Master USB memory free routine.** ossFree() calls the free routine installed in the global variable* <ossFreeFuncPtr>. This defaults to ossPartFree(), but can be changed* by the user to their own defined free routine or to a non-partition* method of malloc / free by calling ossOldInstall().** RETURNS: N/A*/void ossFree ( pVOID bfr ) { (*ossFreeFuncPtr) (bfr); }/***************************************************************************** ossPartFree - Frees globally allocated memory** ossPartFree() frees memory allocated by ossMalloc().** RETURNS: N/A*/void ossPartFree ( pVOID bfr ) { if (bfr != NULL) { if (usbMemPartId == NULL) { printf ("ossFree: usbMemPartId is NULL.\n"); return; } memPartFree (usbMemPartId, bfr); /* Update the amount of USB memory in use. */ usbMemCount -= sizeof (bfr); } }/***************************************************************************** ossOldFree - Frees globally allocated memory** ossOldFree() frees memory allocated by ossMalloc().** RETURNS: N/A*/ void ossOldFree ( void * bfr ) { if (bfr != NULL) free (bfr); }/***************************************************************************** ossOldInstall - Installs old method of USB malloc and free. ** Installs old method of USB malloc and free. This must be called before* the call to usbdInitialize().** RETURNS: N/A*/ void ossOldInstall (void) { ossMallocFuncPtr = (FUNCPTR) &ossOldMalloc; ossFreeFuncPtr = (FUNCPTR) &ossOldFree; ossOldInstallFlag = TRUE; }/***************************************************************************** ossTime - Returns relative system time in msec** Returns a count of milliseconds relative to the time the system was* started.** NOTE: The time will wrap approximately every 49 days, so time calucations* should always be based on the difference between two time values.*/UINT32 ossTime (void) { return tickGet () * msecsPerTick; }/***************************************************************************** ossInitialize - Initializes ossLib** This function should be called once at initialization in order* to initialize the ossLib. Calls to this function may be* nested. This permits multiple, indpendent libraries to use this library* without need to coordinate the use of ossInitialize() and ossShutdown()* across the libraries.** RETURNS: OK or ERROR*/STATUS ossInitialize (void) { /* Get the system clock rate...used by ossThreadSleep */ msecsPerTick = 1000 / sysClkRateGet(); /* Default to using the partition method of malloc / free. */ if (!ossOldInstallFlag) { ossMallocFuncPtr = (FUNCPTR) &ossPartMalloc; ossFreeFuncPtr = (FUNCPTR) &ossPartFree; } return OK; }/* End of file. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -