// Increment UINT Inc(COUNTER *c) { UINT ret; // Validate arguments if (c == NULL) { return 0; } if (c->Ready == false) { return 0; } Lock(c->lock); { if (c->Ready == false) { ret = 0; } else { c->c++; ret = c->c; } } Unlock(c->lock); // KS KS_INC(KS_INC_COUNT); KS_INC(KS_CURRENT_COUNT); return ret; }
LOCK *NewLock() { LOCK *lock = NewLockMain(); // KS KS_INC(KS_NEWLOCK_COUNT); KS_INC(KS_CURRENT_LOCK_COUNT); return lock; }
// Lock bool LockInner(LOCK *lock) { // Validate arguments if (lock == NULL) { return false; } // KS KS_INC(KS_LOCK_COUNT); KS_INC(KS_CURRENT_LOCKED_COUNT); return OSLock(lock); }
// Combine the string UINT StrCat(char *dst, UINT size, char *src) { UINT len1, len2, len_test; // Validate arguments if (dst == NULL || src == NULL) { return 0; } // KS KS_INC(KS_STRCAT_COUNT); if (size == 0) { // Ignore the length size = 0x7fffffff; } len1 = StrLen(dst); len2 = StrLen(src); len_test = len1 + len2 + 1; if (len_test > size) { if (len2 <= (len_test - size)) { return 0; } len2 -= len_test - size; } Copy(dst + len1, src, len2); dst[len1 + len2] = 0; return len1 + len2; }
// Make sure that the string is within the specified length bool StrCheckLen(char *str, UINT len) { UINT count = 0; UINT i; // Validate arguments if (str == NULL) { return false; } // KS KS_INC(KS_STRCHECK_COUNT); for (i = 0;;i++) { if (str[i] == '\0') { return true; } count++; if (count > len) { return false; } } }
// Increase of the reference counter UINT AddRef(REF *ref) { UINT c; // Validate arguments if (ref == NULL) { return 0; } c = Inc(ref->c); // KS KS_INC(KS_ADDREF_COUNT); KS_INC(KS_CURRENT_REFED_COUNT); return c; }
// Create a reference counter REF *NewRef() { REF *ref; // Memory allocation ref = Malloc(sizeof(REF)); // Create a Counter ref->c = NewCounter(); // Increment only once Inc(ref->c); // KS KS_INC(KS_NEWREF_COUNT); KS_INC(KS_CURRENT_REF_COUNT); KS_INC(KS_ADDREF_COUNT); KS_INC(KS_CURRENT_REFED_COUNT); return ref; }
// Get the length of the string UINT StrLen(char *str) { // Validate arguments if (str == NULL) { return 0; } // KS KS_INC(KS_STRLEN_COUNT); return (UINT)strlen(str); }
// 文字列の長さを取得 UINT StrLen(char *str) { // 引数チェック if (str == NULL) { return 0; } // KS KS_INC(KS_STRLEN_COUNT); return (UINT)strlen(str); }
// Wait for event bool Wait(EVENT *e, UINT timeout) { // Validate arguments if (e == NULL) { return false; } // KS KS_INC(KS_WAIT_COUNT); return OSWaitEvent(e, timeout); }
// Delete the lock object void DeleteLock(LOCK *lock) { // Validate arguments if (lock == NULL) { return; } // KS KS_INC(KS_DELETELOCK_COUNT); KS_DEC(KS_CURRENT_LOCK_COUNT); OSDeleteLock(lock); }
// Unlock void UnlockInner(LOCK *lock) { // Validate arguments if (lock == NULL) { return; } // KS KS_INC(KS_UNLOCK_COUNT); KS_DEC(KS_CURRENT_LOCKED_COUNT); OSUnlock(lock); }
// Delete the counter void DeleteCounter(COUNTER *c) { // Validate arguments if (c == NULL) { return; } // KS KS_INC(KS_DELETE_COUNTER_COUNT); KS_SUB(KS_CURRENT_COUNT, c->c); DeleteLock(c->lock); Free(c); }
UINT StrCpyAllowOverlap(char *dst, UINT size, char *src) { UINT len; // Validate arguments if (dst == src) { return StrLen(src); } if (dst == NULL || src == NULL) { if (src == NULL && dst != NULL) { if (size >= 1) { dst[0] = '\0'; } } return 0; } if (size == 1) { dst[0] = '\0'; return 0; } if (size == 0) { // Ignore the length size = 0x7fffffff; } // Check the length len = StrLen(src); if (len <= (size - 1)) { Move(dst, src, len + 1); } else { len = size - 1; Move(dst, src, len); dst[len] = '\0'; } // KS KS_INC(KS_STRCPY_COUNT); return len; }
// 文字列をコピー UINT StrCpy(char *dst, UINT size, char *src) { UINT len; // 引数チェック if (dst == src) { return StrLen(src); } if (dst == NULL || src == NULL) { if (src == NULL && dst != NULL) { if (size >= 1) { dst[0] = '\0'; } } return 0; } if (size == 1) { dst[0] = '\0'; return 0; } if (size == 0) { // 長さを無視 size = 0x7fffffff; } // 長さをチェック len = StrLen(src); if (len <= (size - 1)) { Copy(dst, src, len + 1); } else { len = size - 1; Copy(dst, src, len); dst[len] = '\0'; } // KS KS_INC(KS_STRCPY_COUNT); return len; }
// Creating an event object EVENT *NewEvent() { // Memory allocation EVENT *e = Malloc(sizeof(EVENT)); // Reference counter e->ref = NewRef(); // Event initialization OSInitEvent(e); // KS KS_INC(KS_NEWEVENT_COUNT); return e; }
// Delete the event void CleanupEvent(EVENT *e) { // Validate arguments if (e == NULL) { return; } // Release event OSFreeEvent(e); // Memory release Free(e); // KS KS_INC(KS_FREEEVENT_COUNT); }
// Creating a counter COUNTER *NewCounter() { COUNTER *c; // Memory allocation c = Malloc(sizeof(COUNTER)); // Initialization c->Ready = true; c->c = 0; // Lock created c->lock = NewLock(); // KS KS_INC(KS_NEW_COUNTER_COUNT); return c; }