ossLib.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:24k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* ossLib.c - O/S-independent services for vxWorks */
  2. /* Copyright 2000-2002 Wind River Systems, Inc. */
  3. /*
  4. Modification history
  5. --------------------
  6. 01f,22jan02,wef  All USB memory now comes from a cacheDmaMalloc'd partition.  
  7.  Instrument routines to get partition information.
  8. 01e,25oct01,wef  changed ossMalloc to call memalign instead of malloc - it now
  9.  returns buffers that are a multiple of a cache line size 
  10.  aligned on a cache line boundary
  11. 01d,18sep01,wef  merge from wrs.tor2_0.usb1_1-f for veloce
  12. 01c,01aug01,rcb  fixed manpage generation in comments
  13. 01b,07mar00,rcb  Cast handles as necessary to reflect change in handle
  14.  definition from UINT32 to pVOID.
  15. 01a,07jun99,rcb  First.
  16. */
  17. /*
  18. DESCRIPTION
  19. Implements functions defined by ossLib.h.  See ossLib.h for
  20. a complete description of these functions.
  21. */
  22. /* includes */
  23. #include "vxWorks.h"
  24. #include "stdio.h"
  25. #include "stdlib.h"
  26. #include "string.h"
  27. #include "errnoLib.h" /* errno */
  28. #include "sysLib.h" /* sysClkRateGet(), etc. */
  29. #include "taskLib.h" /* taskSpawn(), etc. */
  30. #include "semLib.h" /* semCreate(), etc. */
  31. #include "objLib.h" /* S_objLib_OBJ_xxxx errors */
  32. #include "tickLib.h" /* tickGet(), etc. */
  33. #include "cacheLib.h" /* cacheDmaMalloc, Free */
  34. #include "usb/usbPlatform.h"
  35. #include "usb/ossLib.h"  /* Our API */
  36. /* Defines */
  37. /* #define  FILL_MEMORY */     /* for debugging */
  38. /* Definitions related to the creation of new threads. */
  39. #define DEFAULT_OPTIONS     0     /* task options */
  40. #define DEFAULT_STACK     0x4000  /* Default stack size */
  41. /* 
  42.  * TIMEOUT() calculates the number of ticks for a timeout from a blockFlag
  43.  * parameter to one of the ossLib functions.
  44.  */
  45. #define TIMEOUT(bf) (((bf) == OSS_BLOCK) ? 
  46.     WAIT_FOREVER : ((bf) + msecsPerTick - 1) / msecsPerTick)
  47. #define USB_MEM_PART_DEF_SIZE 0x10000 /* 0x10000  Default 
  48.  * partition size - 64k 
  49.  */
  50. /* typedefs */
  51. /* globals */
  52. /* Default to using the partition method of malloc / free. */
  53. FUNCPTR ossMallocFuncPtr;
  54. FUNCPTR ossFreeFuncPtr;
  55. /* locals */
  56. LOCAL int msecsPerTick = 55;     /* milliseconds per system tick */
  57.     /* value updated by ossInitialize() */
  58. LOCAL BOOL  ossPartInitFlag = TRUE;
  59. LOCAL char *   pUsbMemSpace; 
  60. LOCAL PART_ID  usbMemPartId;
  61. LOCAL UINT32  usbMemPartSize = USB_MEM_PART_DEF_SIZE;
  62. LOCAL UINT32  usbMemCount = 0;
  63. LOCAL UINT32  ossOldInstallFlag = FALSE;
  64. /* Functions */
  65. /***************************************************************************
  66. *
  67. * translateError - translates vxWorks error to S_ossLib_xxxx
  68. *
  69. * Translates certain vxWorks errno values to an appropriate S_ossLib_xxxx.
  70. * This function should only be invoked when the caller has already
  71. * ascertained that a vxWorks function has returned an error.
  72. *
  73. * RETURNS: S_ossLib_xxxx
  74. *
  75. * ERRNO:
  76. *  S_ossLib_BAD_HANDLE
  77. *  S_ossLib_TIMEOUT
  78. *  S_ossLib_GENERAL_FAULT
  79. */
  80. LOCAL int translateError (void)
  81.     {
  82.     switch (errno)
  83. {
  84. case S_objLib_OBJ_ID_ERROR: return S_ossLib_BAD_HANDLE;
  85. case S_objLib_OBJ_TIMEOUT: return S_ossLib_TIMEOUT;
  86. default: return S_ossLib_GENERAL_FAULT;
  87. }
  88.     }
  89. /***************************************************************************
  90. *
  91. * translatePriority - translates OSS_PRIORITY_xxx to vxWorks priority
  92. *
  93. * RETURNS: vxWorks task priority
  94. */
  95. LOCAL int translatePriority
  96.     (
  97.     UINT16 priority
  98.     )
  99.     {
  100.     int curPriority;
  101.     switch (priority)
  102. {
  103. case OSS_PRIORITY_LOW:
  104. case OSS_PRIORITY_TYPICAL:
  105. case OSS_PRIORITY_HIGH:
  106. case OSS_PRIORITY_INTERRUPT:
  107.     return priority;
  108. case OSS_PRIORITY_INHERIT:
  109. default:
  110.     taskPriorityGet (0, &curPriority);
  111.     return curPriority;
  112. }
  113.     }
  114. /***************************************************************************
  115. *
  116. * threadHead - vxWorks taskSpawn() compatible thread entry point
  117. *
  118. * ossThreadCreate() uses this intermediary function to spawn threads.  The
  119. * vxWorks taskSpawn() function passes 10 int parameters to the task entry
  120. * point.  By convention, ossThreadCreate passes the function's real entry
  121. * point in <arg1> and the thread's <param> in <arg2>..
  122. */
  123. LOCAL int threadHead
  124.     (
  125.     int arg1,     /* <func> in this parameter */
  126.     int arg2,     /* <param> in this parameter */
  127.     int arg3,
  128.     int arg4,
  129.     int arg5,
  130.     int arg6,
  131.     int arg7,
  132.     int arg8,
  133.     int arg9,
  134.     int arg10
  135.     )
  136.     
  137.     {
  138.     THREAD_PROTOTYPE func = (THREAD_PROTOTYPE) arg1;
  139.     pVOID param = (pVOID) arg2;
  140.     (*func) (param);
  141.     return 0;
  142.     }
  143. /***************************************************************************
  144. *
  145. * ossStatus - Returns OK or ERROR and sets errno based on status
  146. *
  147. * If <status> & 0xffff is not equal to zero, then sets errno to the
  148. * indicated <status> and returns ERROR.  Otherwise, does not set errno
  149. * and returns OK.
  150. *
  151. * RETURNS: OK or ERROR
  152. */
  153. STATUS ossStatus 
  154.     (
  155.     int status
  156.     )
  157.     {
  158.     if ((status & 0xffff) != 0)
  159. {
  160. errnoSet (status);
  161. return ERROR;
  162. }
  163.     
  164.     return OK;
  165.     }
  166. /***************************************************************************
  167. *
  168. * ossShutdown - Shuts down ossLib
  169. *
  170. * This function should be called once at system shutdown if an only if
  171. * the corresponding call to ossInitialize() was successful.
  172. *
  173. * RETURNS: OK or ERROR
  174. */
  175. STATUS ossShutdown (void)
  176.     {
  177.     return OK;
  178.     }
  179. /***************************************************************************
  180. *
  181. * ossThreadCreate - Spawns a new thread
  182. *
  183. * The ossThreadCreate() function creates a new thread which begins execution 
  184. * with the specified <func>.  The <param> argument will be passed to <func>.  
  185. * The ossThreadCreate() function creates the new thread with a stack of a 
  186. * default size and with no security restrictions - that is, there are no 
  187. * restrictions on the use of the returned <pThreadHandle> by other threads.  
  188. * The newly created thread will execute in the same address space as the 
  189. * calling thread.  <priority> specifies the thread's desired priority - in
  190. * systems which implement thread priorities, as OSS_PRIORITY_xxxx.  
  191. *
  192. * RETURNS: OK or ERROR
  193. *
  194. * ERRNO:
  195. *  S_ossLib_BAD_PARAMETER
  196. *  S_ossLib_GENERAL_FAULT
  197. */
  198. STATUS ossThreadCreate 
  199.     (
  200.     THREAD_PROTOTYPE func,     /* function to spawn as new thread */
  201.     pVOID param,     /* Parameter to be passed to new thread */
  202.     UINT16 priority,     /* OSS_PRIORITY_xxxx */
  203.     pCHAR name,      /* thread name or NULL */
  204.     pTHREAD_HANDLE pThreadHandle    /* Handle of newly spawned thread */
  205.     )
  206.     {
  207.     /* validate params */
  208.     if (pThreadHandle == NULL)
  209. return ossStatus (S_ossLib_BAD_PARAMETER);
  210.     /* Use vxWorks to spawn a new thread (task in vxWorks parlance). */
  211.     if ((*pThreadHandle = (THREAD_HANDLE) taskSpawn (name, 
  212. translatePriority (priority), DEFAULT_OPTIONS, DEFAULT_STACK, 
  213. threadHead, (int) func, (int) param, 0, 0, 0, 0, 0, 0, 0, 0)) == 
  214. (THREAD_HANDLE) ERROR)
  215. return ossStatus (S_ossLib_GENERAL_FAULT);
  216.     return OK;
  217.     }
  218. /***************************************************************************
  219. *
  220. * ossThreadDestroy - Attempts to destroy a thread
  221. *
  222. * This function attempts to destroy the thread specified by <threadHandle>.
  223. *
  224. * NOTE: Generally, this function should be called only after the given
  225. * thread has terminated normally.  Destroying a running thread may result
  226. * in a failure to release resources allocated by the thread.
  227. *
  228. * RETURNS: OK or ERROR
  229. *
  230. * ERRNO:
  231. *  S_ossLib_GENERAL_FAULT
  232. */
  233. STATUS ossThreadDestroy 
  234.     (
  235.     THREAD_HANDLE threadHandle     /* handle of thread to be destroyed */
  236.     )
  237.     {
  238.     if (taskDelete ((int) threadHandle) == ERROR)
  239. return ossStatus (S_ossLib_GENERAL_FAULT);
  240.     return OK;
  241.     }
  242.     
  243. /***************************************************************************
  244. *
  245. * ossThreadSleep - Voluntarily relinquishes the CPU
  246. *
  247. * Threads may call ossThreadSleeph() to voluntarily release the CPU to
  248. * another thread/process.  If the <msec> argument is 0, then the thread will
  249. * be reschuled for execution as soon as possible.  If the <msec> argument is
  250. * greater than 0, then the current thread will sleep for at least the number
  251. * of milliseconds specified.
  252. *
  253. * RETURNS: OK or ERROR
  254. */
  255. STATUS ossThreadSleep
  256.     (
  257.     UINT32 msec      /* Number of msec to sleep */
  258.     )
  259.     {
  260.     /* Delay for a number of ticks at least as long as the number of
  261.     milliseconds requested by the caller. */
  262.     taskDelay ((msec == 0) ? 0 : ((msec + msecsPerTick - 1) / msecsPerTick) + 1);
  263.     return OK;
  264.     }
  265. /***************************************************************************
  266. *
  267. * ossSemCreate - Creates a new semaphore
  268. *
  269. * This function creates a new semaphore and returns the handle of that
  270. * semaphore in <pSemHandle>.  The semaphore's initial count is set to
  271. * <curCount> and has a maximum count as specified by <maxCount>.
  272. *
  273. * RETURNS: OK or ERROR
  274. * ERRNO:
  275. *  S_ossLib_BAD_PARAMETER
  276. *  S_ossLib_GENERAL_FAULT
  277. */
  278. STATUS ossSemCreate 
  279.     (
  280.     UINT32 maxCount,     /* Max count allowed for semaphore */
  281.     UINT32 curCount,     /* initial count for semaphore */
  282.     pSEM_HANDLE pSemHandle     /* newly created semaphore handle */
  283.     )
  284.     {
  285.     /* Validate parameters */
  286.     if (pSemHandle == NULL) 
  287. return ossStatus (S_ossLib_BAD_PARAMETER);
  288.     if ((*pSemHandle = (SEM_HANDLE) semCCreate (SEM_Q_FIFO, curCount)) == NULL)
  289. return ossStatus (S_ossLib_GENERAL_FAULT);
  290.     return OK;
  291.     }
  292. /***************************************************************************
  293. *
  294. * ossSemDestroy - Destroys a semaphore
  295. *
  296. * Destroys the semaphore <semHandle> created by ossSemCreate().
  297. *
  298. * RETURNS: OK or ERROR
  299. *
  300. * ERRNO:
  301. *  S_ossLib_GENERAL_FAULT
  302. */
  303. STATUS ossSemDestroy 
  304.     (
  305.     SEM_HANDLE semHandle     /* Handle of semaphore to destroy */
  306.     )
  307.     {
  308.     if (semDelete ((SEM_ID) semHandle) != OK)
  309. return ossStatus (S_ossLib_GENERAL_FAULT);
  310.     return OK;
  311.     }
  312. /***************************************************************************
  313. *
  314. * ossSemGive - Signals a semaphore
  315. *
  316. * This function signals the sepcified semaphore.  A semaphore may have more
  317. * than one outstanding signal, as specified by the maxCount parameter when
  318. * the semaphore was created by ossSemCreate(). While the semaphore is at its
  319. * maximum count, additional calls to ossSemSignal for that semaphore have no
  320. * effect.
  321. *
  322. * RETURNS: OK or ERROR
  323. *
  324. * ERRNO:
  325. *  S_ossLib_BAD_HANDLE
  326. */
  327. STATUS ossSemGive
  328.     (
  329.     SEM_HANDLE semHandle     /* semaphore to signal */
  330.     )
  331.     {
  332.     if (semGive ((SEM_ID) semHandle) != OK)
  333. return ossStatus (S_ossLib_BAD_HANDLE);
  334.     return OK;
  335.     }
  336. /***************************************************************************
  337. *
  338. * ossSemTake - Attempts to take a semaphore
  339. *
  340. * ossSemTake() attempts to "take" the semaphore specified by <semHandle>.
  341. * <blockFlag> specifies the blocking behavior. OSS_BLOCK blocks indefinitely
  342. * waiting for the semaphore to be signalled.  OSS_DONT_BLOCK does not block
  343. * and returns an error if the semaphore is not in the signalled state. Other
  344. * values of <blockFlag> are interpreted as a count of milliseconds to wait
  345. * for the semaphore to enter the signalled state before declaring an error.
  346. *
  347. * RETURNS: OK or ERROR
  348. */
  349. STATUS ossSemTake 
  350.     (
  351.     SEM_HANDLE semHandle,     /* semaphore to take */
  352.     UINT32 blockFlag     /* specifies blocking action */
  353.     )
  354.     {
  355.     if (semTake ((SEM_ID) semHandle, TIMEOUT (blockFlag)) != OK)
  356. return ossStatus (translateError ());
  357.     return OK;
  358.     }
  359. /***************************************************************************
  360. *
  361. * ossMutexCreate - Creates a new mutex
  362. *
  363. * This function creates a new mutex and returns the handle of that
  364. * mutex in <pMutexHandle>.  The mutex is created in the "untaken" state.
  365. *
  366. * RETURNS: OK or STATUS
  367. *
  368. * ERRNO:
  369. *  S_ossLib_BAD_PARAMETER
  370. *  S_ossLib_GENERAL_FAULT
  371. */
  372. STATUS ossMutexCreate 
  373.     (
  374.     pMUTEX_HANDLE pMutexHandle     /* Handle of newly created mutex */
  375.     )
  376.     {
  377.     
  378.     /* Validate parameters */
  379.     if (pMutexHandle == NULL) 
  380. return ossStatus (S_ossLib_BAD_PARAMETER);
  381.     if ((*pMutexHandle = (MUTEX_HANDLE) semMCreate (SEM_Q_FIFO)) == NULL)
  382. return ossStatus (S_ossLib_GENERAL_FAULT);
  383.     return OK;
  384.     }
  385. /***************************************************************************
  386. *
  387. * ossMutexDestroy - Destroys a mutex
  388. *
  389. * Destroys the mutex <mutexHandle> created by ossMutexCreate().
  390. *
  391. * RETURNS: OK or ERROR
  392. *
  393. * ERRNO:
  394. *  S_ossLib_GENERAL_FAULT
  395. */
  396. STATUS ossMutexDestroy 
  397.     (
  398.     MUTEX_HANDLE mutexHandle     /* Handle of mutex to destroy */
  399.     )
  400.     {
  401.     if (semDelete ((SEM_ID) mutexHandle) != OK)
  402. return ossStatus (S_ossLib_GENERAL_FAULT);
  403.     return OK;
  404.     }
  405. /***************************************************************************
  406. *
  407. * ossMutexTake - Attempts to take a mutex
  408. *
  409. * ossMutexTake() attempts to "take" the specified mutex.  The attempt will
  410. * succeed if the mutex is not owned by any other threads.  If a thread
  411. * attempts to take a mutex which it already owns, the attempt will succeed.
  412. * <blockFlag> specifies the blocking behavior. OSS_BLOCK blocks indefinitely
  413. * waiting for the mutex to be released.  OSS_DONT_BLOCK does not block and
  414. * returns an error if the mutex is not in the released state.  Other values
  415. * of <blockFlag> are interpreted as a count of milliseconds to wait for the
  416. * mutex to be released before declaring an error.
  417. *
  418. * RETURNS: OK or ERROR
  419. */
  420. STATUS ossMutexTake 
  421.     (
  422.     MUTEX_HANDLE mutexHandle,     /* Mutex to take */
  423.     UINT32 blockFlag     /* specifies blocking action */
  424.     )
  425.     {
  426.     if (semTake ((SEM_ID) mutexHandle, TIMEOUT (blockFlag)) != OK)
  427. return ossStatus (translateError ());
  428.     return OK;
  429.     }
  430. /***************************************************************************
  431. *
  432. * ossMutexRelease - Releases (gives) a mutex
  433. *
  434. * Release the mutex specified by <mutexHandle>.  This function will fail
  435. * if the calling thread is not the owner of the mutex.
  436. *
  437. * RETURNS: OK or ERROR
  438. *
  439. * ERRNO:
  440. *  S_ossLib_BAD_HANDLE
  441. */
  442. STATUS ossMutexRelease 
  443.     (
  444.     MUTEX_HANDLE mutexHandle     /* Mutex to be released */
  445.     )
  446.     {
  447.     if (semGive ((SEM_ID) mutexHandle) != OK)
  448. return ossStatus (S_ossLib_BAD_HANDLE);
  449.     return OK;
  450.     }
  451. /***************************************************************************
  452. *
  453. * ossPartSizeGet - Retrieves the size of the USB memory partition.
  454. *
  455. * Returns the size of the USB memory partition.  
  456. *
  457. * RETURNS: Size of partition.
  458. */
  459. UINT32 ossPartSizeGet (void)
  460.     {
  461.     return usbMemPartSize;
  462.     }
  463. /***************************************************************************
  464. *
  465. * ossPartSizeSet - Sets the the initial size of the USB memory partition.
  466. *
  467. * Sets the size of the USB memory partition.  This must be called prior to 
  468. * the first call to ossMalloc.  This will set the size that ossMalloc will
  469. * use to do its allocation. Once ossMalloc has been called, the partition 
  470. * size has been already allocated.  To add more memory to the USB partition, 
  471. * you must retrieve the USB partition ID and add more memory via the 
  472. * memPartLib routines.
  473. *
  474. * RETURNS: OK or ERROR
  475. *
  476. * SEE ALSO: memPartLib
  477. *
  478. */
  479. STATUS ossPartSizeSet 
  480.     (
  481.     UINT32 numBytes
  482.     )
  483.     {
  484.     /* ossMalloc has already initialized the USB memory partition */
  485.     if (ossPartInitFlag == FALSE)
  486. return ERROR;
  487.     /* ossMalloc has not been called yet, so set the partition size.  */
  488.     else
  489. usbMemPartSize = numBytes;
  490.     return usbMemPartSize;
  491.     }
  492. /***************************************************************************
  493. *
  494. * ossPartIdGet - Retrieves the partition ID of USB memory partition.
  495. *
  496. * Returns the partition ID of the USB memory partition.  
  497. *
  498. * RETURNS: The partition ID.
  499. */
  500. PART_ID ossPartIdGet (void)
  501.     {
  502.     return usbMemPartId;
  503.     }
  504. /***************************************************************************
  505. *
  506. * ossMemUsedGet - Retrieves amount of memory currently in use by USB.
  507. *
  508. * Returns the amount, in bytes, currently being used by USB.
  509. *
  510. * RETURNS: Number of bytes of memory in use.
  511. */
  512. UINT32 ossMemUsedGet (void)
  513.     {
  514.     return usbMemCount;
  515.     }
  516. /***************************************************************************
  517. *
  518. * ossMalloc - Master USB memory allocation routine.
  519. *
  520. * ossMalloc() calls the malloc routine installed in the global variable
  521. * <ossMallocFuncPtr>.  These default to ossPartMalloc(), but can be changed
  522. * by the user to their own defined malloc routine or to a non-partition
  523. * method of malloc / free by calling ossOldInstall().
  524. *
  525. * RETURNS: Pointer to allocated buffer, or NULL 
  526. *
  527. */
  528. void * ossMalloc 
  529.     (
  530.     UINT32 numBytes
  531.     )
  532.     {
  533.     return (void *) ((*ossMallocFuncPtr) (numBytes));
  534.     }
  535. /***************************************************************************
  536. *
  537. * ossPartMalloc - USB memory allocation.
  538. *
  539. * ossPartMalloc() allocates cache-safe buffer of size <numBytes> out of the USB 
  540. * partition and returns a pointer to this buffer.  The buffer is allocated 
  541. * from a local USB partition.  The size of this partition is defaulted to 
  542. * 64k but can be modified to suit the users needs.  This partition will 
  543. * dynamically grow based on additional need.  Memory allocated by this 
  544. * function must be freed by calling ossFree().
  545. *
  546. * RETURNS: Pointer to allocated buffer, or NULL 
  547. *
  548. * INTERNAL:
  549. *
  550. * ossPartMalloc() is responsible for creating the USB partition.  This is 
  551. * to allow full user control over the USB memory system.  The decision to
  552. * put the partition creation here as opposed to ossInitialize() was because
  553. * ossInitialize() is called from usbdInitialize().  Implementations of both
  554. * these routines are not shipped in source.  Therefore the order in which
  555. * ossInitialize() is called cannot be modified by users who do not have 
  556. * source code.   Putting the partition creation in ossPartMalloc() allows
  557. * the user to set the malloc / free pointers with a call to ossOldInstall()
  558. * such that the partition is not created if need be.
  559. *
  560. */
  561. void * ossPartMalloc 
  562.     (
  563.     UINT32 numBytes     /* Size of buffer to allocate */
  564.     )
  565.     {
  566.     void * pBfr;
  567.     void * pMoreMemory;
  568.     UINT32 growSize;
  569.     /*  Create the USB memory partition from cache safe memory */ 
  570.     if (ossPartInitFlag == TRUE)
  571. {
  572. /* on the first pass create 64k chunk of cache safe memory for usb */
  573. pUsbMemSpace = (char *) cacheDmaMalloc (usbMemPartSize);
  574. if (pUsbMemSpace == NULL)
  575.     {
  576.     printf ("ossLib.c: unable to allocate USB memory space.n");
  577.     return NULL;
  578.     } 
  579. /* make this chunk a partition */
  580. usbMemPartId = memPartCreate (pUsbMemSpace, usbMemPartSize);
  581. if (usbMemPartId == NULL)
  582.     {
  583.     printf ("ossLib.c: unable to create USB memory partition.n");
  584.     return NULL;
  585.     } 
  586. ossPartInitFlag = FALSE;
  587.      }
  588.     /* 
  589.      * If this call to ossMalloc is going to allocate more memory than is 
  590.      * available in the partition, then grow the partition by 8k, or if
  591.      * numBytes is larger than 4k, then grow the partition by numBytes + 4k.
  592.      */
  593.     if ((usbMemCount + numBytes) > (usbMemPartSize - 0x1000))
  594. {
  595. growSize = 0x2000;
  596. if (numBytes > 0x1000)
  597.     growSize += numBytes;
  598. pMoreMemory = cacheDmaMalloc (growSize);
  599. if (pMoreMemory == NULL)
  600.    {
  601.    printf ("ossLib.c: ossPartMalloc could not cacheDmaMalloc() new" 
  602.    " memory.n");
  603.    return NULL;
  604.    }
  605. if (memPartAddToPool (usbMemPartId, pMoreMemory, growSize) == ERROR)
  606.    {
  607.    printf ("ossLib.c: ossPartMalloc could not add new memory to" 
  608.    " pool.n");
  609.    return NULL;
  610.    }
  611. usbMemPartSize += growSize;
  612. }
  613.     /* From now on, use this partition for all USB mallocs */
  614.     pBfr = memPartAlignedAlloc (usbMemPartId, 
  615. ROUND_UP(numBytes, _CACHE_ALIGN_SIZE), 
  616. _CACHE_ALIGN_SIZE);
  617.     if (pBfr == NULL)
  618. {
  619. printf ("ossLib.c: unable to malloc USB memory.n");
  620. return NULL;
  621.     /* Update the amount of memory USB is currently using. */
  622.     usbMemCount += ROUND_UP(numBytes, _CACHE_ALIGN_SIZE);
  623.     return pBfr;
  624.     }
  625. /***************************************************************************
  626. *
  627. * ossOldMalloc - Global memory allocation
  628. *
  629. * ossOldMalloc() allocates a buffer of <numBytes> in length and returns a
  630. * pointer to the allocated buffer.  The buffer i allocated from a global
  631. * pool which can be made visible to all processes or drivers in the
  632. * system.  Memory allocated by this function must be freed by calling
  633. * ossFree().
  634. *
  635. * RETURNS: Pointer to allocated buffer, or NULL
  636. *
  637. */
  638.  
  639. void * ossOldMalloc
  640.     (
  641.     UINT32 numBytes                 /* Size of buffer to allocate */
  642.     )
  643.  
  644.     {
  645. #ifdef  FILL_MEMORY
  646.     pVOID pBfr = memalign (_CACHE_ALIGN_SIZE,
  647.                            ROUND_UP(numBytes, _CACHE_ALIGN_SIZE));
  648.     memset (pBfr, 0xdd, numBytes);
  649.     return pBfr;
  650. #else
  651.     return memalign (_CACHE_ALIGN_SIZE,
  652.                      ROUND_UP(numBytes, _CACHE_ALIGN_SIZE));
  653. #endif
  654.     }
  655. /***************************************************************************
  656. *
  657. * ossCalloc - Allocates memory initialized to zeros
  658. *
  659. * ossCalloc() uses ossMalloc() to allocate a block of memory and then
  660. * initializes it to zeros.  Memory allocated using this function should
  661. * be freed using ossFree().
  662. *
  663. * RETURNS: Pointer to allocated buffer, or NULL. 
  664. *
  665. */
  666. pVOID ossCalloc
  667.     (
  668.     UINT32 numBytes     /* size of buffer to allocate */
  669.     )
  670.     {
  671.     pVOID mem;
  672.     if ((mem = ossMalloc (numBytes)) == NULL)
  673. return NULL;
  674.     memset (mem, 0, numBytes);
  675.     return mem;
  676.     }
  677. /***************************************************************************
  678. *
  679. * ossFree - Master USB memory free routine.
  680. *
  681. * ossFree() calls the free routine installed in the global variable
  682. * <ossFreeFuncPtr>.  This defaults to ossPartFree(), but can be changed
  683. * by the user to their own defined free routine or to a non-partition
  684. * method of malloc / free by calling ossOldInstall().
  685. *
  686. * RETURNS: N/A
  687. */
  688. void ossFree 
  689.     (
  690.     pVOID bfr
  691.     )
  692.     {
  693.     (*ossFreeFuncPtr) (bfr);
  694.     }
  695. /***************************************************************************
  696. *
  697. * ossPartFree - Frees globally allocated memory
  698. *
  699. * ossPartFree() frees memory allocated by ossMalloc().
  700. *
  701. * RETURNS: N/A
  702. */
  703. void ossPartFree 
  704.     (
  705.     pVOID bfr
  706.     )
  707.     {
  708.     if (bfr != NULL)
  709. {
  710. if (usbMemPartId == NULL)
  711.     {
  712.     printf ("ossFree: usbMemPartId is NULL.n");
  713.     return;
  714.     }
  715. memPartFree (usbMemPartId, bfr);
  716. /* Update the amount of USB memory in use. */
  717. usbMemCount -= sizeof (bfr);
  718. }
  719.     }
  720. /***************************************************************************
  721. *
  722. * ossOldFree - Frees globally allocated memory
  723. *
  724. * ossOldFree() frees memory allocated by ossMalloc().
  725. *
  726. * RETURNS: N/A
  727. */
  728.  
  729. void ossOldFree
  730.     (
  731.     void * bfr
  732.     )
  733.  
  734.     {
  735.     if (bfr != NULL)
  736.         free (bfr);
  737.     }
  738. /***************************************************************************
  739. *
  740. * ossOldInstall - Installs old method of USB malloc and free. 
  741. *
  742. * Installs old method of USB malloc and free.  This must be called before
  743. * the call to usbdInitialize().
  744. *
  745. * RETURNS: N/A
  746. */
  747.  
  748. void ossOldInstall (void)
  749.     {
  750.     ossMallocFuncPtr = (FUNCPTR) &ossOldMalloc;
  751.     ossFreeFuncPtr = (FUNCPTR) &ossOldFree;
  752.     ossOldInstallFlag = TRUE;
  753.     }
  754. /***************************************************************************
  755. *
  756. * ossTime - Returns relative system time in msec
  757. *
  758. * Returns a count of milliseconds relative to the time the system was
  759. * started.
  760. *
  761. * NOTE: The time will wrap approximately every 49 days, so time calucations
  762. * should always be based on the difference between two time values.
  763. */
  764. UINT32 ossTime (void)
  765.     {
  766.     return tickGet () * msecsPerTick;
  767.     }
  768. /***************************************************************************
  769. *
  770. * ossInitialize - Initializes ossLib
  771. *
  772. * This function should be called once at initialization in order
  773. * to initialize the ossLib.  Calls to this function may be
  774. * nested.  This permits multiple, indpendent libraries to use this library
  775. * without need to coordinate the use of ossInitialize() and ossShutdown()
  776. * across the libraries.
  777. *
  778. * RETURNS: OK or ERROR
  779. */
  780. STATUS ossInitialize (void)
  781.     {
  782.     /* Get the system clock rate...used by ossThreadSleep */
  783.     msecsPerTick = 1000 / sysClkRateGet();
  784.    
  785.     /*  Default to using the partition method of malloc / free. */
  786.     if (!ossOldInstallFlag)
  787.     {
  788. ossMallocFuncPtr = (FUNCPTR) &ossPartMalloc;
  789.         ossFreeFuncPtr = (FUNCPTR) &ossPartFree;
  790. }
  791.     return OK;
  792.     }
  793. /* End of file. */