📄 wsm.c
字号:
* POST-Condition: handle refers to buffer bufName; BufferSize = size * * IN: bufName * Name of buffer to be created * IN: bufSize * Size of buffer to be created * * OUT: wsmH * Handle to new buffer * * RETURN: SML_ERR_OK, if O.K. * SML_ERR_INVALID_SIZE, if bufSize <= 0 * SML_ERR_NOT_ENOUGH_SPACE, if available memory < bufSize * SML_ERR_WSM_BUF_TABLE_FULL, if buffer table is full * SML_ERR_WRONG_USAGE, if wsmInit wasn't called before * * @see wsmDestroy */Ret_t wsmCreate (String_t bufName, MemSize_t bufSize, MemHandle_t *wsmH) { *wsmH = 0; // 0 in case of error if ( ! initWasCalled ) return SML_ERR_WRONG_USAGE; // check buffer space if ( wsmBuf[0].memH != WSM_MEMH_UNUSED ) { return wsmRet=SML_ERR_WSM_BUF_TABLE_FULL; } // check for maxMemAvailable if ( ! isMemAvailable(bufSize) ) { return SML_ERR_NOT_ENOUGH_SPACE; } // create buffer if ( (wsmRet = smCreate(bufName, bufSize, wsmH)) != SML_ERR_OK ) { if ( wsmRet == SML_ERR_WRONG_USAGE ) { // buffer already exists // resize existing buffer // open buffer if ( (wsmRet = smOpen(bufName, wsmH)) != SML_ERR_OK ) { return wsmRet=SML_ERR_NOT_ENOUGH_SPACE; } // resize buffer if ( (wsmRet = smSetSize(*wsmH, bufSize)) != SML_ERR_OK ) { return wsmRet=SML_ERR_NOT_ENOUGH_SPACE; } } else { return wsmRet; } } // reset buffer vars resetBufferGlobals(*wsmH); // set buffer vars wsmBuf[0].size = bufSize; wsmBuf[0].bufName = smlLibStrdup(bufName); return wsmRet=SML_ERR_OK;}/** * FUNCTION: wsmOpen * * Open existing buffer with name bufName. * * PRE-Condition: WSM knows bufName * * POST-Condition: wsmH refers to buffer bufName * * IN: bufName * Name of buffer to be opened * * OUT: wsmH * Handle to new buffer * * RETURN: SML_ERR_OK, if O.K. * SML_WRONG_PARAM, if bufName is unknown * * @see wsmClose */Ret_t wsmOpen (String_t bufName, MemHandle_t *wsmH){ // open buffer if ( (wsmRet = smOpen(bufName, wsmH)) != SML_ERR_OK ) { return wsmRet; } // reset buffer vars resetBufferGlobals(*wsmH); // set buf vars wsmRet = smGetSize(*wsmH, &wsmBuf[0].size); wsmBuf[0].bufName = smlLibStrdup(bufName); return wsmRet=SML_ERR_OK;}/** * FUNCTION: wsmClose * * Close an open buffer. * * PRE-Condition: handle is valid; handle is unlocked * * POST-Condition: handle is not known to WSM any more * * IN: wsmH * Handle to the open buffer * * RETURN: SML_ERR_OK, if O.K. * SML_ERR_INVALID_HANDLE, if handle was invalid * SML_ERR_WRONG_USAGE, if handle was still locked * * @see wsmOpen */Ret_t wsmClose (MemHandle_t wsmH) { // check if handle is invalid // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_VALID_F)) ) { return wsmRet=SML_ERR_INVALID_HANDLE; } // close handle if ( (wsmRet = smClose(wsmH)) != SML_ERR_OK ) { return wsmRet; } wsmRet = deleteBufferHandle(wsmH); return wsmRet=SML_ERR_OK;}/** * FUNCTION: wsmDestroy * * Destroy existing buffer with name bufName. * * PRE-Condition: WSM knows bufName; handle is unlocked * * POST-Condition: buffer is not known to WSM any more; all resources * connected to this buffer are freed * * IN: bufName * Name of buffer to be opened * * RETURN: SML_ERR_OK, if O.K. * SML_ERR_WRONG_PARAM, if bufName is unknown to WSM * SML_ERR_WRONG_USAGE, if handle was still locked * * @see wsmCreate */Ret_t wsmDestroy (String_t bufName) { // free resources if ( (wsmRet = wsmClose(wsmBuf[0].memH)) != SML_ERR_OK ) { return wsmRet; } // free buffer if ( (wsmRet = smDestroy(bufName)) != SML_ERR_OK ) { return wsmRet; } return wsmRet=SML_ERR_OK;}/** * FUNCTION: wsmTerminate * * Terminate WSM; free all buffers and resources. * * PRE-Condition: all handles must be unlocked * * POST-Condition: all resources are freed * * RETURN: SML_ERR_OK, if O.K. * SML_ERR_WRONG_USAGE, if a handle was still locked * */Ret_t wsmTerminate () { 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 // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_VALID_F)) ) { return wsmRet=SML_ERR_INVALID_HANDLE; } // check if handle is unlocked // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_LOCKED_F)) ) { return wsmRet=SML_ERR_WRONG_USAGE; } if ( noBytes > wsmBuf[0].usedBytes ) { return wsmRet=SML_ERR_INVALID_SIZE; } // adapt usedSize wsmBuf[0].usedBytes -= noBytes; // move memory // check return ????? smlLibMemmove(wsmBuf[0].pFirstData, (wsmBuf[0].pFirstData + noBytes), wsmBuf[0].usedBytes); // move pFirstFree wsmBuf[0].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 // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_VALID_F)) ) { return wsmRet=SML_ERR_INVALID_HANDLE; } // check if handle is locked // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_LOCKED_F)) ) { return wsmRet=SML_ERR_WRONG_USAGE; } // lock if ( (wsmRet = smLock(wsmH, pMem)) != SML_ERR_OK ) { return wsmRet=SML_ERR_UNSPECIFIC; } // set local pointers wsmBuf[0].pFirstData = *pMem; wsmBuf[0].pFirstFree = *pMem + wsmBuf[0].usedBytes; wsmBuf[0].flags |= WSM_LOCKED_F; switch (requestedPos) { case SML_FIRST_DATA_ITEM: *pMem = wsmBuf[0].pFirstData; break; case SML_FIRST_FREE_ITEM: *pMem = wsmBuf[0].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 // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_VALID_F)) ) { return wsmRet=SML_ERR_INVALID_HANDLE; } *freeSize = wsmBuf[0].size - wsmBuf[0].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 // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_VALID_F)) ) { return wsmRet=SML_ERR_INVALID_HANDLE; } *usedSize = wsmBuf[0].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 // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_VALID_F)) ) { return wsmRet=SML_ERR_INVALID_HANDLE; } // check if handle is already unlocked // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_LOCKED_F)) ) { return wsmRet=SML_ERR_WRONG_USAGE; } // unlock if ( (wsmRet = smUnlock(wsmH)) != SML_ERR_OK ) { return wsmRet=SML_ERR_UNSPECIFIC; } // set local pointers wsmBuf[0].pFirstData = NULL; wsmBuf[0].pFirstFree = NULL; wsmBuf[0].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 // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_VALID_F)) ) { return wsmRet=SML_ERR_INVALID_HANDLE; } // check if handle is unlocked // must be buffer 0, as only this one exists if ( ! ((wsmBuf[0].memH == wsmH) || (wsmBuf[0].flags & WSM_LOCKED_F)) ) { return wsmRet=SML_ERR_WRONG_USAGE; } // usedSize > freeSize? if ( usedSize > (wsmBuf[0].size - wsmBuf[0].usedBytes) ) { return wsmRet=SML_ERR_INVALID_SIZE; } // adapt usedSize wsmBuf[0].usedBytes += usedSize; // move pFirstFree wsmBuf[0].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) { wsmBuf[0].pFirstFree = wsmBuf[0].pFirstFree - wsmBuf[0].usedBytes ; wsmBuf[0].pFirstData = wsmBuf[0].pFirstFree; wsmBuf[0].usedBytes = 0; return SML_ERR_OK;}#endif // #ifndef __SML_LITE__#endif // #idndef NOWSM
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -