⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wsm.c

📁 SyncML ToolKits,学习syncml的参考工具包.非常好用.
💻 C
📖 第 1 页 / 共 3 页
字号:
 * * RETURN:  SML_ERR_OK, if O.K. *          SML_ERR_WRONG_USAGE, if a handle was still locked * */Ret_t wsmTerminate (void) {  int i;  // free all WSM resources  for (i=0; i < MAX_WSM_BUFFERS; ++i) {    if ( wsmBuf[i].memH != WSM_MEMH_UNUSED )      if ( wsmDestroy(wsmBuf[i].bufName) == SML_ERR_WRONG_USAGE ) {	return SML_ERR_WRONG_USAGE;      }  }  // free global DataStructs  freeDataStructs();  return SML_ERR_OK;}/** * FUNCTION: wsmProcessedBytes * * Tell Workspace Manager the number of bytes already processed. * * PRE-Condition:   handle is locked; handle is valid; *                  noBytes <= wsmGetUsedSize * * POST-Condition:  noBytes starting at wsmGetPtr() position are deleted;  *                  remaining bytes are copied to  *                  wsmGetPtr(SML_FIRST_FREE_ITEM) position; *                  wsmGetUsedSize -= noBytes; wsmGetFreeSize += noBytes * * IN:      wsmH *          Handle to the open buffer * IN:      noBytes *          Number of bytes already processed from buffer. * * RETURN:  SML_ERR_OK, if O.K. *          SML_ERR_INVALID_HANDLE, if handle was invalid *          SML_ERR_WRONG_USAGE, if handle was not locked *          SML_ERR_INVALID_SIZE, if noBytes > wsmGetUsedSize * * @see  wsmGetFreeSize */Ret_t wsmProcessedBytes (MemHandle_t wsmH, MemSize_t noBytes) {  // check if handle is invalid  if ( ! isValidMemH(wsmH) ) {    return wsmRet=SML_ERR_INVALID_HANDLE;  }  // check if handle is unlocked  if ( ! isLockedMemH(wsmH) ) {    return wsmRet=SML_ERR_WRONG_USAGE;  }  wsmIndex = lookup(wsmH);  if ( noBytes > wsmBuf[wsmIndex].usedBytes ) {    return wsmRet=SML_ERR_INVALID_SIZE;  }  // adapt usedSize  wsmBuf[wsmIndex].usedBytes -= noBytes;  // move memory  // check return ?????  smlLibMemmove(wsmBuf[wsmIndex].pFirstData,	  (wsmBuf[wsmIndex].pFirstData + noBytes),	  wsmBuf[wsmIndex].usedBytes);  // move pFirstFree  wsmBuf[wsmIndex].pFirstFree -= noBytes;  return wsmRet=SML_ERR_OK;}/** * FUNCTION: wsmLockH * * Locks handle wsmH and get a pointer to the contents of wsmH. <BR> * RequestedPos describes the position in the buffer to which the returned  * pointer should point. Valid values are:  * <UL> *   <LI> SML_FIRST_DATA_ITEM *   <LI> SML_FIRST_FREE_ITEM * </UL> * * PRE-Condition:   handle is unlocked; handle is valid * * POST-Condition:  handle is locked; points to first data item,  *                  or first free item. * * IN:      wsmH *          Handle to the open buffer * IN:      requestedPos *          Requested position of the returned pointer *          <UL> *            <LI> SML_FIRST_DATA_ITEM : points to first data entry *            <LI> SML_FIRST_FREE_ITEM : points to first free entry *          </UL> *  * OUT:     pMem *          Pointer to requested memory           *           * RETURN:  SML_ERR_OK, if O.K. *          SML_ERR_INVALID_HANDLE, if handle was invalid *          SML_ERR_WRONG_USAGE, if handle was still locked *          SML_ERR_UNSPECIFIC, if requested position is unknown, or lock failed * * @see  wsmUnlockH */Ret_t wsmLockH (MemHandle_t wsmH, SmlBufPtrPos_t requestedPos,		MemPtr_t *pMem) {  // check if handle is invalid  if ( ! isValidMemH(wsmH) ) {    return wsmRet=SML_ERR_INVALID_HANDLE;  }  // check if handle is locked  if ( isLockedMemH(wsmH) ) {    return wsmRet=SML_ERR_WRONG_USAGE;  }  // lock  if ( (wsmRet = smLock(wsmH, pMem)) != SML_ERR_OK ) {    return wsmRet=SML_ERR_UNSPECIFIC;  }  // set local pointers    wsmIndex = lookup(wsmH);  wsmBuf[wsmIndex].pFirstData = *pMem;  wsmBuf[wsmIndex].pFirstFree = *pMem + wsmBuf[wsmIndex].usedBytes;  wsmBuf[wsmIndex].flags |= WSM_LOCKED_F;  switch (requestedPos) {  case SML_FIRST_DATA_ITEM:    *pMem = wsmBuf[wsmIndex].pFirstData;    break;  case SML_FIRST_FREE_ITEM:    *pMem = wsmBuf[wsmIndex].pFirstFree;    break;  default:    return wsmRet=SML_ERR_UNSPECIFIC;  }  return wsmRet=SML_ERR_OK;}/** * FUNCTION: wsmGetFreeSize * * Returns the remaining unused bytes in the buffer. * * PRE-Condition:   handle is valid * * POST-Condition:  wsmGetFreeSize = BufferSize - wsmGetUsedSize * * IN:      wsmH *          Handle to the open buffer * * OUT:     freeSize *          Number of bytes which are unused in this buffer * * RETURN:  SML_ERR_OK, if O.K. *          SML_ERR_INVALID_HANDLE, if handle was invalid * * @see  wsmGetUsedSize * @see  wsmProcessedBytes */Ret_t wsmGetFreeSize(MemHandle_t wsmH, MemSize_t *freeSize) {  // check if handle is invalid  if ( ! isValidMemH(wsmH) ) {    return wsmRet=SML_ERR_INVALID_HANDLE;  }    wsmIndex = lookup(wsmH);  *freeSize = wsmBuf[wsmIndex].size - wsmBuf[wsmIndex].usedBytes;  return wsmRet=SML_ERR_OK;}/** * FUNCTION: wsmGetUsedSize * * Returns the number of bytes used in the buffer. * * PRE-Condition:   handle is valid * * POST-Condition:  usedSize = BufferSize - wsmGetFreeSize * * IN:      wsmH *          Handle to the open buffer * * OUT:     usedSize *          Number of bytes which are already used in this buffer * * RETURN:  SML_ERR_OK, if O.K. *          SML_ERR_INVALID_HANDLE, if handle was invalid * * @see  wsmGetFreeSize * @see  wsmSetUsedSize */Ret_t wsmGetUsedSize(MemHandle_t wsmH, MemSize_t *usedSize) {  // check if handle is invalid  if ( ! isValidMemH(wsmH) ) {    return wsmRet=SML_ERR_INVALID_HANDLE;  }    wsmIndex = lookup(wsmH);  *usedSize = wsmBuf[wsmIndex].usedBytes;  return wsmRet=SML_ERR_OK;}/** * FUNCTION: wsmUnlockH * * Unlock handle wsmH. <BR> * After this call all pointers to this memory handle are invalid  * and should no longer be used. * * PRE-Condition:   handle is locked; handle is valid * * POST-Condition:  handle is unlocked * * OUT:     wsmH *          Handle to unlock * * RETURN:  SML_ERR_OK, if O.K. *          SML_ERR_INVALID_HANDLE, if handle was invalid *          SML_ERR_WRONG_USAGE, if handle was not locked *          SML_ERR_UNSPECIFIC, unlock failed * * @see  wsmLockH */Ret_t wsmUnlockH (MemHandle_t wsmH) {  // check if handle is invalid  if ( ! isValidMemH(wsmH) ) {    return wsmRet=SML_ERR_INVALID_HANDLE;  }  // check if handle is already unlocked  if ( ! isLockedMemH(wsmH) ) {    return wsmRet=SML_ERR_WRONG_USAGE;  }  // unlock  if ( (wsmRet = smUnlock(wsmH)) != SML_ERR_OK ) {    return wsmRet=SML_ERR_UNSPECIFIC;  }  // set local pointers    wsmIndex = lookup(wsmH);  wsmBuf[wsmIndex].pFirstData = NULL;  wsmBuf[wsmIndex].pFirstFree = NULL;  wsmBuf[wsmIndex].flags &= ~WSM_LOCKED_F;  return wsmRet=SML_ERR_OK;}/** * FUNCTION: wsmSetUsedSize * * Tell Workspace how many data were written into buffer. * * PRE-Condition:   handle is valid; usedSize <= wsmGetFreeSize; handle is  *                  locked * * POST-Condition:  wsmGetUsedSize += usedSize; wsmGetFreeSize -= usedSize; *                  instancePtr += usedSize; * * IN:      wsmH *          Handle to the open buffer * IN:      usedSize *          Number of bytes which were written into buffer * * RETURN:  SML_ERR_OK, if O.K. *          SML_ERR_INVALID_HANDLE, if handle was invalid *          SML_ERR_INVALID_SIZE, if usedSize <= wsmGetFreeSize * * @see  wsmGetUsedSize */Ret_t wsmSetUsedSize (MemHandle_t wsmH, MemSize_t usedSize) {  // check if handle is invalid  if ( ! isValidMemH(wsmH) ) {    return wsmRet=SML_ERR_INVALID_HANDLE;  }  // check if handle is unlocked  if ( ! isLockedMemH(wsmH) ) {    return wsmRet=SML_ERR_WRONG_USAGE;  }  wsmIndex = lookup(wsmH);  // usedSize > freeSize?  if ( usedSize >        (wsmBuf[wsmIndex].size - wsmBuf[wsmIndex].usedBytes) ) {    return wsmRet=SML_ERR_INVALID_SIZE;  }  // adapt usedSize  wsmBuf[wsmIndex].usedBytes += usedSize;  // move pFirstFree  wsmBuf[wsmIndex].pFirstFree += usedSize;  return wsmRet=SML_ERR_OK;} /** * FUNCTION: wsmReset * * Reset the Workspace * * PRE-Condition:   - * * POST-Condition:  all data is lost. The FirstFree Position equals  * the First Data position * * IN:      wsmH *          Handle to the open buffer *  * RETURN:  SML_ERR_OK, if O.K. * */Ret_t wsmReset (MemHandle_t wsmH) {  wsmIndex = lookup(wsmH);  wsmBuf[wsmIndex].pFirstFree = wsmBuf[wsmIndex].pFirstFree - wsmBuf[wsmIndex].usedBytes ;  wsmBuf[wsmIndex].pFirstData = wsmBuf[wsmIndex].pFirstFree;  wsmBuf[wsmIndex].usedBytes = 0;  return SML_ERR_OK;}/*======================================================================================*/#else/* WSM_LITE Version - uses only one buffer*//*======================================================================================*//* Global Vars *//* =========== *//* defines for convient use of global anchor */#define wsmRet          (mgrGetSyncMLAnchor())->wsmGlobals->wsmRet#define initWasCalled   (mgrGetSyncMLAnchor())->wsmGlobals->initWasCalled#define maxWsmAvailMem  (mgrGetSyncMLAnchor())->syncmlOptions->maxWorkspaceAvailMem#define wsmBuf          (mgrGetSyncMLAnchor())->wsmGlobals->wsmBuf#define wsmIndex        (mgrGetSyncMLAnchor())->wsmGlobals->wsmIndexvoid createDataStructs(void);void createDataStructs() {  if ( (mgrGetSyncMLAnchor())->wsmGlobals == NULL ) {    if ( ((mgrGetSyncMLAnchor())->wsmGlobals=smlLibMalloc(sizeof(WsmGlobals_t))) == 0 ) {      return;    }    wsmRet = 0;    initWasCalled = 0;    wsmIndex = 0;#ifdef __ANSI_C__    (mgrGetSyncMLAnchor())->wsmGlobals->wsmSm = NULL;#endif#ifdef __PALM_OS__    (mgrGetSyncMLAnchor())->wsmGlobals->wsmSm.smMemH = 0;    (mgrGetSyncMLAnchor())->wsmGlobals->wsmSm.smLocked = 0;#endif#ifdef __EPOC_OS__    (mgrGetSyncMLAnchor())->wsmGlobals->wsmSm = NULL;#endif  }}#define freeDataStructs() smlLibFree((mgrGetSyncMLAnchor())->wsmGlobals)/* private functions prototypes */static Short_t getNextFreeEntry();static Short_t deleteBufferHandle(MemHandle_t memH);static Short_t resetBufferGlobals(MemHandle_t memH);static Byte_t isMemAvailable(MemSize_t memToAlloc);/*************************************************************************//*  Internal Functions                                                   *//*************************************************************************/ /** * Delete memory handle from buffer. * Return -1, if handle not found. */static Short_t deleteBufferHandle(MemHandle_t memH) {  // reset the values  wsmBuf[0].memH = WSM_MEMH_UNUSED;  wsmBuf[0].pFirstFree = NULL;  wsmBuf[0].pFirstData = NULL;  wsmBuf[0].size      = 0;  wsmBuf[0].usedBytes = 0;  wsmBuf[0].flags     = ~WSM_VALID_F;  smlLibFree(wsmBuf[0].bufName);   // free mem  wsmBuf[0].bufName   = NULL;  return 0;}/** * Reset values in buffer table for entry memH. * If memH doesn't exist create an entry. * Return index to memH in buffer table, * or -1 if table is full */static Short_t resetBufferGlobals(MemHandle_t memH) {  if ( (wsmBuf[0].memH != memH) && (wsmBuf[0].memH == WSM_MEMH_UNUSED)) {    // create new one    wsmBuf[0].memH = memH;  } else {    // use existing one, which has to be reset prior usage    smlLibFree(wsmBuf[0].bufName);   // free mem  }  // reset the values  wsmBuf[0].pFirstFree = NULL;  wsmBuf[0].pFirstData = NULL;  wsmBuf[0].size      = 0;  wsmBuf[0].usedBytes = 0;  wsmBuf[0].flags     = WSM_VALID_F;  wsmBuf[0].bufName   = NULL;  return 0;}static Byte_t isMemAvailable(MemSize_t memToAlloc) {  MemSize_t actMem = memToAlloc;  if ( maxWsmAvailMem == 0 )    return 1;  // no memsize restrictions  if ( wsmBuf[0].memH != WSM_MEMH_UNUSED )    actMem += wsmBuf[0].size;  return (actMem <= maxWsmAvailMem);}/*************************************************************************//*  External Functions                                                   *//*************************************************************************//** * FUNCTION: wsmInit * * Initializes all Workspace Manager related resources.<BR> * Should only be called once! * * PRE-Condition:   This is the first function call to WSM * * POST-Condition:  All WSM resources are initialized * * IN:      wsmOpts *          WSM options, valid options are: *          <UL> *          <LI> maxAvailMem<BR> *               Maximal amount of memory which wsm can use for the buffers<BR> *               0 == no limitation *          </UL> *  * OUT:     wsmH *          Handle to new buffer * * RETURN:  SML_ERR_OK, if O.K. *          SML_ERR_INVALID_OPTIONS, if wsmOpts is not valid *          SML_ERR_NOT_ENOUGH_SPACE, if not enough available memory *          SML_ERR_WRONG_USAGE, if wsmInit was already called */Ret_t wsmInit (const WsmOptions_t *wsmOpts) {  // create global datastructs  createDataStructs();  if (NULL == mgrGetSyncMLAnchor()->wsmGlobals) {	  return SML_ERR_NOT_ENOUGH_SPACE;   }  // check if init was already called  if ( initWasCalled )    return SML_ERR_WRONG_USAGE;  // check options  if ( wsmOpts != NULL ) {    if ( wsmOpts->maxAvailMem > 0 ) {      maxWsmAvailMem = wsmOpts->maxAvailMem;    }  }  // init resources  wsmBuf[0].memH = WSM_MEMH_UNUSED;  wsmIndex = 0;  initWasCalled = (Byte_t) 1;  return wsmRet=SML_ERR_OK;} /** * FUNCTION: wsmCreate * * Creates and opens a new buffer with name bufName and size bufSize.<BR>  * If a buffer with name bufName already exists, the existing buffer  * is resized to bufSize. * * PRE-Condition:   bufSize > 0 *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -