diff --git a/modules/src/kc_memory_dump.c b/modules/src/kc_memory_dump.c index 49e63de..d858b29 100644 --- a/modules/src/kc_memory_dump.c +++ b/modules/src/kc_memory_dump.c @@ -60,7 +60,7 @@ int bytes, bool binary, bool ascii, int column) { // 改行コード 考慮 - if (((int)buff_size + 2) < column) + if (((int)buff_size - 2) < column) { // バッファ不足の場合は何もしない return false; } diff --git a/modules/src/kc_memory_dump.c b/modules/src/kc_memory_dump.c index 49e63de..d858b29 100644 --- a/modules/src/kc_memory_dump.c +++ b/modules/src/kc_memory_dump.c @@ -60,7 +60,7 @@ int bytes, bool binary, bool ascii, int column) { // 改行コード 考慮 - if (((int)buff_size + 2) < column) + if (((int)buff_size - 2) < column) { // バッファ不足の場合は何もしない return false; } diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index c942ceb..5c78972 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -10,6 +10,13 @@ #if (KC_IS_WINDOWS) +/** + * C11 の thrd_create と同様 + * + * @param thr スレッド識別子 + * @param func 関数 + * @param arg 関数に渡される引数 + */ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) { if ((thr == NULL) || (func == NULL)) @@ -32,6 +39,14 @@ } return thrd_success; } + +/** + * 指定されたスレッドを待機します。 + * res が NULL でない場合、スレッドの関数の戻り値が格納されます。 + * + * @param thr スレッド識別子 + * @param res スレッド関数の戻り値格納用 + */ int thrd_join(thrd_t thr, int *res) { if (WaitForSingleObject(thr.handle, INFINITE) != WAIT_OBJECT_0) @@ -51,6 +66,11 @@ return thrd_success; } +/** + * スレッドをデタッチします。 + * + * @param thr スレッド識別子 + */ int thrd_detach(thrd_t thr) { if (thr.handle == NULL) @@ -64,6 +84,11 @@ return thrd_success; } +/** + * 現在のスレッド識別子を返します。 + * + * @return スレッド識別子 + */ thrd_t thrd_current(void) { thrd_t current; @@ -72,16 +97,34 @@ return current; } +/** + * スレッドが同一か判定します。 + * + * @param lhs スレッド識別子 + * @param rhs スレッド識別子 + * @return true/false (同一/異なる) + */ int thrd_equal(thrd_t lhs, thrd_t rhs) { return (lhs.thread_id == rhs.thread_id); } +/** + * スレッドを切り替えます。 + */ void thrd_yield(void) { SwitchToThread(); } +/** + * 指定された時間 sleep します。 + * Windows の場合、ミリ秒以下の指定は無視されます。 + * また、remaining には常に 0 秒が格納されます。 + * + * @param duration sleep する時間 + * @param remaining 常に残り秒数は0となります。 + */ int thrd_sleep(const struct timespec *duration, struct timespec *remaining) { if (duration == NULL) @@ -145,7 +188,7 @@ } /** - * + * 指定されたミューテックスのロックを解除します。 */ int mtx_unlock(mtx_t *mtx) { @@ -153,6 +196,12 @@ return thrd_success; } +/** + * 条件変数を初期化します。 + * + * @param cond 条件変数 + * @return thrd_success/thrd_error (成功/失敗) + */ int cnd_init(cnd_t *cond) { if (cond == NULL) @@ -172,6 +221,7 @@ WakeConditionVariable(&cond->cond); return thrd_success; } + int cnd_broadcast(cnd_t *cond) { if (cond == NULL) diff --git a/modules/src/kc_memory_dump.c b/modules/src/kc_memory_dump.c index 49e63de..d858b29 100644 --- a/modules/src/kc_memory_dump.c +++ b/modules/src/kc_memory_dump.c @@ -60,7 +60,7 @@ int bytes, bool binary, bool ascii, int column) { // 改行コード 考慮 - if (((int)buff_size + 2) < column) + if (((int)buff_size - 2) < column) { // バッファ不足の場合は何もしない return false; } diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index c942ceb..5c78972 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -10,6 +10,13 @@ #if (KC_IS_WINDOWS) +/** + * C11 の thrd_create と同様 + * + * @param thr スレッド識別子 + * @param func 関数 + * @param arg 関数に渡される引数 + */ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) { if ((thr == NULL) || (func == NULL)) @@ -32,6 +39,14 @@ } return thrd_success; } + +/** + * 指定されたスレッドを待機します。 + * res が NULL でない場合、スレッドの関数の戻り値が格納されます。 + * + * @param thr スレッド識別子 + * @param res スレッド関数の戻り値格納用 + */ int thrd_join(thrd_t thr, int *res) { if (WaitForSingleObject(thr.handle, INFINITE) != WAIT_OBJECT_0) @@ -51,6 +66,11 @@ return thrd_success; } +/** + * スレッドをデタッチします。 + * + * @param thr スレッド識別子 + */ int thrd_detach(thrd_t thr) { if (thr.handle == NULL) @@ -64,6 +84,11 @@ return thrd_success; } +/** + * 現在のスレッド識別子を返します。 + * + * @return スレッド識別子 + */ thrd_t thrd_current(void) { thrd_t current; @@ -72,16 +97,34 @@ return current; } +/** + * スレッドが同一か判定します。 + * + * @param lhs スレッド識別子 + * @param rhs スレッド識別子 + * @return true/false (同一/異なる) + */ int thrd_equal(thrd_t lhs, thrd_t rhs) { return (lhs.thread_id == rhs.thread_id); } +/** + * スレッドを切り替えます。 + */ void thrd_yield(void) { SwitchToThread(); } +/** + * 指定された時間 sleep します。 + * Windows の場合、ミリ秒以下の指定は無視されます。 + * また、remaining には常に 0 秒が格納されます。 + * + * @param duration sleep する時間 + * @param remaining 常に残り秒数は0となります。 + */ int thrd_sleep(const struct timespec *duration, struct timespec *remaining) { if (duration == NULL) @@ -145,7 +188,7 @@ } /** - * + * 指定されたミューテックスのロックを解除します。 */ int mtx_unlock(mtx_t *mtx) { @@ -153,6 +196,12 @@ return thrd_success; } +/** + * 条件変数を初期化します。 + * + * @param cond 条件変数 + * @return thrd_success/thrd_error (成功/失敗) + */ int cnd_init(cnd_t *cond) { if (cond == NULL) @@ -172,6 +221,7 @@ WakeConditionVariable(&cond->cond); return thrd_success; } + int cnd_broadcast(cnd_t *cond) { if (cond == NULL) diff --git a/modules/test/src/test_list_linked.c b/modules/test/src/test_list_linked.c index 5e18063..95eb114 100644 --- a/modules/test/src/test_list_linked.c +++ b/modules/test/src/test_list_linked.c @@ -261,6 +261,17 @@ int *res_val = list->get(list, 1, NULL); assert_equals(99, *res_val); + // 値設定 (元の値, サイズ取得[設定値の方がサイズが大きい]) + set_value = 101; + int orig_val_array[2]; + orig_size = sizeof(int) * 2; + ret = list->set(list, 1, &set_value, sizeof(int), orig_val_array, &orig_size); + assert_true(ret); + assert_equals(99, orig_val_array[0]); + assert_equals((int)sizeof(int), (int)orig_size); + res_val = list->get(list, 1, NULL); + assert_equals(101, *res_val); + // 値設定 (元の値取得, サイズ指定なし[=値取得不可]) set_value = 98; orig_val = 9999; diff --git a/modules/src/kc_memory_dump.c b/modules/src/kc_memory_dump.c index 49e63de..d858b29 100644 --- a/modules/src/kc_memory_dump.c +++ b/modules/src/kc_memory_dump.c @@ -60,7 +60,7 @@ int bytes, bool binary, bool ascii, int column) { // 改行コード 考慮 - if (((int)buff_size + 2) < column) + if (((int)buff_size - 2) < column) { // バッファ不足の場合は何もしない return false; } diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index c942ceb..5c78972 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -10,6 +10,13 @@ #if (KC_IS_WINDOWS) +/** + * C11 の thrd_create と同様 + * + * @param thr スレッド識別子 + * @param func 関数 + * @param arg 関数に渡される引数 + */ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) { if ((thr == NULL) || (func == NULL)) @@ -32,6 +39,14 @@ } return thrd_success; } + +/** + * 指定されたスレッドを待機します。 + * res が NULL でない場合、スレッドの関数の戻り値が格納されます。 + * + * @param thr スレッド識別子 + * @param res スレッド関数の戻り値格納用 + */ int thrd_join(thrd_t thr, int *res) { if (WaitForSingleObject(thr.handle, INFINITE) != WAIT_OBJECT_0) @@ -51,6 +66,11 @@ return thrd_success; } +/** + * スレッドをデタッチします。 + * + * @param thr スレッド識別子 + */ int thrd_detach(thrd_t thr) { if (thr.handle == NULL) @@ -64,6 +84,11 @@ return thrd_success; } +/** + * 現在のスレッド識別子を返します。 + * + * @return スレッド識別子 + */ thrd_t thrd_current(void) { thrd_t current; @@ -72,16 +97,34 @@ return current; } +/** + * スレッドが同一か判定します。 + * + * @param lhs スレッド識別子 + * @param rhs スレッド識別子 + * @return true/false (同一/異なる) + */ int thrd_equal(thrd_t lhs, thrd_t rhs) { return (lhs.thread_id == rhs.thread_id); } +/** + * スレッドを切り替えます。 + */ void thrd_yield(void) { SwitchToThread(); } +/** + * 指定された時間 sleep します。 + * Windows の場合、ミリ秒以下の指定は無視されます。 + * また、remaining には常に 0 秒が格納されます。 + * + * @param duration sleep する時間 + * @param remaining 常に残り秒数は0となります。 + */ int thrd_sleep(const struct timespec *duration, struct timespec *remaining) { if (duration == NULL) @@ -145,7 +188,7 @@ } /** - * + * 指定されたミューテックスのロックを解除します。 */ int mtx_unlock(mtx_t *mtx) { @@ -153,6 +196,12 @@ return thrd_success; } +/** + * 条件変数を初期化します。 + * + * @param cond 条件変数 + * @return thrd_success/thrd_error (成功/失敗) + */ int cnd_init(cnd_t *cond) { if (cond == NULL) @@ -172,6 +221,7 @@ WakeConditionVariable(&cond->cond); return thrd_success; } + int cnd_broadcast(cnd_t *cond) { if (cond == NULL) diff --git a/modules/test/src/test_list_linked.c b/modules/test/src/test_list_linked.c index 5e18063..95eb114 100644 --- a/modules/test/src/test_list_linked.c +++ b/modules/test/src/test_list_linked.c @@ -261,6 +261,17 @@ int *res_val = list->get(list, 1, NULL); assert_equals(99, *res_val); + // 値設定 (元の値, サイズ取得[設定値の方がサイズが大きい]) + set_value = 101; + int orig_val_array[2]; + orig_size = sizeof(int) * 2; + ret = list->set(list, 1, &set_value, sizeof(int), orig_val_array, &orig_size); + assert_true(ret); + assert_equals(99, orig_val_array[0]); + assert_equals((int)sizeof(int), (int)orig_size); + res_val = list->get(list, 1, NULL); + assert_equals(101, *res_val); + // 値設定 (元の値取得, サイズ指定なし[=値取得不可]) set_value = 98; orig_val = 9999; diff --git a/modules/test/src/test_memory_dump.c b/modules/test/src/test_memory_dump.c index 2a4991d..e3aefec 100644 --- a/modules/test/src/test_memory_dump.c +++ b/modules/test/src/test_memory_dump.c @@ -52,25 +52,45 @@ */ static void test_memory_dump(void) { - char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - KcMemoryEntry entry = { - .size = 5, - .mark = KC_MEMORY_ALLOCATED, - .file = "test_file", - .func = "test_func", - .line = 123, - ._prev = NULL, - ._next = NULL, - .data = test_data}; + { + char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + KcMemoryEntry entry = { + .size = 5, + .mark = KC_MEMORY_ALLOCATED, + .file = "test_file", + .func = "test_func", + .line = 123, + ._prev = NULL, + ._next = NULL, + .data = test_data}; - char buff[256]; - bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, true, 130); - assert_true(ret); - assert_equals("test_file:123 ( 5.000 B) [test_func] " - " | 41 42 43 44 45 -- -- -- -- -- -- -- -- -- -- -- " - " | ABCDE " - "\n", - buff); + char buff[256]; + bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, true, 130); + assert_true(ret); + assert_equals("test_file:123 ( 5.000 B) [test_func] " + " | 41 42 43 44 45 -- -- -- -- -- -- -- -- -- -- -- " + " | ABCDE " + "\n", + buff); + } + { + char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + KcMemoryEntry entry = { + .size = 5, + .mark = KC_MEMORY_ALLOCATED, + .file = "test_file", + .func = "test_func", + .line = 123, + ._prev = NULL, + ._next = NULL, + .data = test_data}; + + char buff[256]; + bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, true, 42); + assert_true(ret); + const char *ex = "test_file:123 ( 5.000 B) [test_func] \n"; + assert_equals(ex, buff); + } } /**