From fc2b8dddf6f2b0181d4410b9588b478e51118c6c Mon Sep 17 00:00:00 2001 From: Maxim Polyakov Date: Tue, 4 Oct 2022 17:20:11 +0300 Subject: [PATCH] Fixed a various warnings Signed-off-by: Maxim Polyakov --- libc-test/src/functional/test-iterate.c | 18 ++++- libc-test/src/functional/test-mallinfo.c | 15 ++++ .../src/functional/test-malloc-api-common.h | 24 ++++++- .../src/functional/test-malloc-backtrace.c | 15 ++++ .../src/functional/test-malloc-disable.c | 15 ++++ .../functional/test-malloc-info-stats-print.h | 15 ++++ libc-test/src/functional/test-malloc-info.c | 18 ++++- .../src/functional/test-malloc-stats-common.h | 69 ++++++++++++------- .../src/functional/test-malloc-stats-print.c | 15 ++++ libc-test/src/functional/test-mallopt.c | 15 ++++ porting/linux/user/include/malloc.h | 3 +- .../linux/user/src/env/__libc_start_main.c | 1 - porting/linux/user/src/malloc/stats.c | 27 +++++++- .../user/src/thread/pthread_getspecific.c | 11 --- .../user/src/thread/pthread_setspecific.c | 12 ---- 15 files changed, 216 insertions(+), 57 deletions(-) delete mode 100644 porting/linux/user/src/thread/pthread_getspecific.c delete mode 100644 porting/linux/user/src/thread/pthread_setspecific.c diff --git a/libc-test/src/functional/test-iterate.c b/libc-test/src/functional/test-iterate.c index 3ee37b9d..f28e38e9 100644 --- a/libc-test/src/functional/test-iterate.c +++ b/libc-test/src/functional/test-iterate.c @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include @@ -33,7 +48,8 @@ static const size_t allocs_sizes[ALLOCATIONS_NUMBER] = { 2 * 1024 * 1024, 8 * 1024 * 1024, 16 * 1024 * 1024, - 32 * 1024 * 1024}; + 32 * 1024 * 1024 +}; void iterate_callback(void *base, size_t size, void *data) { diff --git a/libc-test/src/functional/test-mallinfo.c b/libc-test/src/functional/test-mallinfo.c index 4b86a060..18ff45f0 100644 --- a/libc-test/src/functional/test-mallinfo.c +++ b/libc-test/src/functional/test-mallinfo.c @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "test-malloc-stats-common.h" static int stats_from_mallinfo(malloc_thread_stats_t *stats, long long *total_free_heap_space, int use_mallinfo2) diff --git a/libc-test/src/functional/test-malloc-api-common.h b/libc-test/src/functional/test-malloc-api-common.h index 0f2f2d21..011fe9e1 100644 --- a/libc-test/src/functional/test-malloc-api-common.h +++ b/libc-test/src/functional/test-malloc-api-common.h @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef TEST_ITERATE_DISABLE_COMMON_H #define TEST_ITERATE_DISABLE_COMMON_H @@ -7,12 +22,15 @@ int check_and_report(const char *message, int (*check_func)(void)) { t_printf("%s...", message); int ret = check_func(); - if (ret == 0) { + if (ret == 0) + { t_printf("Success\n"); - } else { + } + else + { t_error("Failed\n"); } return ret; } -#endif //TEST_ITERATE_DISABLE_COMMON_H \ No newline at end of file +#endif // TEST_ITERATE_DISABLE_COMMON_H \ No newline at end of file diff --git a/libc-test/src/functional/test-malloc-backtrace.c b/libc-test/src/functional/test-malloc-backtrace.c index e7ed293b..fb440920 100644 --- a/libc-test/src/functional/test-malloc-backtrace.c +++ b/libc-test/src/functional/test-malloc-backtrace.c @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include "test-malloc-api-common.h" diff --git a/libc-test/src/functional/test-malloc-disable.c b/libc-test/src/functional/test-malloc-disable.c index 7ce503df..73339192 100644 --- a/libc-test/src/functional/test-malloc-disable.c +++ b/libc-test/src/functional/test-malloc-disable.c @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include diff --git a/libc-test/src/functional/test-malloc-info-stats-print.h b/libc-test/src/functional/test-malloc-info-stats-print.h index 28b186b2..e32bed40 100644 --- a/libc-test/src/functional/test-malloc-info-stats-print.h +++ b/libc-test/src/functional/test-malloc-info-stats-print.h @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef TEST_MALLOC_STATS_H #define TEST_MALLOC_STATS_H diff --git a/libc-test/src/functional/test-malloc-info.c b/libc-test/src/functional/test-malloc-info.c index e3ee41cd..e069c19d 100644 --- a/libc-test/src/functional/test-malloc-info.c +++ b/libc-test/src/functional/test-malloc-info.c @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include "test-malloc-info-stats-print.h" @@ -27,8 +42,7 @@ static const xmlChar *get_attribute(const char *attr_name, xmlNodePtr node) return NULL; } -static xmlNodePtr -find_child_node_with_attr(const char *name, const char *attr_name, const char *attr_value, xmlNodePtr parent) +static xmlNodePtr find_child_node_with_attr(const char *name, const char *attr_name, const char *attr_value, xmlNodePtr parent) { if (parent == NULL) { diff --git a/libc-test/src/functional/test-malloc-stats-common.h b/libc-test/src/functional/test-malloc-stats-common.h index 39242b68..4723e974 100644 --- a/libc-test/src/functional/test-malloc-stats-common.h +++ b/libc-test/src/functional/test-malloc-stats-common.h @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef TEST_MALLOC_STATS_COMMON_H #define TEST_MALLOC_STATS_COMMON_H @@ -11,27 +26,34 @@ #include "test.h" #define SIZES_COUNT 11 -#define SIZE_ALIGN (8*sizeof(size_t)) -#define MMAP_THRESHOLD ((0x1c00*SIZE_ALIGN) - OVERHEAD) -#define LIST_OVERHEAD (2*sizeof(void*)) -#define OVERHEAD (2*sizeof(size_t) + sizeof(void*) + LIST_OVERHEAD) +#define SIZE_ALIGN (8 * sizeof(size_t)) +#define MMAP_THRESHOLD ((0x1c00 * SIZE_ALIGN) - OVERHEAD) +#define LIST_OVERHEAD (2 * sizeof(void *)) +#define OVERHEAD (2 * sizeof(size_t) + sizeof(void *) + LIST_OVERHEAD) static size_t sizes[SIZES_COUNT] = { - 23, 32, - 256, 3072, - 3584, 229376, - 262144, 327680, - 8 * 1024 * 1024,16 * 1024 * 1024, + 23, + 32, + 256, + 3072, + 3584, + 229376, + 262144, + 327680, + 8 * 1024 * 1024, + 16 * 1024 * 1024, 32 * 1024 * 1024 }; -typedef struct { +typedef struct +{ long long mmapped_regions; long long total_mmapped_memory; long long total_allocated_memory; } malloc_thread_stats_t; -typedef struct { +typedef struct +{ size_t alloc_size; pthread_barrier_t *alloc_barrier; pthread_barrier_t *free_barrier; @@ -41,8 +63,10 @@ typedef struct { static malloc_thread_stats_t get_total_from_test_sizes() { malloc_thread_stats_t total_stats = {0}; - for (size_t i = 0; i < SIZES_COUNT; i++) { - if (sizes[i] > MMAP_THRESHOLD) { + for (size_t i = 0; i < SIZES_COUNT; i++) + { + if (sizes[i] > MMAP_THRESHOLD) + { total_stats.total_mmapped_memory += sizes[i]; total_stats.mmapped_regions++; } @@ -53,7 +77,8 @@ static malloc_thread_stats_t get_total_from_test_sizes() static int expect_greater_equal(long long amt1, long long amt2, const char *amt1_name, const char *amt2_name) { - if (amt1 >= amt2) { + if (amt1 >= amt2) + { return 1; } t_error("Expected %s(value: %lld) to be >= %s(value: %lld)\n", amt1_name, amt1, amt2_name, amt2); @@ -62,7 +87,8 @@ static int expect_greater_equal(long long amt1, long long amt2, const char *amt1 static int expect_equal(long long amt, long long value, const char *amt_name) { - if (amt == value) { + if (amt == value) + { return 1; } t_error("Expected %s(value: %lld) to be %lld\n", amt_name, amt, value); @@ -77,23 +103,20 @@ static int validate_total_allocated(malloc_thread_stats_t *total_stats) total_stats->total_allocated_memory, total_from_test_sizes.total_allocated_memory, "allocated memory", - "total memory from test sizes" - ); + "total memory from test sizes"); result &= expect_greater_equal( total_stats->total_mmapped_memory, total_from_test_sizes.total_mmapped_memory, "mmapped memory", - "total large memory from test sizes" - ); + "total large memory from test sizes"); result &= expect_equal( total_stats->mmapped_regions, total_from_test_sizes.mmapped_regions, - "mmapped regions" - ); + "mmapped regions"); return result; } -static int validate_all_freed(malloc_thread_stats_t *total_stats) +static int validate_all_freed(malloc_thread_stats_t *total_stats) { int result = expect_equal(total_stats->total_allocated_memory, 0, "allocated memory"); result &= expect_equal(total_stats->total_mmapped_memory, 0, "mmapped memory"); @@ -112,4 +135,4 @@ static void *allocate_wait_free(void *arg) return NULL; } -#endif //TEST_MALLOC_STATS_COMMON_H +#endif // TEST_MALLOC_STATS_COMMON_H diff --git a/libc-test/src/functional/test-malloc-stats-print.c b/libc-test/src/functional/test-malloc-stats-print.c index bc8bf4ca..0a4976c0 100644 --- a/libc-test/src/functional/test-malloc-stats-print.c +++ b/libc-test/src/functional/test-malloc-stats-print.c @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "test-malloc-info-stats-print.h" static void stderr_stats_cb(void) diff --git a/libc-test/src/functional/test-mallopt.c b/libc-test/src/functional/test-mallopt.c index c7eab1f4..b6e3c608 100644 --- a/libc-test/src/functional/test-mallopt.c +++ b/libc-test/src/functional/test-mallopt.c @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include "test-malloc-api-common.h" diff --git a/porting/linux/user/include/malloc.h b/porting/linux/user/include/malloc.h index f0448125..e32eeea7 100644 --- a/porting/linux/user/include/malloc.h +++ b/porting/linux/user/include/malloc.h @@ -1,6 +1,8 @@ #ifndef _MALLOC_H #define _MALLOC_H +#include + #ifdef __cplusplus extern "C" { #endif @@ -10,7 +12,6 @@ extern "C" { #define __NEED_uintptr_t #include -#include void *malloc (size_t); void *calloc (size_t, size_t); diff --git a/porting/linux/user/src/env/__libc_start_main.c b/porting/linux/user/src/env/__libc_start_main.c index 672443e2..d9b89f5e 100644 --- a/porting/linux/user/src/env/__libc_start_main.c +++ b/porting/linux/user/src/env/__libc_start_main.c @@ -107,7 +107,6 @@ static int libc_start_main_stage2(int (*main)(int,char **,char **), int argc, ch #endif libc.initialized = 1; - /* Pass control to the application */ exit(main(argc, argv, envp)); return 0; diff --git a/porting/linux/user/src/malloc/stats.c b/porting/linux/user/src/malloc/stats.c index 9b6563eb..999488ad 100644 --- a/porting/linux/user/src/malloc/stats.c +++ b/porting/linux/user/src/malloc/stats.c @@ -1,6 +1,22 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include #include #include +#include #include "pthread_impl.h" #include "malloc_impl.h" @@ -27,8 +43,11 @@ static void stat_printf(write_cb_fun *write_cb, void *write_cb_arg, const char * va_list args; va_start(args, fmt); char buf[STAT_PRINTF_MAX_LEN + 1]; - vsnprintf(buf, STAT_PRINTF_MAX_LEN, fmt, args); - write_cb(write_cb_arg, buf); + if (vsnprintf(buf, STAT_PRINTF_MAX_LEN, fmt, args)) { + write_cb(write_cb_arg, buf); + } else { + fprintf(stderr, "Error writing to buffer"); + } va_end(args); } @@ -175,7 +194,9 @@ static void print_total_free_heap_space( static void print_to_file(void *fp, const char *s) { - fputs(s, fp); + if (fputs(s, fp) == EOF) { + fprintf(stderr, "Error writing to file stream: %s", strerror(errno)); + } } static void add_stats(malloc_stats_t *destination, const malloc_stats_t *source) diff --git a/porting/linux/user/src/thread/pthread_getspecific.c b/porting/linux/user/src/thread/pthread_getspecific.c deleted file mode 100644 index 63c1a969..00000000 --- a/porting/linux/user/src/thread/pthread_getspecific.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "pthread_impl.h" -#include - -static void *__pthread_getspecific(pthread_key_t k) -{ - struct pthread *self = __pthread_self(); - return self->tsd[k]; -} - -weak_alias(__pthread_getspecific, pthread_getspecific); -weak_alias(__pthread_getspecific, tss_get); diff --git a/porting/linux/user/src/thread/pthread_setspecific.c b/porting/linux/user/src/thread/pthread_setspecific.c deleted file mode 100644 index 15ab22fd..00000000 --- a/porting/linux/user/src/thread/pthread_setspecific.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "pthread_impl.h" - -int pthread_setspecific(pthread_key_t k, const void *x) -{ - struct pthread *self = __pthread_self(); - /* Avoid unnecessary COW */ - if (self->tsd[k] != x) { - self->tsd[k] = (void *)x; - self->tsd_used = 1; - } - return 0; -} -- GitLab