From 64803277bbdbe0a943360d918298a48157d9da55 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 16 Nov 2021 21:36:17 -0500 Subject: [PATCH] Add type-checked versions of allocation and deallocations functions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit A common mistake when porting things from C to C++ is to use malloc for types that have a on-trivial constructor (and to not call the constructor explicitly either). For example: struct foo { std::vector field; }; foo *f = (foo *) zmalloc(sizeof(*f)); This allocates a `foo` without calling the constructor, leaving it in an invalid state. Same idea when free-ing with free something that has a non-trivial destructor. To avoid this, I suggest adding templated allocation functions that we will use throughout, that verify if the given type is safe to malloc (and generate a compilation failure if not). The existing code barely needs changes. For example: - (foo *) zmalloc(sizeof(*f)) + zmalloc() For simplicity I propose that as soon as a type is non-POD (std::is_pod::value is false), we prevent using malloc/free on it. It would be ok in theory to allocate such a type with malloc and free with free, but call the constructor (using placement-new) and destructor explicitly, but I don't see why we would want to do that. It might also be technically more correct to use a combination of std::is_trivially_constructible and std::is_trivially_destructible (std::is_pod being not fine-grained enough), but using std::is_pod just keeps things simpler. This patch introduces the following templated allocation functions: 1. zmalloc() 2. zmalloc(size) 3. malloc() 4. malloc(size) 5. calloc(nmemb) 1. Allocate one T, zero-initialized 2. Allocate a buffer of size `size`, zero-initialized, this is used when the caller calculates the size to allocate, like when using flexible array members 3. Same as 1, but without the zero-initialization 4. Same as 2, but without the zero-initialization 5. Allocate an array of `nmemb` elements of type T, zero-initialized For the de-allocation side, add templated `free` function declaration that uses std::enable_if (SFINAE) to declare a deleted prototype if the type T isn't safe to free (causing a compilation error). There are a lot of places where we pass pointers to void to free. These can't be checked, as we don't know what type of object the pointer really points to. We could forbid that and fix all callers to pass a typed pointer, but that seems a bit too much to chew for the moment. So for now, simply accept that freeing pointers to void won't be checked. It's a best effort. As an example, if I add an explicit constructor to type ctf_trace (in src/bin/lttng-relayd/ctf-trace.h), I get the following errors with clang. For the allocation: /home/simark/src/lttng-tools/src/common/macros.h:57:2: error: static_assert failed due to requirement 'std::is_pod::value' "type is POD" static_assert (std::is_pod::value, "type is POD"); ^ ~~~~~~~~~~~~~~~~~~~~~ /home/simark/src/lttng-tools/src/bin/lttng-relayd/ctf-trace.cpp:84:10: note: in instantiation of function template specialization 'zmalloc' requested here trace = zmalloc(); ^ For the de-allocation: /home/simark/src/lttng-tools/src/bin/lttng-relayd/ctf-trace.cpp:29:2: error: call to deleted function 'free' free(trace); ^~~~ /home/simark/src/lttng-tools/src/common/macros.h:125:6: note: candidate function [with T = ctf_trace, $1 = void] has been explicitly deleted void free(T *p) = delete; ^ /usr/include/stdlib.h:565:13: note: candidate function extern void free (void *__ptr) __THROW; ^ Change-Id: I246a9113d08fa36b81a49137f4e80a5e808de913 Signed-off-by: Simon Marchi Signed-off-by: Jérémie Galarneau --- src/bin/lttng-crash/lttng-crash.cpp | 4 +- .../backward-compatibility-group-by.cpp | 2 +- src/bin/lttng-relayd/connection.cpp | 2 +- src/bin/lttng-relayd/ctf-trace.cpp | 2 +- src/bin/lttng-relayd/index.cpp | 2 +- src/bin/lttng-relayd/live.cpp | 6 +- src/bin/lttng-relayd/session.cpp | 2 +- .../lttng-relayd/sessiond-trace-chunks.cpp | 4 +- src/bin/lttng-relayd/stream.cpp | 2 +- src/bin/lttng-relayd/tracefile-array.cpp | 4 +- src/bin/lttng-relayd/viewer-session.cpp | 2 +- src/bin/lttng-relayd/viewer-stream.cpp | 2 +- src/bin/lttng-sessiond/action-executor.cpp | 4 +- src/bin/lttng-sessiond/agent-thread.cpp | 2 +- src/bin/lttng-sessiond/agent.cpp | 16 +-- src/bin/lttng-sessiond/buffer-registry.cpp | 12 +- src/bin/lttng-sessiond/channel.cpp | 4 +- src/bin/lttng-sessiond/clear.cpp | 2 +- src/bin/lttng-sessiond/client.cpp | 38 +++--- src/bin/lttng-sessiond/cmd.cpp | 14 ++- src/bin/lttng-sessiond/consumer.cpp | 6 +- src/bin/lttng-sessiond/dispatch.cpp | 4 +- .../event-notifier-error-accounting.cpp | 10 +- src/bin/lttng-sessiond/health.cpp | 2 +- src/bin/lttng-sessiond/kernel.cpp | 2 +- src/bin/lttng-sessiond/lttng-syscall.cpp | 6 +- src/bin/lttng-sessiond/manage-apps.cpp | 2 +- src/bin/lttng-sessiond/manage-consumer.cpp | 4 +- src/bin/lttng-sessiond/manage-kernel.cpp | 2 +- src/bin/lttng-sessiond/modprobe.cpp | 8 +- .../notification-thread-commands.cpp | 4 +- .../notification-thread-events.cpp | 34 ++--- .../lttng-sessiond/notification-thread.cpp | 4 +- src/bin/lttng-sessiond/notify-apps.cpp | 2 +- src/bin/lttng-sessiond/register.cpp | 4 +- src/bin/lttng-sessiond/rotation-thread.cpp | 6 +- src/bin/lttng-sessiond/save.cpp | 2 +- src/bin/lttng-sessiond/session.cpp | 2 +- src/bin/lttng-sessiond/snapshot.cpp | 2 +- src/bin/lttng-sessiond/thread.cpp | 2 +- src/bin/lttng-sessiond/trace-kernel.cpp | 24 ++-- src/bin/lttng-sessiond/trace-ust.cpp | 10 +- src/bin/lttng-sessiond/tracker.cpp | 4 +- src/bin/lttng-sessiond/ust-app.cpp | 34 ++--- src/bin/lttng-sessiond/ust-registry.cpp | 8 +- src/bin/lttng/commands/add_context.cpp | 10 +- src/bin/lttng/commands/enable_events.cpp | 2 +- src/bin/lttng/commands/list.cpp | 3 +- src/bin/lttng/conf.cpp | 2 +- src/bin/lttng/uprobe.cpp | 4 +- src/common/actions/list.cpp | 2 +- src/common/actions/notify.cpp | 2 +- src/common/actions/path.cpp | 2 +- src/common/actions/rate-policy.cpp | 4 +- src/common/actions/rotate-session.cpp | 18 +-- src/common/actions/snapshot-session.cpp | 18 +-- src/common/actions/start-session.cpp | 18 +-- src/common/actions/stop-session.cpp | 17 +-- src/common/argpar-utils/argpar-utils.hpp | 8 +- src/common/bytecode/bytecode.cpp | 5 +- src/common/channel.cpp | 11 +- src/common/compat/directory-handle.cpp | 6 +- src/common/compat/poll.cpp | 14 +-- src/common/compat/string.hpp | 2 +- src/common/conditions/buffer-usage.cpp | 4 +- src/common/conditions/event-rule-matches.cpp | 6 +- .../conditions/session-consumed-size.cpp | 4 +- src/common/conditions/session-rotation.cpp | 4 +- src/common/config/session-config.cpp | 9 +- .../consumer/consumer-metadata-cache.cpp | 3 +- src/common/consumer/consumer-stream.cpp | 2 +- src/common/consumer/consumer.cpp | 16 +-- src/common/consumer/metadata-bucket.cpp | 4 +- src/common/context.cpp | 4 +- src/common/error-query.cpp | 10 +- src/common/event-expr/event-expr.cpp | 2 +- src/common/event-field-value.cpp | 2 +- src/common/event-rule/jul-logging.cpp | 4 +- src/common/event-rule/kernel-kprobe.cpp | 2 +- src/common/event-rule/kernel-syscall.cpp | 2 +- src/common/event-rule/kernel-tracepoint.cpp | 2 +- src/common/event-rule/kernel-uprobe.cpp | 2 +- src/common/event-rule/log4j-logging.cpp | 4 +- src/common/event-rule/python-logging.cpp | 4 +- src/common/event-rule/user-tracepoint.cpp | 5 +- src/common/event.cpp | 28 ++--- src/common/fd-handle.cpp | 2 +- src/common/fd-tracker/fd-tracker.cpp | 10 +- src/common/fd-tracker/inode.cpp | 6 +- src/common/filter.cpp | 2 +- src/common/filter/filter-parser.ypp | 10 +- .../filter/filter-visitor-generate-ir.cpp | 28 ++--- src/common/hashtable/hashtable.cpp | 2 +- src/common/health/health.cpp | 4 +- src/common/index-allocator.cpp | 4 +- src/common/index/index.cpp | 2 +- src/common/ini-config/ini-config.cpp | 2 +- src/common/ini-config/ini.cpp | 2 +- src/common/kernel-ctl/kernel-ctl.cpp | 6 +- src/common/kernel-probe.cpp | 4 +- src/common/location.cpp | 2 +- src/common/log-level-rule.cpp | 6 +- src/common/lttng-elf.cpp | 8 +- src/common/macros.hpp | 118 +++++++++++++++--- src/common/mi-lttng.cpp | 2 +- src/common/notification.cpp | 2 +- src/common/path.cpp | 6 +- src/common/pipe.cpp | 2 +- src/common/relayd/relayd.cpp | 6 +- src/common/runas.cpp | 8 +- src/common/session-descriptor.cpp | 14 +-- src/common/sessiond-comm/sessiond-comm.cpp | 6 +- src/common/spawn-viewer.cpp | 9 +- src/common/string-utils/string-utils.cpp | 8 +- src/common/trace-chunk.cpp | 10 +- src/common/tracker.cpp | 6 +- src/common/trigger.cpp | 4 +- src/common/uri.cpp | 4 +- src/common/userspace-probe.cpp | 12 +- src/common/ust-consumer/ust-consumer.cpp | 4 +- src/common/utils.cpp | 15 ++- src/lib/lttng-ctl/channel.cpp | 8 +- src/lib/lttng-ctl/clear.cpp | 2 +- src/lib/lttng-ctl/destruction-handle.cpp | 2 +- src/lib/lttng-ctl/event.cpp | 4 +- src/lib/lttng-ctl/load.cpp | 18 ++- src/lib/lttng-ctl/lttng-ctl-health.cpp | 2 +- src/lib/lttng-ctl/lttng-ctl.cpp | 12 +- src/lib/lttng-ctl/rotate.cpp | 8 +- src/lib/lttng-ctl/save.cpp | 2 +- src/lib/lttng-ctl/snapshot.cpp | 4 +- src/lib/lttng-ctl/tracker.cpp | 4 +- tests/regression/tools/live/live_test.cpp | 7 +- tests/unit/test_ust_data.cpp | 6 +- tests/unit/test_utils_expand_path.cpp | 4 +- 135 files changed, 539 insertions(+), 465 deletions(-) diff --git a/src/bin/lttng-crash/lttng-crash.cpp b/src/bin/lttng-crash/lttng-crash.cpp index 0b4afc91b..f63ececc0 100644 --- a/src/bin/lttng-crash/lttng-crash.cpp +++ b/src/bin/lttng-crash/lttng-crash.cpp @@ -781,7 +781,7 @@ int copy_crash_data(const struct lttng_crash_layout *layout, int fd_dest, return ret; } src_file_len = layout->mmap_length; - buf = (char *) zmalloc(src_file_len); + buf = calloc(src_file_len); if (!buf) { return -1; } @@ -1129,7 +1129,7 @@ int delete_dir_recursive(const char *path) } if (S_ISDIR(st.st_mode)) { - char *subpath = (char *) zmalloc(PATH_MAX); + char *subpath = calloc(PATH_MAX); if (!subpath) { PERROR("zmalloc path"); diff --git a/src/bin/lttng-relayd/backward-compatibility-group-by.cpp b/src/bin/lttng-relayd/backward-compatibility-group-by.cpp index e1d289d98..78f8a1971 100644 --- a/src/bin/lttng-relayd/backward-compatibility-group-by.cpp +++ b/src/bin/lttng-relayd/backward-compatibility-group-by.cpp @@ -172,7 +172,7 @@ char *backward_compat_group_by_session(const char *path, goto error_regex; } } else { - datetime = (char *) zmalloc(DATETIME_STR_LEN); + datetime = calloc(DATETIME_STR_LEN); if (!datetime) { PERROR("Failed to allocate DATETIME string"); goto error; diff --git a/src/bin/lttng-relayd/connection.cpp b/src/bin/lttng-relayd/connection.cpp index c1e58aba3..fb53862a4 100644 --- a/src/bin/lttng-relayd/connection.cpp +++ b/src/bin/lttng-relayd/connection.cpp @@ -90,7 +90,7 @@ struct relay_connection *connection_create(struct lttcomm_sock *sock, { struct relay_connection *conn; - conn = (relay_connection *) zmalloc(sizeof(*conn)); + conn = zmalloc(); if (!conn) { PERROR("zmalloc relay connection"); goto end; diff --git a/src/bin/lttng-relayd/ctf-trace.cpp b/src/bin/lttng-relayd/ctf-trace.cpp index 4ee745a10..cad67b78e 100644 --- a/src/bin/lttng-relayd/ctf-trace.cpp +++ b/src/bin/lttng-relayd/ctf-trace.cpp @@ -97,7 +97,7 @@ static struct ctf_trace *ctf_trace_create(struct relay_session *session, { struct ctf_trace *trace; - trace = (ctf_trace *) zmalloc(sizeof(*trace)); + trace = zmalloc(); if (!trace) { PERROR("Failed to allocate ctf_trace"); goto end; diff --git a/src/bin/lttng-relayd/index.cpp b/src/bin/lttng-relayd/index.cpp index 651b36715..e7846a5b9 100644 --- a/src/bin/lttng-relayd/index.cpp +++ b/src/bin/lttng-relayd/index.cpp @@ -34,7 +34,7 @@ static struct relay_index *relay_index_create(struct relay_stream *stream, DBG2("Creating relay index for stream id %" PRIu64 " and seqnum %" PRIu64, stream->stream_handle, net_seq_num); - index = (relay_index *) zmalloc(sizeof(*index)); + index = zmalloc(); if (!index) { PERROR("Relay index zmalloc"); goto end; diff --git a/src/bin/lttng-relayd/live.cpp b/src/bin/lttng-relayd/live.cpp index 64c194fdc..ef830d4ba 100644 --- a/src/bin/lttng-relayd/live.cpp +++ b/src/bin/lttng-relayd/live.cpp @@ -1112,7 +1112,7 @@ int viewer_list_sessions(struct relay_connection *conn) uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT; uint32_t count = 0; - send_session_buf = (lttng_viewer_session *) zmalloc(SESSION_BUF_DEFAULT_COUNT * sizeof(*send_session_buf)); + send_session_buf = calloc(SESSION_BUF_DEFAULT_COUNT); if (!send_session_buf) { return -1; } @@ -2115,7 +2115,7 @@ int viewer_get_packet(struct relay_connection *conn) reply_size += packet_data_len; } - reply = (char *) zmalloc(reply_size); + reply = zmalloc(reply_size); if (!reply) { get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR; PERROR("Falled to allocate reply, returning status=%s", @@ -2378,7 +2378,7 @@ int viewer_get_metadata(struct relay_connection *conn) } reply.len = htobe64(len); - data = (char *) zmalloc(len); + data = zmalloc(len); if (!data) { PERROR("viewer metadata zmalloc"); goto error; diff --git a/src/bin/lttng-relayd/session.cpp b/src/bin/lttng-relayd/session.cpp index abefc0d27..a8a152069 100644 --- a/src/bin/lttng-relayd/session.cpp +++ b/src/bin/lttng-relayd/session.cpp @@ -310,7 +310,7 @@ struct relay_session *session_create(const char *session_name, goto error; } - session = (relay_session *) zmalloc(sizeof(*session)); + session = zmalloc(); if (!session) { PERROR("Failed to allocate session"); goto error; diff --git a/src/bin/lttng-relayd/sessiond-trace-chunks.cpp b/src/bin/lttng-relayd/sessiond-trace-chunks.cpp index 0a5c44825..68a5e454f 100644 --- a/src/bin/lttng-relayd/sessiond-trace-chunks.cpp +++ b/src/bin/lttng-relayd/sessiond-trace-chunks.cpp @@ -197,7 +197,7 @@ int trace_chunk_registry_ht_element_create( goto end; } - new_element = (trace_chunk_registry_ht_element *) zmalloc(sizeof(*new_element)); + new_element = zmalloc(); if (!new_element) { ret = -1; goto end; @@ -263,7 +263,7 @@ end: struct sessiond_trace_chunk_registry *sessiond_trace_chunk_registry_create(void) { struct sessiond_trace_chunk_registry *sessiond_registry = - (sessiond_trace_chunk_registry *) zmalloc(sizeof(*sessiond_registry)); + zmalloc(); if (!sessiond_registry) { goto end; diff --git a/src/bin/lttng-relayd/stream.cpp b/src/bin/lttng-relayd/stream.cpp index aba160bfa..9f7bfcb15 100644 --- a/src/bin/lttng-relayd/stream.cpp +++ b/src/bin/lttng-relayd/stream.cpp @@ -580,7 +580,7 @@ struct relay_stream *stream_create(struct ctf_trace *trace, bool acquired_reference = false; struct lttng_trace_chunk *current_trace_chunk; - stream = (relay_stream *) zmalloc(sizeof(struct relay_stream)); + stream = zmalloc(); if (stream == NULL) { PERROR("relay stream zmalloc"); goto error_no_alloc; diff --git a/src/bin/lttng-relayd/tracefile-array.cpp b/src/bin/lttng-relayd/tracefile-array.cpp index 8b1b533d5..73a288794 100644 --- a/src/bin/lttng-relayd/tracefile-array.cpp +++ b/src/bin/lttng-relayd/tracefile-array.cpp @@ -17,11 +17,11 @@ struct tracefile_array *tracefile_array_create(size_t count) struct tracefile_array *tfa = NULL; int i; - tfa = (tracefile_array *) zmalloc(sizeof(*tfa)); + tfa = zmalloc(); if (!tfa) { goto error; } - tfa->tf = (tracefile *) zmalloc(sizeof(*tfa->tf) * count); + tfa->tf = calloc(count); if (!tfa->tf) { goto error; } diff --git a/src/bin/lttng-relayd/viewer-session.cpp b/src/bin/lttng-relayd/viewer-session.cpp index 4529a21fa..194073f59 100644 --- a/src/bin/lttng-relayd/viewer-session.cpp +++ b/src/bin/lttng-relayd/viewer-session.cpp @@ -22,7 +22,7 @@ struct relay_viewer_session *viewer_session_create(void) { struct relay_viewer_session *vsession; - vsession = (relay_viewer_session *) zmalloc(sizeof(*vsession)); + vsession = zmalloc(); if (!vsession) { goto end; } diff --git a/src/bin/lttng-relayd/viewer-stream.cpp b/src/bin/lttng-relayd/viewer-stream.cpp index fa447d45b..2790903ad 100644 --- a/src/bin/lttng-relayd/viewer-stream.cpp +++ b/src/bin/lttng-relayd/viewer-stream.cpp @@ -62,7 +62,7 @@ struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream, ASSERT_LOCKED(stream->lock); - vstream = (relay_viewer_stream *) zmalloc(sizeof(*vstream)); + vstream = zmalloc(); if (!vstream) { PERROR("relay viewer stream zmalloc"); goto error; diff --git a/src/bin/lttng-sessiond/action-executor.cpp b/src/bin/lttng-sessiond/action-executor.cpp index f43716c63..cc481e5b7 100644 --- a/src/bin/lttng-sessiond/action-executor.cpp +++ b/src/bin/lttng-sessiond/action-executor.cpp @@ -821,7 +821,7 @@ static void clean_up_action_executor_thread(void *_data) struct action_executor *action_executor_create( struct notification_thread_handle *handle) { - struct action_executor *executor = (action_executor *) zmalloc(sizeof(*executor)); + struct action_executor *executor = zmalloc(); if (!executor) { goto end; @@ -893,7 +893,7 @@ enum action_executor_status action_executor_enqueue_trigger( goto error_unlock; } - work_item = (action_work_item *) zmalloc(sizeof(*work_item)); + work_item = zmalloc(); if (!work_item) { PERROR("Failed to allocate action executor work item: trigger name = `%s`", get_trigger_name(trigger)); diff --git a/src/bin/lttng-sessiond/agent-thread.cpp b/src/bin/lttng-sessiond/agent-thread.cpp index 77470b3ee..704f21d65 100644 --- a/src/bin/lttng-sessiond/agent-thread.cpp +++ b/src/bin/lttng-sessiond/agent-thread.cpp @@ -570,7 +570,7 @@ bool launch_agent_management_thread(void) struct thread_notifiers *notifiers; struct lttng_thread *thread; - notifiers = (thread_notifiers *) zmalloc(sizeof(*notifiers)); + notifiers = zmalloc(); if (!notifiers) { goto error_alloc; } diff --git a/src/bin/lttng-sessiond/agent.cpp b/src/bin/lttng-sessiond/agent.cpp index ed8a8d126..087852356 100644 --- a/src/bin/lttng-sessiond/agent.cpp +++ b/src/bin/lttng-sessiond/agent.cpp @@ -348,7 +348,7 @@ static ssize_t list_events(struct agent_app *app, struct lttng_event **events) goto error; } - reply = (lttcomm_agent_list_reply *) zmalloc(data_size); + reply = zmalloc(data_size); if (!reply) { ret = LTTNG_ERR_NOMEM; goto error; @@ -361,7 +361,7 @@ static ssize_t list_events(struct agent_app *app, struct lttng_event **events) } nb_event = be32toh(reply->nb_event); - tmp_events = (lttng_event *) zmalloc(sizeof(*tmp_events) * nb_event); + tmp_events = calloc(nb_event); if (!tmp_events) { ret = LTTNG_ERR_NOMEM; goto error; @@ -441,7 +441,7 @@ static int enable_event(const struct agent_app *app, struct agent_event *event) goto error_io; } - bytes_to_send = (char *) zmalloc(data_size); + bytes_to_send = calloc(data_size); if (!bytes_to_send) { ret = LTTNG_ERR_NOMEM; goto error; @@ -726,7 +726,7 @@ struct agent_app_ctx *create_app_ctx(const struct lttng_event_context *ctx) } LTTNG_ASSERT(ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT); - agent_ctx = (agent_app_ctx *) zmalloc(sizeof(*ctx)); + agent_ctx = zmalloc(); if (!agent_ctx) { goto end; } @@ -898,7 +898,7 @@ int agent_list_events(struct lttng_event **events, DBG2("Agent listing events for domain %d", domain); nbmem = UST_APP_EVENT_LIST_SIZE; - tmp_events = (lttng_event *) zmalloc(nbmem * sizeof(*tmp_events)); + tmp_events = calloc(nbmem); if (!tmp_events) { PERROR("zmalloc agent list events"); ret = -ENOMEM; @@ -974,7 +974,7 @@ struct agent_app *agent_create_app(pid_t pid, enum lttng_domain_type domain, LTTNG_ASSERT(sock); - app = (agent_app *) zmalloc(sizeof(*app)); + app = zmalloc(); if (!app) { PERROR("Failed to allocate agent application instance"); goto error; @@ -1117,7 +1117,7 @@ struct agent *agent_create(enum lttng_domain_type domain) int ret; struct agent *agt; - agt = (agent *) zmalloc(sizeof(struct agent)); + agt = zmalloc(); if (!agt) { goto error; } @@ -1156,7 +1156,7 @@ struct agent_event *agent_create_event(const char *name, goto error; } - event = (agent_event *) zmalloc(sizeof(*event)); + event = zmalloc(); if (!event) { goto error; } diff --git a/src/bin/lttng-sessiond/buffer-registry.cpp b/src/bin/lttng-sessiond/buffer-registry.cpp index ca7830a38..7a446633b 100644 --- a/src/bin/lttng-sessiond/buffer-registry.cpp +++ b/src/bin/lttng-sessiond/buffer-registry.cpp @@ -104,14 +104,14 @@ int buffer_reg_uid_create(uint64_t session_id, uint32_t bits_per_long, uid_t uid LTTNG_ASSERT(regp); - reg = (buffer_reg_uid *) zmalloc(sizeof(*reg)); + reg = zmalloc(); if (!reg) { PERROR("zmalloc buffer registry uid"); ret = -ENOMEM; goto error; } - reg->registry = (buffer_reg_session *) zmalloc(sizeof(struct buffer_reg_session)); + reg->registry = zmalloc(); if (!reg->registry) { PERROR("zmalloc buffer registry uid session"); ret = -ENOMEM; @@ -234,14 +234,14 @@ int buffer_reg_pid_create(uint64_t session_id, struct buffer_reg_pid **regp, LTTNG_ASSERT(regp); - reg = (buffer_reg_pid *) zmalloc(sizeof(*reg)); + reg = zmalloc(); if (!reg) { PERROR("zmalloc buffer registry pid"); ret = -ENOMEM; goto error; } - reg->registry = (buffer_reg_session *) zmalloc(sizeof(struct buffer_reg_session)); + reg->registry = zmalloc(); if (!reg->registry) { PERROR("zmalloc buffer registry pid session"); ret = -ENOMEM; @@ -371,7 +371,7 @@ int buffer_reg_channel_create(uint64_t key, struct buffer_reg_channel **regp) DBG3("Buffer registry channel create with key: %" PRIu64, key); - reg = (buffer_reg_channel *) zmalloc(sizeof(*reg)); + reg = zmalloc(); if (!reg) { PERROR("zmalloc buffer registry channel"); return -ENOMEM; @@ -401,7 +401,7 @@ int buffer_reg_stream_create(struct buffer_reg_stream **regp) DBG3("Buffer registry creating stream"); - reg = (buffer_reg_stream *) zmalloc(sizeof(*reg)); + reg = zmalloc(); if (!reg) { PERROR("zmalloc buffer registry stream"); return -ENOMEM; diff --git a/src/bin/lttng-sessiond/channel.cpp b/src/bin/lttng-sessiond/channel.cpp index e3bb232fa..540fc9734 100644 --- a/src/bin/lttng-sessiond/channel.cpp +++ b/src/bin/lttng-sessiond/channel.cpp @@ -34,13 +34,13 @@ struct lttng_channel *channel_new_default_attr(int dom, const char *channel_name = DEFAULT_CHANNEL_NAME; struct lttng_channel_extended *extended_attr = NULL; - chan = (lttng_channel *) zmalloc(sizeof(struct lttng_channel)); + chan = zmalloc(); if (chan == NULL) { PERROR("zmalloc channel init"); goto error_alloc; } - extended_attr = (lttng_channel_extended *) zmalloc(sizeof(struct lttng_channel_extended)); + extended_attr = zmalloc(); if (!extended_attr) { PERROR("zmalloc channel extended init"); goto error; diff --git a/src/bin/lttng-sessiond/clear.cpp b/src/bin/lttng-sessiond/clear.cpp index fcac76ffa..04f2a368e 100644 --- a/src/bin/lttng-sessiond/clear.cpp +++ b/src/bin/lttng-sessiond/clear.cpp @@ -67,7 +67,7 @@ int cmd_clear_session(struct ltt_session *session, int *sock_fd) usess = session->ust_session; if (sock_fd) { - reply_context = (cmd_clear_session_reply_context *) zmalloc(sizeof(*reply_context)); + reply_context = zmalloc(); if (!reply_context) { ret = LTTNG_ERR_NOMEM; goto end; diff --git a/src/bin/lttng-sessiond/client.cpp b/src/bin/lttng-sessiond/client.cpp index cf3fd4424..37ea0509a 100644 --- a/src/bin/lttng-sessiond/client.cpp +++ b/src/bin/lttng-sessiond/client.cpp @@ -264,7 +264,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data) tmp = ""; } tmplen = strlen(the_config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp); - tmpnew = (char *) zmalloc(tmplen + 1 /* \0 */); + tmpnew = zmalloc(tmplen + 1 /* \0 */); if (!tmpnew) { ret = -ENOMEM; goto error; @@ -306,7 +306,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data) tmp = ""; } tmplen = strlen(the_config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp); - tmpnew = (char *) zmalloc(tmplen + 1 /* \0 */); + tmpnew = zmalloc(tmplen + 1 /* \0 */); if (!tmpnew) { ret = -ENOMEM; goto error; @@ -1751,7 +1751,7 @@ skip_domain: goto error; } - uris = (lttng_uri *) zmalloc(len); + uris = calloc(nb_uri); if (uris == NULL) { ret = LTTNG_ERR_FATAL; goto error; @@ -1892,31 +1892,35 @@ skip_domain: case LTTNG_LIST_SESSIONS: { unsigned int nr_sessions; - lttng_session *sessions_payload; - size_t payload_len; + lttng_session *sessions_payload = nullptr; + size_t payload_len = 0; session_lock_list(); nr_sessions = lttng_sessions_count( LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); - payload_len = (sizeof(struct lttng_session) * nr_sessions) + - (sizeof(struct lttng_session_extended) * nr_sessions); - sessions_payload = (lttng_session *) zmalloc(payload_len); + if (nr_sessions > 0) { + payload_len = (sizeof(struct lttng_session) * + nr_sessions) + + (sizeof(struct lttng_session_extended) * + nr_sessions); + sessions_payload = zmalloc(payload_len); + if (!sessions_payload) { + session_unlock_list(); + ret = -ENOMEM; + goto setup_error; + } - if (!sessions_payload) { - session_unlock_list(); - ret = -ENOMEM; - goto setup_error; + cmd_list_lttng_sessions(sessions_payload, nr_sessions, + LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), + LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); } - cmd_list_lttng_sessions(sessions_payload, nr_sessions, - LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), - LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); session_unlock_list(); - ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload, - payload_len); + ret = setup_lttng_msg_no_cmd_header( + cmd_ctx, sessions_payload, payload_len); free(sessions_payload); if (ret < 0) { diff --git a/src/bin/lttng-sessiond/cmd.cpp b/src/bin/lttng-sessiond/cmd.cpp index 53a175372..aea781671 100644 --- a/src/bin/lttng-sessiond/cmd.cpp +++ b/src/bin/lttng-sessiond/cmd.cpp @@ -2140,7 +2140,7 @@ static int _cmd_enable_event(struct ltt_session *session, } } if (filter) { - filter_a = (lttng_bytecode *) zmalloc(sizeof(*filter_a) + filter->len); + filter_a = zmalloc(sizeof(*filter_a) + filter->len); if (!filter_a) { free(filter_expression_a); ret = LTTNG_ERR_FATAL; @@ -2368,7 +2368,7 @@ static int _cmd_enable_event(struct ltt_session *session, struct lttng_bytecode) + filter->len; - filter_copy = (lttng_bytecode *) zmalloc(filter_size); + filter_copy = zmalloc(filter_size); if (!filter_copy) { ret = LTTNG_ERR_NOMEM; goto error; @@ -3380,11 +3380,12 @@ int cmd_destroy_session(struct ltt_session *session, struct cmd_destroy_session_reply_context *reply_context = NULL; if (sock_fd) { - reply_context = (cmd_destroy_session_reply_context *) zmalloc(sizeof(*reply_context)); + reply_context = zmalloc(); if (!reply_context) { ret = LTTNG_ERR_NOMEM; goto end; } + reply_context->reply_sock_fd = *sock_fd; } @@ -3583,12 +3584,13 @@ int cmd_register_consumer(struct ltt_session *session, goto error; } - socket->lock = (pthread_mutex_t *) zmalloc(sizeof(pthread_mutex_t)); + socket->lock = zmalloc(); if (socket->lock == NULL) { PERROR("zmalloc pthread mutex"); ret = LTTNG_ERR_FATAL; goto error; } + pthread_mutex_init(socket->lock, NULL); socket->registered = 1; @@ -3651,7 +3653,7 @@ ssize_t cmd_list_domains(struct ltt_session *session, goto end; } - *domains = (lttng_domain *) zmalloc(nb_dom * sizeof(struct lttng_domain)); + *domains = calloc(nb_dom); if (*domains == NULL) { ret = LTTNG_ERR_FATAL; goto error; @@ -4193,7 +4195,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, goto end; } - list = (lttng_snapshot_output *) zmalloc(session->snapshot.nb_output * sizeof(*list)); + list = calloc(session->snapshot.nb_output); if (!list) { ret = -LTTNG_ERR_NOMEM; goto end; diff --git a/src/bin/lttng-sessiond/consumer.cpp b/src/bin/lttng-sessiond/consumer.cpp index 238da0508..bdda39135 100644 --- a/src/bin/lttng-sessiond/consumer.cpp +++ b/src/bin/lttng-sessiond/consumer.cpp @@ -49,7 +49,7 @@ char *setup_channel_trace_path(struct consumer_output *consumer, * Allocate the string ourself to make sure we never exceed * LTTNG_PATH_MAX. */ - pathname = (char *) zmalloc(LTTNG_PATH_MAX); + pathname = calloc(LTTNG_PATH_MAX); if (!pathname) { goto error; } @@ -429,7 +429,7 @@ struct consumer_socket *consumer_allocate_socket(int *fd) LTTNG_ASSERT(fd); - socket = (consumer_socket *) zmalloc(sizeof(struct consumer_socket)); + socket = zmalloc(); if (socket == NULL) { PERROR("zmalloc consumer socket"); goto error; @@ -520,7 +520,7 @@ struct consumer_output *consumer_create_output(enum consumer_dst_type type) { struct consumer_output *output = NULL; - output = (consumer_output *) zmalloc(sizeof(struct consumer_output)); + output = zmalloc(); if (output == NULL) { PERROR("zmalloc consumer_output"); goto error; diff --git a/src/bin/lttng-sessiond/dispatch.cpp b/src/bin/lttng-sessiond/dispatch.cpp index 7988f1ba8..89d7c7c2e 100644 --- a/src/bin/lttng-sessiond/dispatch.cpp +++ b/src/bin/lttng-sessiond/dispatch.cpp @@ -292,7 +292,7 @@ static void *thread_dispatch_ust_registration(void *data) ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); if (ust_cmd->reg_msg.type == LTTNG_UST_CTL_SOCKET_CMD) { - wait_node = (ust_reg_wait_node *) zmalloc(sizeof(*wait_node)); + wait_node = zmalloc(); if (!wait_node) { PERROR("zmalloc wait_node dispatch"); ret = close(ust_cmd->sock); @@ -510,7 +510,7 @@ bool launch_ust_dispatch_thread(struct ust_cmd_queue *cmd_queue, struct lttng_thread *thread; struct thread_notifiers *notifiers; - notifiers = (thread_notifiers *) zmalloc(sizeof(*notifiers)); + notifiers = zmalloc(); if (!notifiers) { goto error; } diff --git a/src/bin/lttng-sessiond/event-notifier-error-accounting.cpp b/src/bin/lttng-sessiond/event-notifier-error-accounting.cpp index 10d69493b..d42f5802d 100644 --- a/src/bin/lttng-sessiond/event-notifier-error-accounting.cpp +++ b/src/bin/lttng-sessiond/event-notifier-error-accounting.cpp @@ -387,7 +387,7 @@ struct ust_error_accounting_entry *ust_error_accounting_entry_create( goto error; } - entry = (ust_error_accounting_entry *) zmalloc(sizeof(struct ust_error_accounting_entry)); + entry = zmalloc(); if (!entry) { PERROR("Failed to allocate event notifier error acounting entry") goto error; @@ -397,7 +397,7 @@ struct ust_error_accounting_entry *ust_error_accounting_entry_create( entry->uid = app->uid; entry->nr_counter_cpu_fds = lttng_ust_ctl_get_nr_cpu_per_counter(); - cpu_counter_fds = (int *) zmalloc(entry->nr_counter_cpu_fds * sizeof(*cpu_counter_fds)); + cpu_counter_fds = calloc(entry->nr_counter_cpu_fds); if (!cpu_counter_fds) { PERROR("Failed to allocate event notifier error counter file descriptors array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu", (int) app->uid, app->name, (int) app->pid, @@ -410,7 +410,7 @@ struct ust_error_accounting_entry *ust_error_accounting_entry_create( cpu_counter_fds[i] = -1; } - cpu_counters = (lttng_ust_abi_object_data **) zmalloc(entry->nr_counter_cpu_fds * sizeof(struct lttng_ust_abi_object_data *)); + cpu_counters = calloc(entry->nr_counter_cpu_fds); if (!cpu_counters) { PERROR("Failed to allocate event notifier error counter lttng_ust_abi_object_data array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu", (int) app->uid, app->name, (int) app->pid, @@ -653,7 +653,7 @@ event_notifier_error_accounting_register_app(struct ust_app *app) goto error_send_counter_data; } - cpu_counters = (lttng_ust_abi_object_data **) zmalloc(entry->nr_counter_cpu_fds * sizeof(struct lttng_ust_abi_object_data *)); + cpu_counters = calloc(entry->nr_counter_cpu_fds); if (!cpu_counters) { PERROR("Failed to allocate event notifier error counter lttng_ust_abi_object_data array: application uid = %d, application name = '%s', pid = %d, allocation size = %zu", (int) app->uid, app->name, (int) app->pid, @@ -1050,7 +1050,7 @@ enum event_notifier_error_accounting_status create_error_counter_index_for_token goto end; } - index_entry = (index_ht_entry *) zmalloc(sizeof(*index_entry)); + index_entry = zmalloc(); if (index_entry == NULL) { PERROR("Failed to allocate event notifier error counter hash table entry"); status = EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NOMEM; diff --git a/src/bin/lttng-sessiond/health.cpp b/src/bin/lttng-sessiond/health.cpp index a7f4bdcbb..9a4dee861 100644 --- a/src/bin/lttng-sessiond/health.cpp +++ b/src/bin/lttng-sessiond/health.cpp @@ -255,7 +255,7 @@ bool launch_health_management_thread(void) struct thread_notifiers *notifiers; struct lttng_thread *thread; - notifiers = (thread_notifiers *) zmalloc(sizeof(*notifiers)); + notifiers = zmalloc(); if (!notifiers) { goto error_alloc; } diff --git a/src/bin/lttng-sessiond/kernel.cpp b/src/bin/lttng-sessiond/kernel.cpp index 9d256e207..f7d232ffc 100644 --- a/src/bin/lttng-sessiond/kernel.cpp +++ b/src/bin/lttng-sessiond/kernel.cpp @@ -1436,7 +1436,7 @@ ssize_t kernel_list_events(struct lttng_event **events) * See kernel-ctl.h for explanation of this value */ nbmem = KERNEL_EVENT_INIT_LIST_SIZE; - elist = (lttng_event *) zmalloc(sizeof(struct lttng_event) * nbmem); + elist = calloc(nbmem); if (elist == NULL) { PERROR("alloc list events"); count = -ENOMEM; diff --git a/src/bin/lttng-sessiond/lttng-syscall.cpp b/src/bin/lttng-sessiond/lttng-syscall.cpp index d7f6327ae..a3030bcf4 100644 --- a/src/bin/lttng-sessiond/lttng-syscall.cpp +++ b/src/bin/lttng-sessiond/lttng-syscall.cpp @@ -60,7 +60,7 @@ int syscall_init_table(int tracer_fd) } nbmem = SYSCALL_TABLE_INIT_SIZE; - syscall_table = (struct syscall *) zmalloc(sizeof(struct syscall) * nbmem); + syscall_table = calloc(nbmem); if (!syscall_table) { ret = -errno; PERROR("syscall list zmalloc"); @@ -245,7 +245,7 @@ static int add_syscall_to_ht(struct lttng_ht *ht, unsigned int index, LTTNG_ASSERT(ht); - ksyscall = (struct syscall *) zmalloc(sizeof(*ksyscall)); + ksyscall = zmalloc(); if (!ksyscall) { ret = -LTTNG_ERR_NOMEM; goto error; @@ -288,7 +288,7 @@ ssize_t syscall_table_list(struct lttng_event **_events) * them might not be valid. The count below will make sure to return the * right size of the events array. */ - events = (lttng_event *) zmalloc(syscall_table_nb_entry * sizeof(*events)); + events = calloc(syscall_table_nb_entry); if (!events) { PERROR("syscall table list zmalloc"); ret = -LTTNG_ERR_NOMEM; diff --git a/src/bin/lttng-sessiond/manage-apps.cpp b/src/bin/lttng-sessiond/manage-apps.cpp index 4ee3538b2..93d3b93cc 100644 --- a/src/bin/lttng-sessiond/manage-apps.cpp +++ b/src/bin/lttng-sessiond/manage-apps.cpp @@ -211,7 +211,7 @@ bool launch_application_management_thread(int apps_cmd_pipe_read_fd) struct thread_notifiers *notifiers = NULL; struct lttng_thread *thread; - notifiers = (thread_notifiers *) zmalloc(sizeof(*notifiers)); + notifiers = zmalloc(); if (!notifiers) { goto error_alloc; } diff --git a/src/bin/lttng-sessiond/manage-consumer.cpp b/src/bin/lttng-sessiond/manage-consumer.cpp index 4a3e3b4ab..47ce9d16a 100644 --- a/src/bin/lttng-sessiond/manage-consumer.cpp +++ b/src/bin/lttng-sessiond/manage-consumer.cpp @@ -193,7 +193,7 @@ static void *thread_consumer_management(void *data) consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; /* Create metadata socket lock. */ - consumer_data->metadata_sock.lock = (pthread_mutex_t *) zmalloc(sizeof(pthread_mutex_t)); + consumer_data->metadata_sock.lock = zmalloc(); if (consumer_data->metadata_sock.lock == NULL) { PERROR("zmalloc pthread mutex"); mark_thread_intialization_as_failed(notifiers); @@ -437,7 +437,7 @@ bool launch_consumer_management_thread(struct consumer_data *consumer_data) struct thread_notifiers *notifiers = NULL; struct lttng_thread *thread; - notifiers = (thread_notifiers *) zmalloc(sizeof(*notifiers)); + notifiers = zmalloc(); if (!notifiers) { goto error_alloc; } diff --git a/src/bin/lttng-sessiond/manage-kernel.cpp b/src/bin/lttng-sessiond/manage-kernel.cpp index a5d21b0fe..a6393b6c0 100644 --- a/src/bin/lttng-sessiond/manage-kernel.cpp +++ b/src/bin/lttng-sessiond/manage-kernel.cpp @@ -332,7 +332,7 @@ bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd) struct thread_notifiers *notifiers = NULL; struct lttng_thread *thread; - notifiers = (thread_notifiers *) zmalloc(sizeof(*notifiers)); + notifiers = zmalloc(); if (!notifiers) { goto error_alloc; } diff --git a/src/bin/lttng-sessiond/modprobe.cpp b/src/bin/lttng-sessiond/modprobe.cpp index 0dc123de1..90ed3ff14 100644 --- a/src/bin/lttng-sessiond/modprobe.cpp +++ b/src/bin/lttng-sessiond/modprobe.cpp @@ -666,7 +666,7 @@ static int grow_probes(void) /* Initialize capacity to 1 if 0. */ if (probes_capacity == 0) { - probes = (kern_modules_param *) zmalloc(sizeof(*probes)); + probes = zmalloc(); if (!probes) { PERROR("malloc probe list"); return -ENOMEM; @@ -679,7 +679,7 @@ static int grow_probes(void) /* Double size. */ probes_capacity *= 2; - tmp_probes = (kern_modules_param *) zmalloc(sizeof(*tmp_probes) * probes_capacity); + tmp_probes = calloc(probes_capacity); if (!tmp_probes) { PERROR("malloc probe list"); return -ENOMEM; @@ -741,7 +741,7 @@ static int append_list_to_probes(const char *list) name_len = strlen(next) + 13; cur_mod = &probes[nr_probes]; - cur_mod->name = (char *) zmalloc(name_len); + cur_mod->name = calloc(name_len); if (!cur_mod->name) { PERROR("malloc probe list"); ret = -ENOMEM; @@ -792,7 +792,7 @@ int modprobe_lttng_data(void) /* Default probes. */ int def_len = ARRAY_SIZE(kern_modules_probes_default); - probes = (kern_modules_param *) zmalloc(sizeof(*probes) * def_len); + probes = calloc(def_len); if (!probes) { PERROR("malloc probe list"); return -ENOMEM; diff --git a/src/bin/lttng-sessiond/notification-thread-commands.cpp b/src/bin/lttng-sessiond/notification-thread-commands.cpp index ce499eb1c..568580403 100644 --- a/src/bin/lttng-sessiond/notification-thread-commands.cpp +++ b/src/bin/lttng-sessiond/notification-thread-commands.cpp @@ -59,7 +59,7 @@ struct notification_thread_command *notification_thread_command_copy( { struct notification_thread_command *new_cmd; - new_cmd = (notification_thread_command *) zmalloc(sizeof(*new_cmd)); + new_cmd = zmalloc(); if (!new_cmd) { goto end; } @@ -425,7 +425,7 @@ struct lttng_event_notifier_notification *lttng_event_notifier_notification_crea LTTNG_ASSERT(domain != LTTNG_DOMAIN_NONE); LTTNG_ASSERT((payload && payload_size) || (!payload && !payload_size)); - notification = (lttng_event_notifier_notification *) zmalloc(sizeof(struct lttng_event_notifier_notification)); + notification = zmalloc(); if (notification == NULL) { ERR("Error allocating notification"); goto end; diff --git a/src/bin/lttng-sessiond/notification-thread-events.cpp b/src/bin/lttng-sessiond/notification-thread-events.cpp index 67fca2a35..48ba7a00b 100644 --- a/src/bin/lttng-sessiond/notification-thread-events.cpp +++ b/src/bin/lttng-sessiond/notification-thread-events.cpp @@ -569,7 +569,7 @@ struct session_info *session_info_create(const char *name, uid_t uid, gid_t gid, LTTNG_ASSERT(name); - session_info = (struct session_info *) zmalloc(sizeof(*session_info)); + session_info = zmalloc(); if (!session_info) { goto end; } @@ -623,7 +623,7 @@ struct channel_info *channel_info_create(const char *channel_name, struct channel_key *channel_key, uint64_t channel_capacity, struct session_info *session_info) { - struct channel_info *channel_info = (struct channel_info *) zmalloc(sizeof(*channel_info)); + struct channel_info *channel_info = zmalloc(); if (!channel_info) { goto end; @@ -723,7 +723,7 @@ struct notification_client_list *notification_client_list_create( struct cds_lfht_iter iter; struct notification_client_list *client_list; - client_list = (notification_client_list *) zmalloc(sizeof(*client_list)); + client_list = zmalloc(); if (!client_list) { PERROR("Failed to allocate notification client list"); goto end; @@ -755,7 +755,7 @@ struct notification_client_list *notification_client_list_create( continue; } - client_list_element = (notification_client_list_element *) zmalloc(sizeof(*client_list_element)); + client_list_element = zmalloc(); if (!client_list_element) { goto error_put_client_list; } @@ -1123,12 +1123,12 @@ int notification_thread_client_subscribe(struct notification_client *client, } } - condition_list_element = (lttng_condition_list_element *) zmalloc(sizeof(*condition_list_element)); + condition_list_element = zmalloc(); if (!condition_list_element) { ret = -1; goto error; } - client_list_element = (notification_client_list_element *) zmalloc(sizeof(*client_list_element)); + client_list_element = zmalloc(); if (!client_list_element) { ret = -1; goto error; @@ -1506,7 +1506,7 @@ struct lttng_session_trigger_list *lttng_session_trigger_list_create( { struct lttng_session_trigger_list *list; - list = (lttng_session_trigger_list *) zmalloc(sizeof(*list)); + list = zmalloc(); if (!list) { goto end; } @@ -1557,7 +1557,7 @@ int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, { int ret = 0; struct lttng_trigger_list_element *new_element = - (lttng_trigger_list_element *) zmalloc(sizeof(*new_element)); + zmalloc(); if (!new_element) { ret = -1; @@ -1755,7 +1755,7 @@ int handle_notification_thread_command_add_channel( continue; } - new_element = (lttng_trigger_list_element *) zmalloc(sizeof(*new_element)); + new_element = zmalloc(); if (!new_element) { rcu_read_unlock(); goto error; @@ -1769,7 +1769,7 @@ int handle_notification_thread_command_add_channel( DBG("Found %i triggers that apply to newly added channel", trigger_count); - channel_trigger_list = (lttng_channel_trigger_list *) zmalloc(sizeof(*channel_trigger_list)); + channel_trigger_list = zmalloc(); if (!channel_trigger_list) { goto error; } @@ -2032,7 +2032,7 @@ int handle_notification_thread_command_add_tracer_event_source( enum lttng_error_code cmd_result = LTTNG_OK; struct notification_event_tracer_event_source_element *element = NULL; - element = (notification_event_tracer_event_source_element *) zmalloc(sizeof(*element)); + element = zmalloc(); if (!element) { cmd_result = LTTNG_ERR_NOMEM; ret = -1; @@ -2501,7 +2501,7 @@ int bind_trigger_to_matching_channels(struct lttng_trigger *trigger, struct lttng_channel_trigger_list, channel_triggers_ht_node); - trigger_list_element = (lttng_trigger_list_element *) zmalloc(sizeof(*trigger_list_element)); + trigger_list_element = zmalloc(); if (!trigger_list_element) { ret = -1; goto end; @@ -2622,7 +2622,7 @@ enum lttng_error_code setup_tracer_notifier( struct lttng_condition *condition = lttng_trigger_get_condition(trigger); struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = NULL; - trigger_tokens_ht_element = (notification_trigger_tokens_ht_element *) zmalloc(sizeof(*trigger_tokens_ht_element)); + trigger_tokens_ht_element = zmalloc(); if (!trigger_tokens_ht_element) { ret = LTTNG_ERR_NOMEM; goto end; @@ -2745,7 +2745,7 @@ int handle_notification_thread_command_register_trigger( goto error; } - trigger_ht_element = (lttng_trigger_ht_element *) zmalloc(sizeof(*trigger_ht_element)); + trigger_ht_element = zmalloc(); if (!trigger_ht_element) { ret = -1; goto error; @@ -3336,7 +3336,7 @@ int handle_notification_thread_client_connect( DBG("Handling new notification channel client connection"); - client = (notification_client *) zmalloc(sizeof(*client)); + client = zmalloc(); if (!client) { /* Fatal error. */ ret = -1; @@ -4586,7 +4586,7 @@ struct lttng_event_notifier_notification *recv_one_event_notifier_notification( goto end; } - capture_buffer = (char *) zmalloc(capture_buffer_size); + capture_buffer = calloc(capture_buffer_size); if (!capture_buffer) { ERR("Failed to allocate capture buffer"); goto end; @@ -4889,7 +4889,7 @@ int handle_notification_thread_channel_sample( */ struct channel_state_sample *stored_sample; - stored_sample = (channel_state_sample *) zmalloc(sizeof(*stored_sample)); + stored_sample = zmalloc(); if (!stored_sample) { ret = -1; goto end_unlock; diff --git a/src/bin/lttng-sessiond/notification-thread.cpp b/src/bin/lttng-sessiond/notification-thread.cpp index 74e37902d..cd95e6885 100644 --- a/src/bin/lttng-sessiond/notification-thread.cpp +++ b/src/bin/lttng-sessiond/notification-thread.cpp @@ -94,7 +94,7 @@ struct notification_thread_handle *notification_thread_handle_create( struct notification_thread_handle *handle; struct lttng_pipe *event_pipe = NULL; - handle = (notification_thread_handle *) zmalloc(sizeof(*handle)); + handle = zmalloc(); if (!handle) { goto end; } @@ -162,7 +162,7 @@ char *get_notification_channel_sock_path(void) bool is_root = !getuid(); char *sock_path; - sock_path = (char *) zmalloc(LTTNG_PATH_MAX); + sock_path = calloc(LTTNG_PATH_MAX); if (!sock_path) { goto error; } diff --git a/src/bin/lttng-sessiond/notify-apps.cpp b/src/bin/lttng-sessiond/notify-apps.cpp index 8e238e5c1..80c7fb143 100644 --- a/src/bin/lttng-sessiond/notify-apps.cpp +++ b/src/bin/lttng-sessiond/notify-apps.cpp @@ -212,7 +212,7 @@ bool launch_application_notification_thread(int apps_cmd_notify_pipe_read_fd) struct thread_notifiers *notifiers; struct lttng_pipe *quit_pipe; - notifiers = (thread_notifiers *) zmalloc(sizeof(*notifiers)); + notifiers = zmalloc(); if (!notifiers) { goto error_alloc; } diff --git a/src/bin/lttng-sessiond/register.cpp b/src/bin/lttng-sessiond/register.cpp index 36a29d863..aac16b183 100644 --- a/src/bin/lttng-sessiond/register.cpp +++ b/src/bin/lttng-sessiond/register.cpp @@ -265,7 +265,7 @@ static void *thread_application_registration(void *data) (void) utils_set_fd_cloexec(sock); /* Create UST registration command for enqueuing */ - ust_cmd = (ust_command *) zmalloc(sizeof(struct ust_command)); + ust_cmd = zmalloc(); if (ust_cmd == NULL) { PERROR("ust command zmalloc"); ret = close(sock); @@ -392,7 +392,7 @@ struct lttng_thread *launch_application_registration_thread( const bool is_root = (getuid() == 0); int application_socket = -1; - thread_state = (struct thread_state *) zmalloc(sizeof(*thread_state)); + thread_state = zmalloc(); if (!thread_state) { goto error_alloc; } diff --git a/src/bin/lttng-sessiond/rotation-thread.cpp b/src/bin/lttng-sessiond/rotation-thread.cpp index 2f89fbc64..ee924ba98 100644 --- a/src/bin/lttng-sessiond/rotation-thread.cpp +++ b/src/bin/lttng-sessiond/rotation-thread.cpp @@ -89,7 +89,7 @@ struct rotation_thread_timer_queue *rotation_thread_timer_queue_create(void) { struct rotation_thread_timer_queue *queue = NULL; - queue = (rotation_thread_timer_queue *) zmalloc(sizeof(*queue)); + queue = zmalloc(); if (!queue) { PERROR("Failed to allocate timer rotate queue"); goto end; @@ -134,7 +134,7 @@ struct rotation_thread_handle *rotation_thread_handle_create( { struct rotation_thread_handle *handle; - handle = (rotation_thread_handle *) zmalloc(sizeof(*handle)); + handle = zmalloc(); if (!handle) { goto end; } @@ -193,7 +193,7 @@ void rotation_thread_enqueue_job(struct rotation_thread_timer_queue *queue, goto end; } - job = (rotation_thread_job *) zmalloc(sizeof(struct rotation_thread_job)); + job = zmalloc(); if (!job) { PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"", job_type_str, session->name); diff --git a/src/bin/lttng-sessiond/save.cpp b/src/bin/lttng-sessiond/save.cpp index 16b270514..757b97226 100644 --- a/src/bin/lttng-sessiond/save.cpp +++ b/src/bin/lttng-sessiond/save.cpp @@ -2322,7 +2322,7 @@ int save_consumer_output(struct config_writer *writer, { char *uri; - uri = (char *) zmalloc(PATH_MAX); + uri = calloc(PATH_MAX); if (!uri) { ret = LTTNG_ERR_NOMEM; goto end; diff --git a/src/bin/lttng-sessiond/session.cpp b/src/bin/lttng-sessiond/session.cpp index bcab0147d..fc43684e9 100644 --- a/src/bin/lttng-sessiond/session.cpp +++ b/src/bin/lttng-sessiond/session.cpp @@ -1187,7 +1187,7 @@ enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid, goto error; } } - new_session = (ltt_session *) zmalloc(sizeof(struct ltt_session)); + new_session = zmalloc(); if (!new_session) { PERROR("Failed to allocate an ltt_session structure"); ret_code = LTTNG_ERR_NOMEM; diff --git a/src/bin/lttng-sessiond/snapshot.cpp b/src/bin/lttng-sessiond/snapshot.cpp index f7821da95..1601f17cc 100644 --- a/src/bin/lttng-sessiond/snapshot.cpp +++ b/src/bin/lttng-sessiond/snapshot.cpp @@ -165,7 +165,7 @@ error: struct snapshot_output *snapshot_output_alloc(void) { - return (snapshot_output *) zmalloc(sizeof(struct snapshot_output)); + return zmalloc(); } /* diff --git a/src/bin/lttng-sessiond/thread.cpp b/src/bin/lttng-sessiond/thread.cpp index 9ce539b81..94890e6ef 100644 --- a/src/bin/lttng-sessiond/thread.cpp +++ b/src/bin/lttng-sessiond/thread.cpp @@ -77,7 +77,7 @@ struct lttng_thread *lttng_thread_create(const char *name, int ret; struct lttng_thread *thread; - thread = (lttng_thread *) zmalloc(sizeof(*thread)); + thread = zmalloc(); if (!thread) { goto error_alloc; } diff --git a/src/bin/lttng-sessiond/trace-kernel.cpp b/src/bin/lttng-sessiond/trace-kernel.cpp index 846630482..23d808ce8 100644 --- a/src/bin/lttng-sessiond/trace-kernel.cpp +++ b/src/bin/lttng-sessiond/trace-kernel.cpp @@ -151,7 +151,7 @@ struct ltt_kernel_session *trace_kernel_create_session(void) struct ltt_kernel_session *lks = NULL; /* Allocate a new ltt kernel session */ - lks = (ltt_kernel_session *) zmalloc(sizeof(struct ltt_kernel_session)); + lks = zmalloc(); if (lks == NULL) { PERROR("create kernel session zmalloc"); goto alloc_error; @@ -222,19 +222,19 @@ struct ltt_kernel_channel *trace_kernel_create_channel( LTTNG_ASSERT(chan); - lkc = (ltt_kernel_channel *) zmalloc(sizeof(struct ltt_kernel_channel)); + lkc = zmalloc(); if (lkc == NULL) { PERROR("ltt_kernel_channel zmalloc"); goto error; } - lkc->channel = (lttng_channel *) zmalloc(sizeof(struct lttng_channel)); + lkc->channel = zmalloc(); if (lkc->channel == NULL) { PERROR("lttng_channel zmalloc"); goto error; } - extended = (lttng_channel_extended *) zmalloc(sizeof(struct lttng_channel_extended)); + extended = zmalloc(); if (!extended) { PERROR("lttng_channel_channel zmalloc"); goto error; @@ -285,7 +285,7 @@ struct ltt_kernel_context *trace_kernel_create_context( { struct ltt_kernel_context *kctx; - kctx = (ltt_kernel_context *) zmalloc(sizeof(*kctx)); + kctx = zmalloc(); if (!kctx) { PERROR("zmalloc kernel context"); goto error; @@ -310,7 +310,7 @@ struct ltt_kernel_context *trace_kernel_copy_context( struct ltt_kernel_context *kctx_copy; LTTNG_ASSERT(kctx); - kctx_copy = (ltt_kernel_context *) zmalloc(sizeof(*kctx_copy)); + kctx_copy = zmalloc(); if (!kctx_copy) { PERROR("zmalloc ltt_kernel_context"); goto error; @@ -341,8 +341,8 @@ enum lttng_error_code trace_kernel_create_event( LTTNG_ASSERT(ev); - local_kernel_event = (ltt_kernel_event *) zmalloc(sizeof(struct ltt_kernel_event)); - attr = (lttng_kernel_abi_event *) zmalloc(sizeof(struct lttng_kernel_abi_event)); + local_kernel_event = zmalloc(); + attr = zmalloc(); if (local_kernel_event == NULL || attr == NULL) { PERROR("kernel event zmalloc"); ret = LTTNG_ERR_NOMEM; @@ -514,7 +514,7 @@ enum lttng_error_code trace_kernel_create_event_notifier_rule( LTTNG_ASSERT(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN); local_kernel_token_event_rule = - (ltt_kernel_event_notifier_rule *) zmalloc(sizeof(struct ltt_kernel_event_notifier_rule)); + zmalloc(); if (local_kernel_token_event_rule == NULL) { PERROR("Failed to allocate ltt_kernel_token_event_rule structure"); ret = LTTNG_ERR_NOMEM; @@ -735,8 +735,8 @@ struct ltt_kernel_metadata *trace_kernel_create_metadata(void) struct ltt_kernel_metadata *lkm; struct lttng_channel *chan; - lkm = (ltt_kernel_metadata *) zmalloc(sizeof(struct ltt_kernel_metadata)); - chan = (lttng_channel *) zmalloc(sizeof(struct lttng_channel)); + lkm = zmalloc(); + chan = zmalloc(); if (lkm == NULL || chan == NULL) { PERROR("kernel metadata zmalloc"); goto error; @@ -801,7 +801,7 @@ struct ltt_kernel_stream *trace_kernel_create_stream(const char *name, LTTNG_ASSERT(name); - lks = (ltt_kernel_stream *) zmalloc(sizeof(struct ltt_kernel_stream)); + lks = zmalloc(); if (lks == NULL) { PERROR("kernel stream zmalloc"); goto error; diff --git a/src/bin/lttng-sessiond/trace-ust.cpp b/src/bin/lttng-sessiond/trace-ust.cpp index 285ff0811..de368a3ba 100644 --- a/src/bin/lttng-sessiond/trace-ust.cpp +++ b/src/bin/lttng-sessiond/trace-ust.cpp @@ -271,7 +271,7 @@ struct ltt_ust_session *trace_ust_create_session(uint64_t session_id) struct ltt_ust_session *lus; /* Allocate a new ltt ust session */ - lus = (ltt_ust_session *) zmalloc(sizeof(struct ltt_ust_session)); + lus = zmalloc(); if (lus == NULL) { PERROR("create ust session zmalloc"); goto error_alloc; @@ -350,7 +350,7 @@ struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan, LTTNG_ASSERT(chan); - luc = (ltt_ust_channel *) zmalloc(sizeof(struct ltt_ust_channel)); + luc = zmalloc(); if (luc == NULL) { PERROR("ltt_ust_channel zmalloc"); goto error; @@ -463,7 +463,7 @@ enum lttng_error_code trace_ust_create_event(struct lttng_event *ev, goto error; } - local_ust_event = (ltt_ust_event *) zmalloc(sizeof(struct ltt_ust_event)); + local_ust_event = zmalloc(); if (local_ust_event == NULL) { PERROR("ust event zmalloc"); ret = LTTNG_ERR_NOMEM; @@ -687,7 +687,7 @@ struct ltt_ust_context *trace_ust_create_context( goto end; } - uctx = (ltt_ust_context *) zmalloc(sizeof(struct ltt_ust_context)); + uctx = zmalloc(); if (!uctx) { PERROR("zmalloc ltt_ust_context"); goto end; @@ -813,7 +813,7 @@ static int id_tracker_add_id(struct ust_id_tracker *id_tracker, int id) retval = LTTNG_ERR_PROCESS_ATTR_EXISTS; goto end; } - tracker_node = (ust_id_tracker_node *) zmalloc(sizeof(*tracker_node)); + tracker_node = zmalloc(); if (!tracker_node) { retval = LTTNG_ERR_NOMEM; goto end; diff --git a/src/bin/lttng-sessiond/tracker.cpp b/src/bin/lttng-sessiond/tracker.cpp index 204ade426..7d5872806 100644 --- a/src/bin/lttng-sessiond/tracker.cpp +++ b/src/bin/lttng-sessiond/tracker.cpp @@ -49,7 +49,7 @@ struct process_attr_tracker *process_attr_tracker_create(void) { struct process_attr_tracker *tracker; - tracker = (process_attr_tracker *) zmalloc(sizeof(*tracker)); + tracker = zmalloc(); if (!tracker) { return NULL; } @@ -201,7 +201,7 @@ enum process_attr_tracker_status process_attr_tracker_inclusion_set_add_value( goto end; } - value_node = (process_attr_tracker_value_node *) zmalloc(sizeof(*value_node)); + value_node = zmalloc(); if (!value_node) { status = PROCESS_ATTR_TRACKER_STATUS_ERROR; goto end; diff --git a/src/bin/lttng-sessiond/ust-app.cpp b/src/bin/lttng-sessiond/ust-app.cpp index 5cffdc974..fcc4267ad 100644 --- a/src/bin/lttng-sessiond/ust-app.cpp +++ b/src/bin/lttng-sessiond/ust-app.cpp @@ -696,7 +696,7 @@ ssize_t ust_app_push_metadata(struct ust_registry_session *registry, } /* Allocate only what we have to send. */ - metadata_str = (char *) zmalloc(len); + metadata_str = calloc(len); if (!metadata_str) { PERROR("zmalloc ust app metadata string"); ret_val = -ENOMEM; @@ -1160,7 +1160,7 @@ struct ust_app_session *alloc_ust_app_session(void) struct ust_app_session *ua_sess; /* Init most of the default value by allocating and zeroing */ - ua_sess = (ust_app_session *) zmalloc(sizeof(struct ust_app_session)); + ua_sess = zmalloc(); if (ua_sess == NULL) { PERROR("malloc"); goto error_free; @@ -1188,7 +1188,7 @@ struct ust_app_channel *alloc_ust_app_channel(const char *name, struct ust_app_channel *ua_chan; /* Init most of the default value by allocating and zeroing */ - ua_chan = (ust_app_channel *) zmalloc(sizeof(struct ust_app_channel)); + ua_chan = zmalloc(); if (ua_chan == NULL) { PERROR("malloc"); goto error; @@ -1240,7 +1240,7 @@ struct ust_app_stream *ust_app_alloc_stream(void) { struct ust_app_stream *stream = NULL; - stream = (ust_app_stream *) zmalloc(sizeof(*stream)); + stream = zmalloc(); if (stream == NULL) { PERROR("zmalloc ust app stream"); goto error; @@ -1263,7 +1263,7 @@ struct ust_app_event *alloc_ust_app_event(char *name, struct ust_app_event *ua_event; /* Init most of the default value by allocating and zeroing */ - ua_event = (ust_app_event *) zmalloc(sizeof(struct ust_app_event)); + ua_event = zmalloc(); if (ua_event == NULL) { PERROR("Failed to allocate ust_app_event structure"); goto error; @@ -1300,7 +1300,7 @@ static struct ust_app_event_notifier_rule *alloc_ust_app_event_notifier_rule( struct lttng_condition *condition = NULL; const struct lttng_event_rule *event_rule = NULL; - ua_event_notifier_rule = (ust_app_event_notifier_rule *) zmalloc(sizeof(struct ust_app_event_notifier_rule)); + ua_event_notifier_rule = zmalloc(); if (ua_event_notifier_rule == NULL) { PERROR("Failed to allocate ust_app_event_notifier_rule structure"); goto error; @@ -1360,7 +1360,7 @@ struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx) { struct ust_app_ctx *ua_ctx; - ua_ctx = (ust_app_ctx *) zmalloc(sizeof(struct ust_app_ctx)); + ua_ctx = zmalloc(); if (ua_ctx == NULL) { goto error; } @@ -1403,7 +1403,7 @@ static struct lttng_ust_abi_filter_bytecode *create_ust_filter_bytecode_from_byt struct lttng_ust_abi_filter_bytecode *filter = NULL; /* Copy filter bytecode. */ - filter = (lttng_ust_abi_filter_bytecode *) zmalloc(sizeof(*filter) + orig_f->len); + filter = zmalloc(sizeof(*filter) + orig_f->len); if (!filter) { PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len); goto error; @@ -1427,7 +1427,7 @@ create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode *orig_f) struct lttng_ust_abi_capture_bytecode *capture = NULL; /* Copy capture bytecode. */ - capture = (lttng_ust_abi_capture_bytecode *) zmalloc(sizeof(*capture) + orig_f->len); + capture = zmalloc(sizeof(*capture) + orig_f->len); if (!capture) { PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len); goto error; @@ -1711,7 +1711,7 @@ struct lttng_ust_abi_event_exclusion *create_ust_exclusion_from_exclusion( size_t exclusion_alloc_size = sizeof(struct lttng_ust_abi_event_exclusion) + LTTNG_UST_ABI_SYM_NAME_LEN * exclusion->count; - ust_exclusion = (lttng_ust_abi_event_exclusion *) zmalloc(exclusion_alloc_size); + ust_exclusion = zmalloc(exclusion_alloc_size); if (!ust_exclusion) { PERROR("malloc"); goto end; @@ -2331,7 +2331,7 @@ static void shadow_copy_event(struct ust_app_event *ua_event, if (uevent->exclusion) { exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + LTTNG_UST_ABI_SYM_NAME_LEN * uevent->exclusion->count; - ua_event->exclusion = (lttng_event_exclusion *) zmalloc(exclusion_alloc_size); + ua_event->exclusion = zmalloc(exclusion_alloc_size); if (ua_event->exclusion == NULL) { PERROR("malloc"); } else { @@ -3993,7 +3993,7 @@ struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) goto error; } - lta = (ust_app *) zmalloc(sizeof(struct ust_app)); + lta = zmalloc(); if (lta == NULL) { PERROR("malloc"); goto error_free_pipe; @@ -4369,7 +4369,7 @@ int ust_app_list_events(struct lttng_event **events) struct lttng_event *tmp_event; nbmem = UST_APP_EVENT_LIST_SIZE; - tmp_event = (lttng_event *) zmalloc(nbmem * sizeof(struct lttng_event)); + tmp_event = calloc(nbmem); if (tmp_event == NULL) { PERROR("zmalloc ust app events"); ret = -ENOMEM; @@ -4504,7 +4504,7 @@ int ust_app_list_event_fields(struct lttng_event_field **fields) struct lttng_event_field *tmp_event; nbmem = UST_APP_EVENT_LIST_SIZE; - tmp_event = (lttng_event_field *) zmalloc(nbmem * sizeof(struct lttng_event_field)); + tmp_event = calloc(nbmem); if (tmp_event == NULL) { PERROR("zmalloc ust app event fields"); ret = -ENOMEM; @@ -6389,11 +6389,13 @@ static int ust_app_fixup_legacy_context_fields(size_t *_nr_fields, if (!found) { goto end; } - new_fields = (struct lttng_ust_ctl_field *) zmalloc(sizeof(*new_fields) * new_nr_fields); + + new_fields = calloc(new_nr_fields); if (!new_fields) { ret = -ENOMEM; goto end; } + for (i = 0, j = 0; i < nr_fields; i++, j++) { const struct lttng_ust_ctl_field *field = &fields[i]; struct lttng_ust_ctl_field *new_field = &new_fields[j]; @@ -6944,7 +6946,7 @@ void ust_app_notify_sock_unregister(int sock) rcu_read_lock(); - obj = (ust_app_notify_sock_obj *) zmalloc(sizeof(*obj)); + obj = zmalloc(); if (!obj) { /* * An ENOMEM is kind of uncool. If this strikes we continue the diff --git a/src/bin/lttng-sessiond/ust-registry.cpp b/src/bin/lttng-sessiond/ust-registry.cpp index 0b92d19c5..b327fdf32 100644 --- a/src/bin/lttng-sessiond/ust-registry.cpp +++ b/src/bin/lttng-sessiond/ust-registry.cpp @@ -288,7 +288,7 @@ static struct ust_registry_event *alloc_event(int session_objd, return NULL; } - event = (ust_registry_event *) zmalloc(sizeof(*event)); + event = zmalloc(); if (!event) { PERROR("zmalloc ust registry event"); goto error; @@ -630,7 +630,7 @@ int ust_registry_create_or_find_enum(struct ust_registry_session *session, } /* Check if the enumeration was already dumped */ - reg_enum = (ust_registry_enum *) zmalloc(sizeof(*reg_enum)); + reg_enum = zmalloc(); if (!reg_enum) { PERROR("zmalloc ust registry enumeration"); ret = -ENOMEM; @@ -756,7 +756,7 @@ int ust_registry_channel_add(struct ust_registry_session *session, LTTNG_ASSERT(session); - chan = (ust_registry_channel *) zmalloc(sizeof(*chan)); + chan = zmalloc(); if (!chan) { PERROR("zmalloc ust registry channel"); ret = -ENOMEM; @@ -887,7 +887,7 @@ int ust_registry_session_init(struct ust_registry_session **sessionp, LTTNG_ASSERT(sessionp); - session = (ust_registry_session *) zmalloc(sizeof(*session)); + session = zmalloc(); if (!session) { PERROR("zmalloc ust registry session"); goto error_alloc; diff --git a/src/bin/lttng/commands/add_context.cpp b/src/bin/lttng/commands/add_context.cpp index 27d932c34..b25d2d10a 100644 --- a/src/bin/lttng/commands/add_context.cpp +++ b/src/bin/lttng/commands/add_context.cpp @@ -902,21 +902,21 @@ void destroy_ctx_type(struct ctx_type *type) if (type->opt) { free(type->opt->symbol); } - free(type->opt); + delete type->opt; free(type); } static struct ctx_type *create_ctx_type(void) { - struct ctx_type *type = (ctx_type *) zmalloc(sizeof(*type)); + struct ctx_type *type = zmalloc(); if (!type) { PERROR("malloc ctx_type"); goto end; } - type->opt = (struct ctx_opts *) zmalloc(sizeof(*type->opt)); + type->opt = new struct ctx_opts; if (!type->opt) { PERROR("malloc ctx_type options"); destroy_ctx_type(type); @@ -1091,7 +1091,7 @@ struct ctx_type *get_context_type(const char *ctx) } provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2; - provider_name = (char *) zmalloc(provider_name_len); + provider_name = calloc(provider_name_len); if (!provider_name) { PERROR("malloc provider_name"); goto not_found; @@ -1101,7 +1101,7 @@ struct ctx_type *get_context_type(const char *ctx) type->opt->u.app_ctx.provider_name = provider_name; ctx_name_len = len - colon_pos; - ctx_name = (char *) zmalloc(ctx_name_len); + ctx_name = calloc(ctx_name_len); if (!ctx_name) { PERROR("malloc ctx_name"); goto not_found; diff --git a/src/bin/lttng/commands/enable_events.cpp b/src/bin/lttng/commands/enable_events.cpp index 9d9a28ab8..1d81143ae 100644 --- a/src/bin/lttng/commands/enable_events.cpp +++ b/src/bin/lttng/commands/enable_events.cpp @@ -254,7 +254,7 @@ char *print_exclusions(const struct lttng_dynamic_pointer_array *exclusions) } length += sizeof(preamble); - ret = (char *) zmalloc(length); + ret = calloc(length); if (!ret) { return NULL; } diff --git a/src/bin/lttng/commands/list.cpp b/src/bin/lttng/commands/list.cpp index 444a96b40..8636cd101 100644 --- a/src/bin/lttng/commands/list.cpp +++ b/src/bin/lttng/commands/list.cpp @@ -91,11 +91,12 @@ static char *get_cmdline_by_pid(pid_t pid) } /* Caller must free() *cmdline */ - cmdline = (char *) zmalloc(PATH_MAX); + cmdline = zmalloc(PATH_MAX); if (!cmdline) { PERROR("malloc cmdline"); goto end; } + ret = fread(cmdline, 1, PATH_MAX, fp); if (ret < 0) { PERROR("fread proc list"); diff --git a/src/bin/lttng/conf.cpp b/src/bin/lttng/conf.cpp index d79fba989..33550eb37 100644 --- a/src/bin/lttng/conf.cpp +++ b/src/bin/lttng/conf.cpp @@ -178,7 +178,7 @@ int _config_read_session_name(const char *path, char **name) #define NAME_MAX_SCANF_IS_A_BROKEN_API "254" #endif - session_name = (char *) zmalloc(NAME_MAX); + session_name = calloc(NAME_MAX); if (session_name == NULL) { ret = -ENOMEM; ERR("Out of memory"); diff --git a/src/bin/lttng/uprobe.cpp b/src/bin/lttng/uprobe.cpp index 07ac5d5fa..c6a2d9429 100644 --- a/src/bin/lttng/uprobe.cpp +++ b/src/bin/lttng/uprobe.cpp @@ -58,7 +58,7 @@ int walk_command_search_path(const char *binary, char *binary_full_path) * This char array is used to concatenate path to binary to look for * the binary. */ - tentative_binary_path = (char *) zmalloc(LTTNG_PATH_MAX * sizeof(char)); + tentative_binary_path = calloc(LTTNG_PATH_MAX); if (!tentative_binary_path) { ret = -1; goto alloc_error; @@ -292,7 +292,7 @@ int parse_userspace_probe_opts(const char *opt, */ if (strchr(unescaped_target_path, '/') == NULL) { /* Walk the $PATH variable to find the targeted binary. */ - real_target_path = (char *) zmalloc(LTTNG_PATH_MAX * sizeof(char)); + real_target_path = calloc(LTTNG_PATH_MAX); if (!real_target_path) { PERROR("Error allocating path buffer"); ret = CMD_ERROR; diff --git a/src/common/actions/list.cpp b/src/common/actions/list.cpp index c80832570..e524399ad 100644 --- a/src/common/actions/list.cpp +++ b/src/common/actions/list.cpp @@ -362,7 +362,7 @@ struct lttng_action *lttng_action_list_create(void) struct lttng_action_list *action_list; struct lttng_action *action; - action_list = (lttng_action_list *) zmalloc(sizeof(struct lttng_action_list)); + action_list = zmalloc(); if (!action_list) { action = NULL; goto end; diff --git a/src/common/actions/notify.cpp b/src/common/actions/notify.cpp index 8ee51c316..ff117321e 100644 --- a/src/common/actions/notify.cpp +++ b/src/common/actions/notify.cpp @@ -132,7 +132,7 @@ struct lttng_action *lttng_action_notify_create(void) struct lttng_action_notify *notify = NULL; struct lttng_action *action = NULL; - notify = (lttng_action_notify *) zmalloc(sizeof(struct lttng_action_notify)); + notify = zmalloc(); if (!notify) { goto end; } diff --git a/src/common/actions/path.cpp b/src/common/actions/path.cpp index 0c8181896..cbb7519b1 100644 --- a/src/common/actions/path.cpp +++ b/src/common/actions/path.cpp @@ -23,7 +23,7 @@ struct lttng_action_path *lttng_action_path_create( goto error; } - path = (lttng_action_path *) zmalloc(sizeof(*path)); + path = zmalloc(); if (!path) { goto error; } diff --git a/src/common/actions/rate-policy.cpp b/src/common/actions/rate-policy.cpp index 58db624d8..25f8e4083 100644 --- a/src/common/actions/rate-policy.cpp +++ b/src/common/actions/rate-policy.cpp @@ -487,7 +487,7 @@ struct lttng_rate_policy *lttng_rate_policy_every_n_create(uint64_t interval) goto end; } - policy = (lttng_rate_policy_every_n *) zmalloc(sizeof(struct lttng_rate_policy_every_n)); + policy = zmalloc(); if (!policy) { goto end; } @@ -702,7 +702,7 @@ struct lttng_rate_policy *lttng_rate_policy_once_after_n_create( goto end; } - policy = (lttng_rate_policy_once_after_n *) zmalloc(sizeof(struct lttng_rate_policy_once_after_n)); + policy = zmalloc(); if (!policy) { goto end; } diff --git a/src/common/actions/rotate-session.cpp b/src/common/actions/rotate-session.cpp index ad2f9e293..a8e882b4f 100644 --- a/src/common/actions/rotate-session.cpp +++ b/src/common/actions/rotate-session.cpp @@ -288,7 +288,7 @@ end: struct lttng_action *lttng_action_rotate_session_create(void) { - struct lttng_action *action = NULL; + struct lttng_action_rotate_session *action_rotate = NULL; struct lttng_rate_policy *policy = NULL; enum lttng_action_status status; @@ -298,12 +298,13 @@ struct lttng_action *lttng_action_rotate_session_create(void) goto end; } - action = (lttng_action *) zmalloc(sizeof(struct lttng_action_rotate_session)); - if (!action) { + action_rotate = zmalloc(); + if (!action_rotate) { goto end; } - lttng_action_init(action, LTTNG_ACTION_TYPE_ROTATE_SESSION, + lttng_action_init(&action_rotate->parent, + LTTNG_ACTION_TYPE_ROTATE_SESSION, lttng_action_rotate_session_validate, lttng_action_rotate_session_serialize, lttng_action_rotate_session_is_equal, @@ -312,16 +313,17 @@ struct lttng_action *lttng_action_rotate_session_create(void) lttng_action_generic_add_error_query_results, lttng_action_rotate_session_mi_serialize); - status = lttng_action_rotate_session_set_rate_policy(action, policy); + status = lttng_action_rotate_session_set_rate_policy( + &action_rotate->parent, policy); if (status != LTTNG_ACTION_STATUS_OK) { - free(action); - action = NULL; + lttng_action_destroy(&action_rotate->parent); + action_rotate = NULL; goto end; } end: lttng_rate_policy_destroy(policy); - return action; + return action_rotate ? &action_rotate->parent : nullptr; } enum lttng_action_status lttng_action_rotate_session_set_session_name( diff --git a/src/common/actions/snapshot-session.cpp b/src/common/actions/snapshot-session.cpp index 600d803e3..c3570081a 100644 --- a/src/common/actions/snapshot-session.cpp +++ b/src/common/actions/snapshot-session.cpp @@ -445,7 +445,7 @@ end: struct lttng_action *lttng_action_snapshot_session_create(void) { - struct lttng_action *action = NULL; + struct lttng_action_snapshot_session *action_snapshot = NULL; struct lttng_rate_policy *policy = NULL; enum lttng_action_status status; @@ -455,12 +455,13 @@ struct lttng_action *lttng_action_snapshot_session_create(void) goto end; } - action = (lttng_action *) zmalloc(sizeof(struct lttng_action_snapshot_session)); - if (!action) { + action_snapshot = zmalloc(); + if (!action_snapshot) { goto end; } - lttng_action_init(action, LTTNG_ACTION_TYPE_SNAPSHOT_SESSION, + lttng_action_init(&action_snapshot->parent, + LTTNG_ACTION_TYPE_SNAPSHOT_SESSION, lttng_action_snapshot_session_validate, lttng_action_snapshot_session_serialize, lttng_action_snapshot_session_is_equal, @@ -469,16 +470,17 @@ struct lttng_action *lttng_action_snapshot_session_create(void) lttng_action_generic_add_error_query_results, lttng_action_snapshot_session_mi_serialize); - status = lttng_action_snapshot_session_set_rate_policy(action, policy); + status = lttng_action_snapshot_session_set_rate_policy( + &action_snapshot->parent, policy); if (status != LTTNG_ACTION_STATUS_OK) { - free(action); - action = NULL; + lttng_action_destroy(&action_snapshot->parent); + action_snapshot = NULL; goto end; } end: lttng_rate_policy_destroy(policy); - return action; + return action_snapshot ? &action_snapshot->parent : nullptr; } enum lttng_action_status lttng_action_snapshot_session_set_session_name( diff --git a/src/common/actions/start-session.cpp b/src/common/actions/start-session.cpp index 739540189..f9138f51a 100644 --- a/src/common/actions/start-session.cpp +++ b/src/common/actions/start-session.cpp @@ -291,7 +291,7 @@ end: struct lttng_action *lttng_action_start_session_create(void) { - struct lttng_action *action = NULL; + struct lttng_action_start_session *action_start = NULL; struct lttng_rate_policy *policy = NULL; enum lttng_action_status status; @@ -301,12 +301,13 @@ struct lttng_action *lttng_action_start_session_create(void) goto end; } - action = (lttng_action *) zmalloc(sizeof(struct lttng_action_start_session)); - if (!action) { + action_start = zmalloc(); + if (!action_start) { goto end; } - lttng_action_init(action, LTTNG_ACTION_TYPE_START_SESSION, + lttng_action_init(&action_start->parent, + LTTNG_ACTION_TYPE_START_SESSION, lttng_action_start_session_validate, lttng_action_start_session_serialize, lttng_action_start_session_is_equal, @@ -315,16 +316,17 @@ struct lttng_action *lttng_action_start_session_create(void) lttng_action_generic_add_error_query_results, lttng_action_start_session_mi_serialize); - status = lttng_action_start_session_set_rate_policy(action, policy); + status = lttng_action_start_session_set_rate_policy( + &action_start->parent, policy); if (status != LTTNG_ACTION_STATUS_OK) { - free(action); - action = NULL; + lttng_action_destroy(&action_start->parent); + action_start = NULL; goto end; } end: lttng_rate_policy_destroy(policy); - return action; + return &action_start->parent; } enum lttng_action_status lttng_action_start_session_set_session_name( diff --git a/src/common/actions/stop-session.cpp b/src/common/actions/stop-session.cpp index 3cffb4a55..75ff1b847 100644 --- a/src/common/actions/stop-session.cpp +++ b/src/common/actions/stop-session.cpp @@ -291,7 +291,7 @@ end: struct lttng_action *lttng_action_stop_session_create(void) { - struct lttng_action *action = NULL; + struct lttng_action_stop_session *action_stop = NULL; struct lttng_rate_policy *policy = NULL; enum lttng_action_status status; @@ -301,12 +301,12 @@ struct lttng_action *lttng_action_stop_session_create(void) goto end; } - action = (lttng_action *) zmalloc(sizeof(struct lttng_action_stop_session)); - if (!action) { + action_stop = zmalloc(); + if (!action_stop) { goto end; } - lttng_action_init(action, LTTNG_ACTION_TYPE_STOP_SESSION, + lttng_action_init(&action_stop->parent, LTTNG_ACTION_TYPE_STOP_SESSION, lttng_action_stop_session_validate, lttng_action_stop_session_serialize, lttng_action_stop_session_is_equal, @@ -315,16 +315,17 @@ struct lttng_action *lttng_action_stop_session_create(void) lttng_action_generic_add_error_query_results, lttng_action_stop_session_mi_serialize); - status = lttng_action_stop_session_set_rate_policy(action, policy); + status = lttng_action_stop_session_set_rate_policy( + &action_stop->parent, policy); if (status != LTTNG_ACTION_STATUS_OK) { - free(action); - action = NULL; + lttng_action_destroy(&action_stop->parent); + action_stop = NULL; goto end; } end: lttng_rate_policy_destroy(policy); - return action; + return &action_stop->parent; } enum lttng_action_status lttng_action_stop_session_set_session_name( diff --git a/src/common/argpar-utils/argpar-utils.hpp b/src/common/argpar-utils/argpar-utils.hpp index ea7734507..b77609edd 100644 --- a/src/common/argpar-utils/argpar-utils.hpp +++ b/src/common/argpar-utils/argpar-utils.hpp @@ -8,16 +8,16 @@ #ifndef COMMON_ARGPAR_UTILS_H #define COMMON_ARGPAR_UTILS_H -#ifdef __cplusplus -extern "C" { -#endif - #include #include #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define WHILE_PARSING_ARG_N_ARG_FMT "While parsing argument #%d (`%s`): " enum parse_next_item_status diff --git a/src/common/bytecode/bytecode.cpp b/src/common/bytecode/bytecode.cpp index 707394229..c32f3900b 100644 --- a/src/common/bytecode/bytecode.cpp +++ b/src/common/bytecode/bytecode.cpp @@ -251,9 +251,8 @@ end: struct lttng_bytecode *lttng_bytecode_copy( const struct lttng_bytecode *orig_f) { - struct lttng_bytecode *bytecode = NULL; - - bytecode = (lttng_bytecode *) zmalloc(sizeof(*bytecode) + orig_f->len); + lttng_bytecode *bytecode + = zmalloc(sizeof(*bytecode) + orig_f->len); if (!bytecode) { goto error; } diff --git a/src/common/channel.cpp b/src/common/channel.cpp index be3b62343..5bef79558 100644 --- a/src/common/channel.cpp +++ b/src/common/channel.cpp @@ -37,7 +37,7 @@ struct lttng_channel *lttng_channel_copy(const struct lttng_channel *src) struct lttng_channel_extended *extended = nullptr; struct lttng_channel *channel = nullptr, *ret = nullptr; - channel = (struct lttng_channel *) zmalloc(sizeof(*channel)); + channel = zmalloc(); if (!channel) { goto end; } @@ -45,8 +45,7 @@ struct lttng_channel *lttng_channel_copy(const struct lttng_channel *src) *channel = *src; if (src->attr.extended.ptr) { - extended = (struct lttng_channel_extended *) zmalloc( - sizeof(*extended)); + extended = zmalloc(); if (!extended) { goto end; } @@ -71,15 +70,13 @@ struct lttng_channel *lttng_channel_create_internal(void) struct lttng_channel *local_channel = nullptr, *ret = nullptr; struct lttng_channel_extended *extended = nullptr; - local_channel = (struct lttng_channel *) zmalloc( - sizeof(struct lttng_channel)); + local_channel = zmalloc(); if (!local_channel) { goto end; } /* Extended struct */ - extended = (struct lttng_channel_extended *) zmalloc( - sizeof(*extended)); + extended = zmalloc(); if (!extended) { goto end; } diff --git a/src/common/compat/directory-handle.cpp b/src/common/compat/directory-handle.cpp index ba5c6925c..5c8d28db9 100644 --- a/src/common/compat/directory-handle.cpp +++ b/src/common/compat/directory-handle.cpp @@ -147,7 +147,7 @@ struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd( int dirfd) { int ret; - struct lttng_directory_handle *handle = (lttng_directory_handle *) zmalloc(sizeof(*handle)); + struct lttng_directory_handle *handle = zmalloc(); struct stat stat_buf; if (!handle) { @@ -405,7 +405,7 @@ end: static struct lttng_directory_handle *_lttng_directory_handle_create(char *path) { - struct lttng_directory_handle *handle = zmalloc(sizeof(*handle)); + struct lttng_directory_handle *handle = zmalloc(); if (!handle) { goto end; @@ -525,7 +525,7 @@ struct lttng_directory_handle *lttng_directory_handle_create_from_handle( handle_path_len, LTTNG_PATH_MAX); goto end; } - new_path = zmalloc(handle_path_len); + new_path = zmalloc(handle_path_len); if (!new_path) { PERROR("Failed to initialize directory handle"); goto end; diff --git a/src/common/compat/poll.cpp b/src/common/compat/poll.cpp index 4f532a26d..0617905a6 100644 --- a/src/common/compat/poll.cpp +++ b/src/common/compat/poll.cpp @@ -71,11 +71,11 @@ error: /* * Create epoll set and allocate returned events structure. */ -int compat_epoll_create(struct lttng_poll_event *events, int size, int flags) +int compat_epoll_create(struct lttng_poll_event *events, int count, int flags) { int ret; - if (events == NULL || size <= 0) { + if (events == NULL || count <= 0) { goto error; } @@ -86,11 +86,11 @@ int compat_epoll_create(struct lttng_poll_event *events, int size, int flags) } /* Don't bust the limit here */ - if (size > poll_max_size) { - size = poll_max_size; + if (count > poll_max_size) { + count = poll_max_size; } - ret = compat_glibc_epoll_create(size, flags); + ret = compat_glibc_epoll_create(count, flags); if (ret < 0) { /* At this point, every error is fatal */ PERROR("epoll_create1"); @@ -100,13 +100,13 @@ int compat_epoll_create(struct lttng_poll_event *events, int size, int flags) events->epfd = ret; /* This *must* be freed by using lttng_poll_free() */ - events->events = (epoll_event *) zmalloc(size * sizeof(struct epoll_event)); + events->events = calloc(count); if (events->events == NULL) { PERROR("zmalloc epoll set"); goto error_close; } - events->alloc_size = events->init_size = size; + events->alloc_size = events->init_size = count; events->nb_fd = 0; return 0; diff --git a/src/common/compat/string.hpp b/src/common/compat/string.hpp index 4a96cb145..526f2b2ce 100644 --- a/src/common/compat/string.hpp +++ b/src/common/compat/string.hpp @@ -61,7 +61,7 @@ char *lttng_strndup(const char *s, size_t n) navail = n + 1; } - ret = (char *) malloc(navail); + ret = malloc(navail); if (!ret) { goto end; } diff --git a/src/common/conditions/buffer-usage.cpp b/src/common/conditions/buffer-usage.cpp index b1d001dc4..57e1c79cb 100644 --- a/src/common/conditions/buffer-usage.cpp +++ b/src/common/conditions/buffer-usage.cpp @@ -321,7 +321,7 @@ struct lttng_condition *lttng_condition_buffer_usage_create( { struct lttng_condition_buffer_usage *condition; - condition = (lttng_condition_buffer_usage *) zmalloc(sizeof(struct lttng_condition_buffer_usage)); + condition = zmalloc(); if (!condition) { return NULL; } @@ -864,7 +864,7 @@ struct lttng_evaluation *lttng_evaluation_buffer_usage_create( { struct lttng_evaluation_buffer_usage *usage; - usage = (lttng_evaluation_buffer_usage *) zmalloc(sizeof(struct lttng_evaluation_buffer_usage)); + usage = zmalloc(); if (!usage) { goto end; } diff --git a/src/common/conditions/event-rule-matches.cpp b/src/common/conditions/event-rule-matches.cpp index 033dbca48..0a926755e 100644 --- a/src/common/conditions/event-rule-matches.cpp +++ b/src/common/conditions/event-rule-matches.cpp @@ -518,7 +518,7 @@ struct lttng_condition *lttng_condition_event_rule_matches_create( goto end; } - condition = (lttng_condition_event_rule_matches *) zmalloc(sizeof(struct lttng_condition_event_rule_matches)); + condition = zmalloc(); if (!condition) { return NULL; } @@ -889,7 +889,7 @@ lttng_condition_event_rule_matches_append_capture_descriptor( goto end; } - descriptor = (lttng_capture_descriptor *) malloc(sizeof(*descriptor)); + descriptor = malloc(); if (descriptor == NULL) { status = LTTNG_CONDITION_STATUS_ERROR; goto end; @@ -1385,7 +1385,7 @@ struct lttng_evaluation *lttng_evaluation_event_rule_matches_create( struct lttng_evaluation_event_rule_matches *hit; struct lttng_evaluation *evaluation = NULL; - hit = (lttng_evaluation_event_rule_matches *) zmalloc(sizeof(struct lttng_evaluation_event_rule_matches)); + hit = zmalloc(); if (!hit) { goto error; } diff --git a/src/common/conditions/session-consumed-size.cpp b/src/common/conditions/session-consumed-size.cpp index 8bc70abf6..abb5db8ce 100644 --- a/src/common/conditions/session-consumed-size.cpp +++ b/src/common/conditions/session-consumed-size.cpp @@ -203,7 +203,7 @@ struct lttng_condition *lttng_condition_session_consumed_size_create(void) { struct lttng_condition_session_consumed_size *condition; - condition = (lttng_condition_session_consumed_size *) zmalloc(sizeof(struct lttng_condition_session_consumed_size)); + condition = zmalloc(); if (!condition) { return NULL; } @@ -484,7 +484,7 @@ struct lttng_evaluation *lttng_evaluation_session_consumed_size_create( { struct lttng_evaluation_session_consumed_size *consumed_eval; - consumed_eval = (lttng_evaluation_session_consumed_size *) zmalloc(sizeof(struct lttng_evaluation_session_consumed_size)); + consumed_eval = zmalloc(); if (!consumed_eval) { goto end; } diff --git a/src/common/conditions/session-rotation.cpp b/src/common/conditions/session-rotation.cpp index 094d1ea94..82e843465 100644 --- a/src/common/conditions/session-rotation.cpp +++ b/src/common/conditions/session-rotation.cpp @@ -185,7 +185,7 @@ struct lttng_condition *lttng_condition_session_rotation_create( { struct lttng_condition_session_rotation *condition; - condition = (lttng_condition_session_rotation *) zmalloc(sizeof(struct lttng_condition_session_rotation)); + condition = zmalloc(); if (!condition) { return NULL; } @@ -333,7 +333,7 @@ struct lttng_evaluation *lttng_evaluation_session_rotation_create( { struct lttng_evaluation_session_rotation *evaluation; - evaluation = (lttng_evaluation_session_rotation *) zmalloc(sizeof(struct lttng_evaluation_session_rotation)); + evaluation = zmalloc(); if (!evaluation) { return NULL; } diff --git a/src/common/config/session-config.cpp b/src/common/config/session-config.cpp index dfbddf291..9e5f0ff20 100644 --- a/src/common/config/session-config.cpp +++ b/src/common/config/session-config.cpp @@ -287,7 +287,7 @@ struct config_writer *config_writer_create(int fd_output, int indent) struct config_writer *writer; xmlOutputBufferPtr buffer; - writer = (config_writer *) zmalloc(sizeof(struct config_writer)); + writer = zmalloc(); if (!writer) { PERROR("zmalloc config_writer_create"); goto end; @@ -581,7 +581,7 @@ char *get_session_config_xsd_path(void) base_path_len = strlen(base_path); max_path_len = base_path_len + sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1; - xsd_path = (char *) zmalloc(max_path_len); + xsd_path = zmalloc(max_path_len); if (!xsd_path) { goto end; } @@ -1856,7 +1856,7 @@ int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle, continue; } - exclusions = (char **) zmalloc(exclusion_count * sizeof(char *)); + exclusions = calloc(exclusion_count); if (!exclusions) { exclusion_count = 0; ret = -LTTNG_ERR_NOMEM; @@ -3399,9 +3399,8 @@ int process_session_node(xmlNodePtr session_node, const char *session_name, /* Init domains to create the session handles */ for (node = xmlFirstElementChild(domains_node); node; node = xmlNextElementSibling(node)) { - struct lttng_domain *domain; + lttng_domain *domain = zmalloc(); - domain = (lttng_domain *) zmalloc(sizeof(*domain)); if (!domain) { ret = -LTTNG_ERR_NOMEM; goto error; diff --git a/src/common/consumer/consumer-metadata-cache.cpp b/src/common/consumer/consumer-metadata-cache.cpp index a810c7dab..274ecf48f 100644 --- a/src/common/consumer/consumer-metadata-cache.cpp +++ b/src/common/consumer/consumer-metadata-cache.cpp @@ -129,8 +129,7 @@ int consumer_metadata_cache_allocate(struct lttng_consumer_channel *channel) LTTNG_ASSERT(channel); - channel->metadata_cache = (consumer_metadata_cache *) zmalloc( - sizeof(struct consumer_metadata_cache)); + channel->metadata_cache = zmalloc(); if (!channel->metadata_cache) { PERROR("zmalloc metadata cache struct"); ret = -1; diff --git a/src/common/consumer/consumer-stream.cpp b/src/common/consumer/consumer-stream.cpp index f57e37ab8..c22c7e5f3 100644 --- a/src/common/consumer/consumer-stream.cpp +++ b/src/common/consumer/consumer-stream.cpp @@ -654,7 +654,7 @@ struct lttng_consumer_stream *consumer_stream_create( int ret; struct lttng_consumer_stream *stream; - stream = (lttng_consumer_stream *) zmalloc(sizeof(*stream)); + stream = zmalloc(); if (stream == NULL) { PERROR("malloc struct lttng_consumer_stream"); ret = -ENOMEM; diff --git a/src/common/consumer/consumer.cpp b/src/common/consumer/consumer.cpp index 685d55f83..9393e6cc2 100644 --- a/src/common/consumer/consumer.cpp +++ b/src/common/consumer/consumer.cpp @@ -662,7 +662,7 @@ static struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair( goto error; } - obj = (consumer_relayd_sock_pair *) zmalloc(sizeof(struct consumer_relayd_sock_pair)); + obj = zmalloc(); if (obj == NULL) { PERROR("zmalloc relayd sock"); goto error; @@ -1029,7 +1029,7 @@ struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key, } } - channel = (lttng_consumer_channel *) zmalloc(sizeof(*channel)); + channel = zmalloc(); if (channel == NULL) { PERROR("malloc struct lttng_consumer_channel"); goto end; @@ -1425,7 +1425,7 @@ struct lttng_consumer_local_data *lttng_consumer_create( the_consumer_data.type == type); the_consumer_data.type = type; - ctx = (lttng_consumer_local_data *) zmalloc(sizeof(struct lttng_consumer_local_data)); + ctx = zmalloc(); if (ctx == NULL) { PERROR("allocating context"); goto error; @@ -2561,7 +2561,7 @@ void *consumer_thread_data_poll(void *data) health_code_update(); - local_stream = (lttng_consumer_stream **) zmalloc(sizeof(struct lttng_consumer_stream *)); + local_stream = zmalloc(); if (local_stream == NULL) { PERROR("local_stream malloc"); goto end; @@ -2586,18 +2586,14 @@ void *consumer_thread_data_poll(void *data) local_stream = NULL; /* Allocate for all fds */ - pollfd = (struct pollfd *) zmalloc((the_consumer_data.stream_count + - nb_pipes_fd) * - sizeof(struct pollfd)); + pollfd = calloc(the_consumer_data.stream_count + nb_pipes_fd); if (pollfd == NULL) { PERROR("pollfd malloc"); pthread_mutex_unlock(&the_consumer_data.lock); goto end; } - local_stream = (lttng_consumer_stream **) zmalloc((the_consumer_data.stream_count + - nb_pipes_fd) * - sizeof(struct lttng_consumer_stream *)); + local_stream = calloc(the_consumer_data.stream_count + nb_pipes_fd); if (local_stream == NULL) { PERROR("local_stream malloc"); pthread_mutex_unlock(&the_consumer_data.lock); diff --git a/src/common/consumer/metadata-bucket.cpp b/src/common/consumer/metadata-bucket.cpp index ed78ec947..f9c35e22c 100644 --- a/src/common/consumer/metadata-bucket.cpp +++ b/src/common/consumer/metadata-bucket.cpp @@ -25,9 +25,7 @@ struct metadata_bucket { struct metadata_bucket *metadata_bucket_create( metadata_bucket_flush_cb flush, void *data) { - struct metadata_bucket *bucket; - - bucket = (metadata_bucket *) zmalloc(sizeof(typeof(*bucket))); + metadata_bucket *bucket = zmalloc(); if (!bucket) { PERROR("Failed to allocate buffer bucket"); goto end; diff --git a/src/common/context.cpp b/src/common/context.cpp index 98c29f86c..a4111ad90 100644 --- a/src/common/context.cpp +++ b/src/common/context.cpp @@ -52,7 +52,7 @@ int parse_application_context(const char *str, char **out_provider_name, } provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2; - provider_name = (char *) zmalloc(provider_name_len); + provider_name = calloc(provider_name_len); if (!provider_name) { PERROR("malloc provider_name"); goto not_found; @@ -61,7 +61,7 @@ int parse_application_context(const char *str, char **out_provider_name, provider_name_len - 1); ctx_name_len = len - colon_pos; - ctx_name = (char *) zmalloc(ctx_name_len); + ctx_name = calloc(ctx_name_len); if (!ctx_name) { PERROR("malloc ctx_name"); goto not_found; diff --git a/src/common/error-query.cpp b/src/common/error-query.cpp index b29996651..13efbbfd7 100644 --- a/src/common/error-query.cpp +++ b/src/common/error-query.cpp @@ -111,7 +111,7 @@ struct lttng_error_query *lttng_error_query_trigger_create( goto end; } - query = (lttng_error_query_trigger *) zmalloc(sizeof(*query)); + query = zmalloc(); if (!query) { PERROR("Failed to allocate trigger error query"); goto error; @@ -142,7 +142,7 @@ struct lttng_error_query *lttng_error_query_condition_create( goto end; } - query = (lttng_error_query_condition *) zmalloc(sizeof(*query)); + query = zmalloc(); if (!query) { PERROR("Failed to allocate condition error query"); goto error; @@ -221,7 +221,7 @@ struct lttng_error_query *lttng_error_query_action_create( goto end; } - query = (lttng_error_query_action *) zmalloc(sizeof(*query)); + query = zmalloc(); if (!query) { PERROR("Failed to allocate action error query"); goto error; @@ -388,7 +388,7 @@ lttng_error_query_result_counter_create( int init_ret; struct lttng_error_query_result_counter *counter; - counter = (lttng_error_query_result_counter *) zmalloc(sizeof(*counter)); + counter = zmalloc(); if (!counter) { PERROR("Failed to allocate error query counter result"); goto end; @@ -419,7 +419,7 @@ void destroy_result(void *ptr) struct lttng_error_query_results *lttng_error_query_results_create(void) { - struct lttng_error_query_results *set = (lttng_error_query_results *) zmalloc(sizeof(*set)); + struct lttng_error_query_results *set = zmalloc(); if (!set) { PERROR("Failed to allocate an error query result set"); diff --git a/src/common/event-expr/event-expr.cpp b/src/common/event-expr/event-expr.cpp index 9306dbeee..1b592bdb1 100644 --- a/src/common/event-expr/event-expr.cpp +++ b/src/common/event-expr/event-expr.cpp @@ -40,7 +40,7 @@ struct lttng_event_expr *create_empty_expr(enum lttng_event_expr_type type, { struct lttng_event_expr *expr; - expr = (lttng_event_expr *) zmalloc(size); + expr = zmalloc(size); if (!expr) { goto end; } diff --git a/src/common/event-field-value.cpp b/src/common/event-field-value.cpp index 816017a2d..3ab12728c 100644 --- a/src/common/event-field-value.cpp +++ b/src/common/event-field-value.cpp @@ -23,7 +23,7 @@ struct lttng_event_field_value *create_empty_field_val( { struct lttng_event_field_value *field_val; - field_val = (lttng_event_field_value *) zmalloc(size); + field_val = zmalloc(size); if (!field_val) { goto end; } diff --git a/src/common/event-rule/jul-logging.cpp b/src/common/event-rule/jul-logging.cpp index 51cbdc004..14fa179eb 100644 --- a/src/common/event-rule/jul-logging.cpp +++ b/src/common/event-rule/jul-logging.cpp @@ -421,7 +421,7 @@ static struct lttng_event *lttng_event_rule_jul_logging_generate_lttng_event( jul_logging = container_of( rule, const struct lttng_event_rule_jul_logging, parent); - local_event = (lttng_event *) zmalloc(sizeof(*local_event)); + local_event = zmalloc(); if (!local_event) { goto error; } @@ -561,7 +561,7 @@ struct lttng_event_rule *lttng_event_rule_jul_logging_create(void) struct lttng_event_rule_jul_logging *tp_rule; enum lttng_event_rule_status status; - tp_rule = (lttng_event_rule_jul_logging *) zmalloc(sizeof(struct lttng_event_rule_jul_logging)); + tp_rule = zmalloc(); if (!tp_rule) { goto end; } diff --git a/src/common/event-rule/kernel-kprobe.cpp b/src/common/event-rule/kernel-kprobe.cpp index 628a051c3..bad894e35 100644 --- a/src/common/event-rule/kernel-kprobe.cpp +++ b/src/common/event-rule/kernel-kprobe.cpp @@ -289,7 +289,7 @@ struct lttng_event_rule *lttng_event_rule_kernel_kprobe_create( struct lttng_event_rule *rule = NULL; struct lttng_event_rule_kernel_kprobe *krule; - krule = (lttng_event_rule_kernel_kprobe *) zmalloc(sizeof(struct lttng_event_rule_kernel_kprobe)); + krule = zmalloc(); if (!krule) { goto end; } diff --git a/src/common/event-rule/kernel-syscall.cpp b/src/common/event-rule/kernel-syscall.cpp index 815f99d9d..d006eaa05 100644 --- a/src/common/event-rule/kernel-syscall.cpp +++ b/src/common/event-rule/kernel-syscall.cpp @@ -356,7 +356,7 @@ struct lttng_event_rule *lttng_event_rule_kernel_syscall_create( goto end; } - syscall_rule = (lttng_event_rule_kernel_syscall *) zmalloc(sizeof(struct lttng_event_rule_kernel_syscall)); + syscall_rule = zmalloc(); if (!syscall_rule) { goto end; } diff --git a/src/common/event-rule/kernel-tracepoint.cpp b/src/common/event-rule/kernel-tracepoint.cpp index e546de1f3..0bedfb501 100644 --- a/src/common/event-rule/kernel-tracepoint.cpp +++ b/src/common/event-rule/kernel-tracepoint.cpp @@ -333,7 +333,7 @@ struct lttng_event_rule *lttng_event_rule_kernel_tracepoint_create(void) struct lttng_event_rule_kernel_tracepoint *tp_rule; enum lttng_event_rule_status status; - tp_rule = (lttng_event_rule_kernel_tracepoint *) zmalloc(sizeof(struct lttng_event_rule_kernel_tracepoint)); + tp_rule = zmalloc(); if (!tp_rule) { goto end; } diff --git a/src/common/event-rule/kernel-uprobe.cpp b/src/common/event-rule/kernel-uprobe.cpp index 1a42421d3..4e611aca2 100644 --- a/src/common/event-rule/kernel-uprobe.cpp +++ b/src/common/event-rule/kernel-uprobe.cpp @@ -279,7 +279,7 @@ struct lttng_event_rule *lttng_event_rule_kernel_uprobe_create( struct lttng_event_rule *rule = NULL; struct lttng_event_rule_kernel_uprobe *urule; - urule = (lttng_event_rule_kernel_uprobe *) zmalloc(sizeof(struct lttng_event_rule_kernel_uprobe)); + urule = zmalloc(); if (!urule) { goto end; } diff --git a/src/common/event-rule/log4j-logging.cpp b/src/common/event-rule/log4j-logging.cpp index 937331a36..b7386c0ba 100644 --- a/src/common/event-rule/log4j-logging.cpp +++ b/src/common/event-rule/log4j-logging.cpp @@ -421,7 +421,7 @@ static struct lttng_event *lttng_event_rule_log4j_logging_generate_lttng_event( log4j_logging = container_of( rule, const struct lttng_event_rule_log4j_logging, parent); - local_event = (lttng_event *) zmalloc(sizeof(*local_event)); + local_event = zmalloc(); if (!local_event) { goto error; } @@ -560,7 +560,7 @@ struct lttng_event_rule *lttng_event_rule_log4j_logging_create(void) struct lttng_event_rule_log4j_logging *tp_rule; enum lttng_event_rule_status status; - tp_rule = (lttng_event_rule_log4j_logging *) zmalloc(sizeof(struct lttng_event_rule_log4j_logging)); + tp_rule = zmalloc(); if (!tp_rule) { goto end; } diff --git a/src/common/event-rule/python-logging.cpp b/src/common/event-rule/python-logging.cpp index 20b649648..25e967702 100644 --- a/src/common/event-rule/python-logging.cpp +++ b/src/common/event-rule/python-logging.cpp @@ -421,7 +421,7 @@ static struct lttng_event *lttng_event_rule_python_logging_generate_lttng_event( python_logging = container_of( rule, const struct lttng_event_rule_python_logging, parent); - local_event = (lttng_event *) zmalloc(sizeof(*local_event)); + local_event = zmalloc(); if (!local_event) { goto error; } @@ -560,7 +560,7 @@ struct lttng_event_rule *lttng_event_rule_python_logging_create(void) struct lttng_event_rule_python_logging *tp_rule; enum lttng_event_rule_status status; - tp_rule = (lttng_event_rule_python_logging *) zmalloc(sizeof(struct lttng_event_rule_python_logging)); + tp_rule = zmalloc(); if (!tp_rule) { goto end; } diff --git a/src/common/event-rule/user-tracepoint.cpp b/src/common/event-rule/user-tracepoint.cpp index 8932ca786..48040fc59 100644 --- a/src/common/event-rule/user-tracepoint.cpp +++ b/src/common/event-rule/user-tracepoint.cpp @@ -362,8 +362,7 @@ lttng_event_rule_user_tracepoint_generate_exclusions( goto end; } - exclusions = (lttng_event_exclusion *) zmalloc(sizeof(struct lttng_event_exclusion) + - (LTTNG_SYMBOL_NAME_LEN * nb_exclusions)); + exclusions = zmalloc(sizeof(struct lttng_event_exclusion) + (LTTNG_SYMBOL_NAME_LEN * nb_exclusions)); if (!exclusions) { PERROR("Failed to allocate exclusions buffer"); ret_status = LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OUT_OF_MEMORY; @@ -560,7 +559,7 @@ struct lttng_event_rule *lttng_event_rule_user_tracepoint_create(void) struct lttng_event_rule_user_tracepoint *tp_rule; enum lttng_event_rule_status status; - tp_rule = (lttng_event_rule_user_tracepoint *) zmalloc(sizeof(struct lttng_event_rule_user_tracepoint)); + tp_rule = zmalloc(); if (!tp_rule) { goto end; } diff --git a/src/common/event.cpp b/src/common/event.cpp index 358d05841..4844888a2 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -41,7 +41,7 @@ struct lttng_event *lttng_event_copy(const struct lttng_event *event) struct lttng_event *new_event; struct lttng_event_extended *new_event_extended; - new_event = (lttng_event *) zmalloc(sizeof(*event)); + new_event = zmalloc(); if (!new_event) { PERROR("Error allocating event structure"); goto end; @@ -54,7 +54,7 @@ struct lttng_event *lttng_event_copy(const struct lttng_event *event) * We need to create a new extended since the previous pointer is now * invalid. */ - new_event_extended = (lttng_event_extended *) zmalloc(sizeof(*new_event_extended)); + new_event_extended = zmalloc(); if (!new_event_extended) { PERROR("Error allocating event extended structure"); goto error; @@ -157,8 +157,7 @@ static ssize_t lttng_event_probe_attr_create_from_payload( comm = (typeof(comm)) comm_view.buffer.data; offset += sizeof(*comm); - local_attr = (struct lttng_event_probe_attr *) zmalloc( - sizeof(*local_attr)); + local_attr = zmalloc(); if (local_attr == NULL) { ret = -1; goto end; @@ -222,8 +221,7 @@ static ssize_t lttng_event_function_attr_create_from_payload( comm = (typeof(comm)) view->buffer.data; offset += sizeof(*comm); - local_attr = (struct lttng_event_function_attr *) zmalloc( - sizeof(*local_attr)); + local_attr = zmalloc(); if (local_attr == NULL) { ret = -1; goto end; @@ -272,12 +270,12 @@ static ssize_t lttng_event_exclusions_create_from_payload( struct lttng_event_exclusion **exclusions) { ssize_t ret, offset = 0; - size_t size = (count * LTTNG_SYMBOL_NAME_LEN); + const size_t size = (count * LTTNG_SYMBOL_NAME_LEN); uint32_t i; const struct lttng_event_exclusion_comm *comm; struct lttng_event_exclusion *local_exclusions; - local_exclusions = (struct lttng_event_exclusion *) zmalloc( + local_exclusions = zmalloc( sizeof(struct lttng_event_exclusion) + size); if (!local_exclusions) { ret = -1; @@ -510,8 +508,7 @@ deserialize_filter_expression: goto end; } - local_bytecode = (struct lttng_bytecode *) zmalloc( - event_comm->bytecode_len); + local_bytecode = zmalloc(event_comm->bytecode_len); if (!local_bytecode) { ret = -1; goto end; @@ -1078,8 +1075,7 @@ ssize_t lttng_event_context_create_from_payload( comm = (typeof(comm)) comm_view.data; offset += sizeof(*comm); - local_context = (struct lttng_event_context *) - zmalloc(sizeof(*local_context)); + local_context = zmalloc(); if (!local_context) { ret = -1; goto end; @@ -1331,8 +1327,7 @@ ssize_t lttng_event_field_create_from_payload( offset += sizeof(*comm); } - local_event_field = (struct lttng_event_field *) - zmalloc(sizeof(*local_event_field)); + local_event_field = zmalloc(); if (!local_event_field) { ret = -1; goto end; @@ -1713,8 +1708,7 @@ static enum lttng_error_code event_list_create_from_payload( ssize_t event_size; struct lttng_payload_view event_view = lttng_payload_view_from_view(view, offset, -1); - struct event_list_element *element = - (struct event_list_element *) zmalloc(sizeof(*element)); + struct event_list_element *element = zmalloc(); if (!element) { ret_code = LTTNG_ERR_NOMEM; @@ -1870,7 +1864,7 @@ static enum lttng_error_code event_field_list_create_from_payload( assert(view); assert(event_field_list); - list = (struct lttng_dynamic_pointer_array *) zmalloc(sizeof(*list)); + list = zmalloc(); if (!list) { ret_code = LTTNG_ERR_NOMEM; goto end; diff --git a/src/common/fd-handle.cpp b/src/common/fd-handle.cpp index 7473f0717..579757782 100644 --- a/src/common/fd-handle.cpp +++ b/src/common/fd-handle.cpp @@ -41,7 +41,7 @@ struct fd_handle *fd_handle_create(int fd) goto end; } - handle = (fd_handle *) zmalloc(sizeof(*handle)); + handle = zmalloc(); if (!handle) { PERROR("Failed to allocate fd_handle"); goto end; diff --git a/src/common/fd-tracker/fd-tracker.cpp b/src/common/fd-tracker/fd-tracker.cpp index 9c6f031b0..398ec971d 100644 --- a/src/common/fd-tracker/fd-tracker.cpp +++ b/src/common/fd-tracker/fd-tracker.cpp @@ -188,7 +188,7 @@ static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry) static struct unsuspendable_fd *unsuspendable_fd_create( const char *name, int fd) { - struct unsuspendable_fd *entry = (unsuspendable_fd *) zmalloc(sizeof(*entry)); + struct unsuspendable_fd *entry = zmalloc(); if (!entry) { goto error; @@ -365,7 +365,7 @@ end: struct fd_tracker *fd_tracker_create(const char *unlinked_file_path, unsigned int capacity) { - struct fd_tracker *tracker = (fd_tracker *) zmalloc(sizeof(struct fd_tracker)); + struct fd_tracker *tracker = zmalloc(); if (!tracker) { goto end; @@ -526,7 +526,7 @@ struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker, } } - handle = (fs_handle_tracked *) zmalloc(sizeof(*handle)); + handle = zmalloc(); if (!handle) { goto end; } @@ -620,7 +620,7 @@ int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker, unsigned int active_fds; struct unsuspendable_fd **entries; - entries = (unsuspendable_fd **) zmalloc(fd_count * sizeof(*entries)); + entries = calloc(fd_count); if (!entries) { ret = -1; goto end; @@ -719,7 +719,7 @@ int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker, * Maintain a local copy of fds_in as the user's callback may modify its * contents (e.g. setting the fd(s) to -1 after close). */ - fds = (int *) malloc(sizeof(*fds) * fd_count); + fds = malloc(sizeof(*fds) * fd_count); if (!fds) { ret = -1; goto end; diff --git a/src/common/fd-tracker/inode.cpp b/src/common/fd-tracker/inode.cpp index b5cdcf1b2..9c1133ea5 100644 --- a/src/common/fd-tracker/inode.cpp +++ b/src/common/fd-tracker/inode.cpp @@ -250,7 +250,7 @@ static void lttng_inode_get(struct lttng_inode *inode) struct lttng_unlinked_file_pool *lttng_unlinked_file_pool_create( const char *path) { - struct lttng_unlinked_file_pool *pool = (lttng_unlinked_file_pool *) zmalloc(sizeof(*pool)); + struct lttng_unlinked_file_pool *pool = zmalloc(); if (!pool) { goto error; @@ -434,7 +434,7 @@ static struct lttng_inode *lttng_inode_create(const struct inode_id *id, reference_acquired = lttng_directory_handle_get(directory_handle); LTTNG_ASSERT(reference_acquired); - inode = (lttng_inode *) zmalloc(sizeof(*inode)); + inode = zmalloc(); if (!inode) { goto end; } @@ -455,7 +455,7 @@ end: struct lttng_inode_registry *lttng_inode_registry_create(void) { - struct lttng_inode_registry *registry = (lttng_inode_registry *) zmalloc(sizeof(*registry)); + struct lttng_inode_registry *registry = zmalloc(); if (!registry) { goto end; diff --git a/src/common/filter.cpp b/src/common/filter.cpp index 2bff02031..f0b248e88 100644 --- a/src/common/filter.cpp +++ b/src/common/filter.cpp @@ -23,7 +23,7 @@ struct bytecode_symbol_iterator *bytecode_symbol_iterator_create( goto end; } - it = (bytecode_symbol_iterator *) zmalloc(sizeof(*it)); + it = zmalloc(); if (!it) { goto end; } diff --git a/src/common/filter/filter-parser.ypp b/src/common/filter/filter-parser.ypp index 89f6b43d9..0f11c234d 100644 --- a/src/common/filter/filter-parser.ypp +++ b/src/common/filter/filter-parser.ypp @@ -97,7 +97,7 @@ static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx, for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len; alloclen *= 2); - gstr = (gc_string *) zmalloc(alloclen); + gstr = zmalloc(alloclen); if (!gstr) { goto end; } @@ -154,7 +154,7 @@ static struct filter_node *make_node(struct filter_parser_ctx *scanner, struct filter_ast *ast = filter_parser_get_ast(scanner); struct filter_node *node; - node = (filter_node *) zmalloc(sizeof(*node)); + node = zmalloc(); if (!node) return NULL; memset(node, 0, sizeof(*node)); @@ -191,7 +191,7 @@ static struct filter_node *make_op_node(struct filter_parser_ctx *scanner, struct filter_ast *ast = filter_parser_get_ast(scanner); struct filter_node *node; - node = (filter_node *) zmalloc(sizeof(*node)); + node = zmalloc(); if (!node) return NULL; memset(node, 0, sizeof(*node)); @@ -227,7 +227,7 @@ static struct filter_ast *filter_ast_alloc(void) { struct filter_ast *ast; - ast = (filter_ast *) zmalloc(sizeof(*ast)); + ast = zmalloc(); if (!ast) return NULL; memset(ast, 0, sizeof(*ast)); @@ -257,7 +257,7 @@ struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input) yydebug = filter_parser_debug; - parser_ctx = (filter_parser_ctx *) zmalloc(sizeof(*parser_ctx)); + parser_ctx = zmalloc(); if (!parser_ctx) return NULL; memset(parser_ctx, 0, sizeof(*parser_ctx)); diff --git a/src/common/filter/filter-visitor-generate-ir.cpp b/src/common/filter/filter-visitor-generate-ir.cpp index a7236bd10..1bfe66c6f 100644 --- a/src/common/filter/filter-visitor-generate-ir.cpp +++ b/src/common/filter/filter-visitor-generate-ir.cpp @@ -31,7 +31,7 @@ struct ir_op *make_op_root(struct ir_op *child, enum ir_side side) { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; switch (child->data_type) { @@ -80,7 +80,7 @@ struct ir_op *make_op_load_string(const char *string, enum ir_side side) { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOAD; @@ -101,7 +101,7 @@ struct ir_op *make_op_load_numeric(int64_t v, enum ir_side side) { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOAD; @@ -118,7 +118,7 @@ struct ir_op *make_op_load_float(double v, enum ir_side side) { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOAD; @@ -192,12 +192,12 @@ struct ir_load_expression *create_load_expression(struct filter_node *node) node = load_expression_get_forward_chain(node); if (!node) return NULL; - load_exp = (ir_load_expression *) zmalloc(sizeof(struct ir_load_expression)); + load_exp = zmalloc(); if (!load_exp) return NULL; /* Root */ - load_exp_op = (ir_load_expression_op *) zmalloc(sizeof(struct ir_load_expression_op)); + load_exp_op = zmalloc(); if (!load_exp_op) goto error; load_exp->child = load_exp_op; @@ -229,7 +229,7 @@ struct ir_load_expression *create_load_expression(struct filter_node *node) struct filter_node *bracket_node; prev_op = load_exp_op; - load_exp_op = (ir_load_expression_op *) zmalloc(sizeof(struct ir_load_expression_op)); + load_exp_op = zmalloc(); if (!load_exp_op) goto error; prev_op->next = load_exp_op; @@ -248,7 +248,7 @@ struct ir_load_expression *create_load_expression(struct filter_node *node) fprintf(stderr, "[error] Expecting constant index in array expression\n"); goto error; } - load_exp_op = (ir_load_expression_op *) zmalloc(sizeof(struct ir_load_expression_op)); + load_exp_op = zmalloc(); if (!load_exp_op) goto error; prev_op->next = load_exp_op; @@ -263,7 +263,7 @@ struct ir_load_expression *create_load_expression(struct filter_node *node) } /* Add final load field */ prev_op = load_exp_op; - load_exp_op = (ir_load_expression_op *) zmalloc(sizeof(struct ir_load_expression_op)); + load_exp_op = zmalloc(); if (!load_exp_op) goto error; prev_op->next = load_exp_op; @@ -281,7 +281,7 @@ struct ir_op *make_op_load_expression(struct filter_node *node, { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOAD; @@ -312,7 +312,7 @@ struct ir_op *make_op_unary(enum unary_op_type unary_op_type, goto error; } - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_UNARY; @@ -380,7 +380,7 @@ struct ir_op *make_op_binary_compare(enum op_type bin_op_type, goto error; } - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_BINARY; @@ -461,7 +461,7 @@ struct ir_op *make_op_binary_logical(enum op_type bin_op_type, goto error; } - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOGICAL; @@ -505,7 +505,7 @@ struct ir_op *make_op_binary_bitwise(enum op_type bin_op_type, goto error; } - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_BINARY; diff --git a/src/common/hashtable/hashtable.cpp b/src/common/hashtable/hashtable.cpp index 6b87ff71f..1b40f91f1 100644 --- a/src/common/hashtable/hashtable.cpp +++ b/src/common/hashtable/hashtable.cpp @@ -110,7 +110,7 @@ struct lttng_ht *lttng_ht_new(unsigned long size, lttng_ht_type type) } pthread_mutex_unlock(&seed_lock); - ht = (lttng_ht *) zmalloc(sizeof(*ht)); + ht = zmalloc(); if (ht == NULL) { PERROR("zmalloc lttng_ht"); goto error; diff --git a/src/common/health/health.cpp b/src/common/health/health.cpp index 9d33ffd2f..8181eab93 100644 --- a/src/common/health/health.cpp +++ b/src/common/health/health.cpp @@ -67,11 +67,11 @@ struct health_app *health_app_create(int nr_types) { struct health_app *ha; - ha = (health_app *) zmalloc(sizeof(*ha)); + ha = zmalloc(); if (!ha) { return NULL; } - ha->flags = (health_flags *) zmalloc(sizeof(*ha->flags) * nr_types); + ha->flags = calloc(nr_types); if (!ha->flags) { goto error_flags; } diff --git a/src/common/index-allocator.cpp b/src/common/index-allocator.cpp index 7fe18eb18..1f7b3e052 100644 --- a/src/common/index-allocator.cpp +++ b/src/common/index-allocator.cpp @@ -32,7 +32,7 @@ struct lttng_index_allocator *lttng_index_allocator_create( { struct lttng_index_allocator *allocator = NULL; - allocator = (lttng_index_allocator *) zmalloc(sizeof(*allocator)); + allocator = zmalloc(); if (!allocator) { PERROR("Failed to allocate index allocator"); goto end; @@ -92,7 +92,7 @@ enum lttng_index_allocator_status lttng_index_allocator_release( LTTNG_ASSERT(idx < allocator->size); - index = (lttng_index *) zmalloc(sizeof(*index)); + index = zmalloc(); if (!index) { PERROR("Failed to allocate free index queue"); status = LTTNG_INDEX_ALLOCATOR_STATUS_ERROR; diff --git a/src/common/index/index.cpp b/src/common/index/index.cpp index 9d6895bae..de66ac4bd 100644 --- a/src/common/index/index.cpp +++ b/src/common/index/index.cpp @@ -45,7 +45,7 @@ static enum lttng_trace_chunk_status _lttng_index_file_create_from_trace_chunk( LTTNG_ASSERT(acquired_reference); - index_file = (lttng_index_file *) zmalloc(sizeof(*index_file)); + index_file = zmalloc(); if (!index_file) { PERROR("Failed to allocate lttng_index_file"); chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR; diff --git a/src/common/ini-config/ini-config.cpp b/src/common/ini-config/ini-config.cpp index 98308c9f3..0b9502fc4 100644 --- a/src/common/ini-config/ini-config.cpp +++ b/src/common/ini-config/ini-config.cpp @@ -144,7 +144,7 @@ int config_parse_value(const char *value) goto end; } - lower_str = (char *) zmalloc(len + 1); + lower_str = zmalloc(len + 1); if (!lower_str) { PERROR("zmalloc"); ret = -errno; diff --git a/src/common/ini-config/ini.cpp b/src/common/ini-config/ini.cpp index d8ec8cf3e..ec5a41119 100644 --- a/src/common/ini-config/ini.cpp +++ b/src/common/ini-config/ini.cpp @@ -109,7 +109,7 @@ int ini_parse_file(FILE* file, ini_entry_handler handler, void* user) int error = 0; #if !INI_USE_STACK - line = (char*)zmalloc(INI_MAX_LINE); + line = zmalloc(INI_MAX_LINE); if (!line) { return -2; } diff --git a/src/common/kernel-ctl/kernel-ctl.cpp b/src/common/kernel-ctl/kernel-ctl.cpp index 059ab4b0d..76960307c 100644 --- a/src/common/kernel-ctl/kernel-ctl.cpp +++ b/src/common/kernel-ctl/kernel-ctl.cpp @@ -181,7 +181,7 @@ int kernctl_syscall_mask(int fd, char **syscall_mask, uint32_t *nr_bits) array_alloc_len = lttng_align_ceil(kmask_len.len, 8) >> 3; - kmask = (lttng_kernel_abi_syscall_mask *) zmalloc(sizeof(*kmask) + array_alloc_len); + kmask = zmalloc(sizeof(*kmask) + array_alloc_len); if (!kmask) { ret = -1; goto end; @@ -519,7 +519,7 @@ int kernctl_capture(int fd, const struct lttng_bytecode *capture) struct lttng_kernel_abi_capture_bytecode *kb; /* Translate bytecode to kernel bytecode. */ - kb = (lttng_kernel_abi_capture_bytecode *) zmalloc(sizeof(*kb) + capture->len); + kb = zmalloc(sizeof(*kb) + capture->len); if (!kb) { ret = -ENOMEM; goto end; @@ -542,7 +542,7 @@ int kernctl_filter(int fd, const struct lttng_bytecode *filter) int ret; /* Translate bytecode to kernel bytecode */ - kb = (lttng_kernel_abi_filter_bytecode *) zmalloc(sizeof(*kb) + filter->len); + kb = zmalloc(sizeof(*kb) + filter->len); if (!kb) return -ENOMEM; kb->len = len = filter->len; diff --git a/src/common/kernel-probe.cpp b/src/common/kernel-probe.cpp index 979ca7311..172527f9a 100644 --- a/src/common/kernel-probe.cpp +++ b/src/common/kernel-probe.cpp @@ -117,7 +117,7 @@ lttng_kernel_probe_location_address_create(uint64_t address) struct lttng_kernel_probe_location *ret = NULL; struct lttng_kernel_probe_location_address *location; - location = (lttng_kernel_probe_location_address *) zmalloc(sizeof(*location)); + location = zmalloc(); if (!location) { PERROR("Error allocating userspace probe location."); goto end; @@ -154,7 +154,7 @@ lttng_kernel_probe_location_symbol_create(const char *symbol_name, goto error; } - location = (lttng_kernel_probe_location_symbol *) zmalloc(sizeof(*location)); + location = zmalloc(); if (!location) { PERROR("Failed to allocate kernel symbol probe location"); goto error; diff --git a/src/common/location.cpp b/src/common/location.cpp index 7e6ea79b0..65c041240 100644 --- a/src/common/location.cpp +++ b/src/common/location.cpp @@ -16,7 +16,7 @@ struct lttng_trace_archive_location *lttng_trace_archive_location_create( { struct lttng_trace_archive_location *location; - location = (lttng_trace_archive_location *) zmalloc(sizeof(*location)); + location = zmalloc(); if (!location) { goto end; } diff --git a/src/common/log-level-rule.cpp b/src/common/log-level-rule.cpp index 766e8b25b..4494b3196 100644 --- a/src/common/log-level-rule.cpp +++ b/src/common/log-level-rule.cpp @@ -44,7 +44,7 @@ struct lttng_log_level_rule *lttng_log_level_rule_exactly_create( { struct lttng_log_level_rule *rule = NULL; - rule = (lttng_log_level_rule *) zmalloc(sizeof(struct lttng_log_level_rule)); + rule = zmalloc(); if (!rule) { goto end; } @@ -77,7 +77,7 @@ lttng_log_level_rule_at_least_as_severe_as_create(int level) { struct lttng_log_level_rule *rule = NULL; - rule = (lttng_log_level_rule *) zmalloc(sizeof(struct lttng_log_level_rule)); + rule = zmalloc(); if (!rule) { goto end; } @@ -227,7 +227,7 @@ struct lttng_log_level_rule *lttng_log_level_rule_copy( LTTNG_ASSERT(source); - copy = (lttng_log_level_rule *) zmalloc(sizeof(struct lttng_log_level_rule)); + copy = zmalloc(); if (!copy) { goto end; } diff --git a/src/common/lttng-elf.cpp b/src/common/lttng-elf.cpp index 47d66936f..ba8d80bb5 100644 --- a/src/common/lttng-elf.cpp +++ b/src/common/lttng-elf.cpp @@ -394,7 +394,7 @@ end: * We found the length of the section name, now seek back to the * beginning of the name and copy it in the newly allocated buffer. */ - name = (char *)zmalloc(sizeof(char) * (name_length + 1)); /* + 1 for \0 */ + name = calloc((name_length + 1)); /* + 1 for \0 */ if (!name) { PERROR("Error allocating ELF section name buffer"); goto error; @@ -495,7 +495,7 @@ int lttng_elf_validate_and_populate(struct lttng_elf *elf) goto end; } - elf->ehdr = (lttng_elf_ehdr *) zmalloc(sizeof(struct lttng_elf_ehdr)); + elf->ehdr = zmalloc(); if (!elf->ehdr) { PERROR("Error allocation buffer for ELF header"); ret = LTTNG_ERR_NOMEM; @@ -548,7 +548,7 @@ struct lttng_elf *lttng_elf_create(int fd) goto error; } - elf = (lttng_elf *) zmalloc(sizeof(struct lttng_elf)); + elf = zmalloc(); if (!elf) { PERROR("Error allocating struct lttng_elf"); goto error; @@ -664,7 +664,7 @@ char *lttng_elf_get_section_data(struct lttng_elf *elf, max_alloc_size); goto error; } - data = (char *) zmalloc(shdr->sh_size); + data = calloc(shdr->sh_size); if (!data) { PERROR("Error allocating buffer for ELF section data"); goto error; diff --git a/src/common/macros.hpp b/src/common/macros.hpp index d2b78200e..49f163b42 100644 --- a/src/common/macros.hpp +++ b/src/common/macros.hpp @@ -9,10 +9,13 @@ #ifndef _MACROS_H #define _MACROS_H -#include +#include + #include +#include #include -#include + +#include /* * Takes a pointer x and transform it so we can use it to access members @@ -36,15 +39,111 @@ */ #define LTTNG_REF(x) ((typeof(*x) *)(x)) +#ifdef NDEBUG +/* +* Force usage of the assertion condition to prevent unused variable warnings +* when `assert()` are disabled by the `NDEBUG` definition. +*/ +# define LTTNG_ASSERT(_cond) ((void) sizeof((void) (_cond), 0)) +#else +# include +# define LTTNG_ASSERT(_cond) assert(_cond) +#endif + /* * Memory allocation zeroed */ + static inline -void *zmalloc(size_t len) +void *zmalloc_internal(size_t size) +{ + return calloc(1, size); +} + +template +struct can_malloc +{ + static constexpr bool value = std::is_trivially_constructible::value; +}; + +/* + * Malloc and zero-initialize an object of type T, asserting that T can be + * safely malloc-ed (is trivially constructible). + */ +template +T *zmalloc() +{ + static_assert (can_malloc::value, "type can be malloc'ed"); + return (T *) zmalloc_internal(sizeof(T)); +} + +/* + * Malloc and zero-initialize a buffer of size `size`, asserting that type T + * can be safely malloc-ed (is trivially constructible). + */ +template +T *zmalloc(size_t size) +{ + static_assert (can_malloc::value, "type can be malloc'ed"); + LTTNG_ASSERT(size >= sizeof(T)); + return (T *) zmalloc_internal(size); +} + +/* + * Malloc and zero-initialize an array of `nmemb` elements of type T, + * asserting that T can be safely malloc-ed (is trivially constructible). + */ +template +T *calloc(size_t nmemb) +{ + static_assert (can_malloc::value, "type can be malloc'ed"); + return (T *) zmalloc_internal(nmemb * sizeof(T)); +} + +/* + * Malloc an object of type T, asserting that T can be safely malloc-ed (is + * trivially constructible). + */ +template +T *malloc() { - return calloc(1, len); + static_assert (can_malloc::value, "type can be malloc'ed"); + return (T *) malloc(sizeof(T)); } +/* + * Malloc a buffer of size `size`, asserting that type T can be safely + * malloc-ed (is trivially constructible). + */ +template +T *malloc(size_t size) +{ + static_assert (can_malloc::value, "type can be malloc'ed"); + return (T *) malloc(size); +} + +/* + * Prevent using `free` on types that are non-POD. + * + * Declare a delete prototype of free if the parameter type is not safe to free + * (non-POD). + * + * If the parameter is a pointer to void, we can't check if what is pointed + * to is safe to free or not, as we don't know what is pointed to. Ideally, + * all calls to free would be with a typed pointer, but there are too many + * instances of passing a pointer to void to enforce that right now. So allow + * pointers to void, these will not be checked. + */ + +template +struct is_pod_or_void +{ + static constexpr bool value = std::is_pod::value || std::is_void::value; +}; + +template::value>::type> +void free(T *p) = delete; + #ifndef ARRAY_SIZE #define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0]))) #endif @@ -130,15 +229,4 @@ int lttng_strncpy(char *dst, const char *src, size_t dst_len) return 0; } -#ifdef NDEBUG -/* -* Force usage of the assertion condition to prevent unused variable warnings -* when `assert()` are disabled by the `NDEBUG` definition. -*/ -# define LTTNG_ASSERT(_cond) ((void) sizeof((void) (_cond), 0)) -#else -# include -# define LTTNG_ASSERT(_cond) assert(_cond) -#endif - #endif /* _MACROS_H */ diff --git a/src/common/mi-lttng.cpp b/src/common/mi-lttng.cpp index 2a1dfa57d..01e89b779 100644 --- a/src/common/mi-lttng.cpp +++ b/src/common/mi-lttng.cpp @@ -773,7 +773,7 @@ struct mi_writer *mi_lttng_writer_create(int fd_output, int mi_output_type) { struct mi_writer *mi_writer; - mi_writer = (struct mi_writer *) zmalloc(sizeof(struct mi_writer)); + mi_writer = zmalloc(); if (!mi_writer) { PERROR("zmalloc mi_writer_create"); goto end; diff --git a/src/common/notification.cpp b/src/common/notification.cpp index 248dd42e7..77e61f21a 100644 --- a/src/common/notification.cpp +++ b/src/common/notification.cpp @@ -24,7 +24,7 @@ struct lttng_notification *lttng_notification_create( goto end; } - notification = (lttng_notification *) zmalloc(sizeof(struct lttng_notification)); + notification = zmalloc(); if (!notification) { goto end; } diff --git a/src/common/path.cpp b/src/common/path.cpp index 1699cc5a6..61e78a5c1 100644 --- a/src/common/path.cpp +++ b/src/common/path.cpp @@ -70,7 +70,7 @@ char *utils_partial_realpath(const char *path) goto error; } - try_path_buf = (char *) zmalloc(LTTNG_PATH_MAX); + try_path_buf = zmalloc(LTTNG_PATH_MAX); if (!try_path_buf) { PERROR("zmalloc"); goto error; @@ -108,7 +108,7 @@ char *utils_partial_realpath(const char *path) } /* Allocate memory for the resolved path. */ - resolved_path = (char *) zmalloc(LTTNG_PATH_MAX); + resolved_path = zmalloc(LTTNG_PATH_MAX); if (resolved_path == NULL) { PERROR("zmalloc resolved path"); goto error; @@ -288,7 +288,7 @@ char *_utils_expand_path(const char *path, bool keep_symlink) } /* Allocate memory for the absolute_path */ - absolute_path = (char *) zmalloc(LTTNG_PATH_MAX); + absolute_path = zmalloc(LTTNG_PATH_MAX); if (absolute_path == NULL) { PERROR("zmalloc expand path"); goto error; diff --git a/src/common/pipe.cpp b/src/common/pipe.cpp index c2dce7881..8bf0c1ff3 100644 --- a/src/common/pipe.cpp +++ b/src/common/pipe.cpp @@ -108,7 +108,7 @@ static struct lttng_pipe *_pipe_create(void) int ret; struct lttng_pipe *p; - p = (lttng_pipe *) zmalloc(sizeof(*p)); + p = zmalloc(); if (!p) { PERROR("zmalloc pipe create"); goto end; diff --git a/src/common/relayd/relayd.cpp b/src/common/relayd/relayd.cpp index 28cab7bc5..5148bd03a 100644 --- a/src/common/relayd/relayd.cpp +++ b/src/common/relayd/relayd.cpp @@ -65,7 +65,7 @@ static int send_command(struct lttcomm_relayd_sock *rsock, buf_size += size; } - buf = (char *) zmalloc(buf_size); + buf = calloc(buf_size); if (buf == NULL) { PERROR("zmalloc relayd send command buf"); ret = -1; @@ -166,7 +166,7 @@ static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock, base_path_len = strlen(base_path) + 1; msg_length = sizeof(*msg) + session_name_len + hostname_len + base_path_len; - msg = (lttcomm_relayd_create_session_2_11 *) zmalloc(msg_length); + msg = zmalloc(msg_length); if (!msg) { PERROR("zmalloc create_session_2_11 command message"); ret = -1; @@ -446,7 +446,7 @@ static int relayd_add_stream_2_11(struct lttcomm_relayd_sock *rsock, pathname_len = strlen(pathname) + 1; msg_length = sizeof(*msg) + channel_name_len + pathname_len; - msg = (lttcomm_relayd_add_stream_2_11 *) zmalloc(msg_length); + msg = zmalloc(msg_length); if (!msg) { PERROR("zmalloc add_stream_2_11 command message"); ret = -1; diff --git a/src/common/runas.cpp b/src/common/runas.cpp index d5b53f2dc..6b1201deb 100644 --- a/src/common/runas.cpp +++ b/src/common/runas.cpp @@ -930,7 +930,7 @@ static int get_user_infos_from_uid( get_pw_buf_size = (size_t) raw_get_pw_buf_size; - buf = (char *) zmalloc(get_pw_buf_size); + buf = calloc(get_pw_buf_size); if (buf == NULL) { PERROR("Failed to allocate buffer to get password file entries"); goto error; @@ -1458,7 +1458,7 @@ int run_as_create_worker_no_lock(const char *procname, ret = 0; goto end; } - worker = (run_as_worker_data *) zmalloc(sizeof(*worker)); + worker = zmalloc(); if (!worker) { ret = -ENOMEM; goto end; @@ -1959,7 +1959,7 @@ int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name, } *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset; - *offsets = (uint64_t *) zmalloc(*num_offset * sizeof(uint64_t)); + *offsets = calloc(*num_offset); if (!*offsets) { ret = -ENOMEM; goto error; @@ -2001,7 +2001,7 @@ int run_as_generate_filter_bytecode(const char *filter_expression, view_bytecode = (const struct lttng_bytecode *) run_as_ret.u.generate_filter_bytecode.bytecode; - local_bytecode = (lttng_bytecode *) zmalloc(sizeof(*local_bytecode) + view_bytecode->len); + local_bytecode = calloc(view_bytecode->len); if (!local_bytecode) { ret = -ENOMEM; goto error; diff --git a/src/common/session-descriptor.cpp b/src/common/session-descriptor.cpp index 9867de83e..a3aa40b3e 100644 --- a/src/common/session-descriptor.cpp +++ b/src/common/session-descriptor.cpp @@ -71,7 +71,7 @@ struct lttng_uri *uri_copy(const struct lttng_uri *uri) goto end; } - new_uri = (lttng_uri *) zmalloc(sizeof(*new_uri)); + new_uri = zmalloc(); if (!new_uri) { goto end; } @@ -179,8 +179,8 @@ int network_location_set_from_uri_strings( * session descriptors expect individually allocated lttng_uris. */ if (uri_count == 2) { - control_uri = (lttng_uri *) zmalloc(sizeof(*control_uri)); - data_uri = (lttng_uri *) zmalloc(sizeof(*data_uri)); + control_uri = zmalloc(); + data_uri = zmalloc(); if (!control_uri || !data_uri) { ret = -1; goto end; @@ -208,7 +208,7 @@ lttng_session_descriptor_create(const char *name) { struct lttng_session_descriptor *descriptor; - descriptor = (lttng_session_descriptor *) zmalloc(sizeof(*descriptor)); + descriptor = zmalloc(); if (!descriptor) { goto error; } @@ -334,7 +334,7 @@ _lttng_session_descriptor_snapshot_create(const char *name) { struct lttng_session_descriptor_snapshot *descriptor; - descriptor = (lttng_session_descriptor_snapshot *) zmalloc(sizeof(*descriptor)); + descriptor = zmalloc(); if (!descriptor) { goto error; } @@ -477,7 +477,7 @@ _lttng_session_descriptor_live_create(const char *name, if (live_timer_interval_us == 0) { goto error; } - descriptor = (lttng_session_descriptor_live *) zmalloc(sizeof(*descriptor)); + descriptor = zmalloc(); if (!descriptor) { goto error; } @@ -709,7 +709,7 @@ skip_name: } uri = (typeof(uri)) current_view.data; - uris[i] = (lttng_uri *) zmalloc(sizeof(*uri)); + uris[i] = zmalloc(); if (!uris[i]) { ret = -1; goto end; diff --git a/src/common/sessiond-comm/sessiond-comm.cpp b/src/common/sessiond-comm/sessiond-comm.cpp index 7b20007a8..ed14a9e63 100644 --- a/src/common/sessiond-comm/sessiond-comm.cpp +++ b/src/common/sessiond-comm/sessiond-comm.cpp @@ -178,9 +178,8 @@ error: */ struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto) { - struct lttcomm_sock *sock; + struct lttcomm_sock *sock = zmalloc(); - sock = (lttcomm_sock *) zmalloc(sizeof(lttcomm_sock)); if (sock == NULL) { PERROR("zmalloc create sock"); goto end; @@ -366,11 +365,10 @@ struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock(struct lttng_uri *uri, { int ret; struct lttcomm_sock *tmp_sock = NULL; - struct lttcomm_relayd_sock *rsock = NULL; + struct lttcomm_relayd_sock *rsock = zmalloc(); LTTNG_ASSERT(uri); - rsock = (lttcomm_relayd_sock *) zmalloc(sizeof(*rsock)); if (!rsock) { PERROR("zmalloc relayd sock"); goto error; diff --git a/src/common/spawn-viewer.cpp b/src/common/spawn-viewer.cpp index fe1158713..288e3a2ae 100644 --- a/src/common/spawn-viewer.cpp +++ b/src/common/spawn-viewer.cpp @@ -85,7 +85,7 @@ static char **alloc_argv_from_user_opts(char *opts, const char *trace_path) } while (*token != '\0'); /* Add two here for the NULL terminating element and trace path */ - argv = (char **) zmalloc(sizeof(char *) * (num_opts + 2)); + argv = calloc(num_opts + 2); if (argv == NULL) { goto error; } @@ -126,7 +126,7 @@ static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len, const char *trace_path, bool opt_live_mode) { char **argv; - size_t size, mem_len; + size_t mem_len; /* Add one for the NULL terminating element. */ mem_len = opts_len + 1; @@ -138,10 +138,7 @@ static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len, mem_len += 1; } - size = sizeof(char *) * mem_len; - - /* Add two here for the trace_path and the NULL terminating element. */ - argv = (char **) zmalloc(size); + argv = calloc(mem_len); if (argv == NULL) { goto error; } diff --git a/src/common/string-utils/string-utils.cpp b/src/common/string-utils/string-utils.cpp index 5cfcef256..d172aeb4b 100644 --- a/src/common/string-utils/string-utils.cpp +++ b/src/common/string-utils/string-utils.cpp @@ -148,7 +148,7 @@ char *strutils_unescape_string(const char *input, char only_char) const char *i; LTTNG_ASSERT(input); - output = (char *) zmalloc(strlen(input) + 1); + output = calloc(strlen(input) + 1); if (!output) { goto end; } @@ -299,7 +299,7 @@ int strutils_split(const char *input, for (at = 0, s = input; at < number_of_substrings; at++) { const char *ss; char *d; - char *substring = (char *) zmalloc(longest_substring_len + 1); + char *substring = calloc(longest_substring_len + 1); if (!substring) { goto error; @@ -385,7 +385,7 @@ int strutils_append_str(char **s, const char *append) size_t oldlen = (old == NULL) ? 0 : strlen(old); size_t appendlen = strlen(append); - new_str = (char *) zmalloc(oldlen + appendlen + 1); + new_str = zmalloc(oldlen + appendlen + 1); if (!new_str) { return -ENOMEM; } @@ -415,7 +415,7 @@ int strutils_appendf(char **s, const char *fmt, ...) } /* Allocate space for old string + new string + \0. */ - new_str = (char *) zmalloc(oldlen + ret + 1); + new_str = zmalloc(oldlen + ret + 1); if (!new_str) { ret = -ENOMEM; goto end; diff --git a/src/common/trace-chunk.cpp b/src/common/trace-chunk.cpp index 2bf1a3962..2d12216d0 100644 --- a/src/common/trace-chunk.cpp +++ b/src/common/trace-chunk.cpp @@ -203,7 +203,7 @@ struct fs_handle *fs_handle_untracked_create( goto end; } - handle = (fs_handle_untracked *) zmalloc(sizeof(typeof(*handle))); + handle = zmalloc(); if (!handle) { PERROR("Failed to allocate untracked filesystem handle"); goto end; @@ -346,7 +346,7 @@ char *generate_chunk_name(uint64_t chunk_id, time_t creation_timestamp, goto error; } } - new_name = (char *) zmalloc(GENERATED_CHUNK_NAME_LEN); + new_name = calloc(GENERATED_CHUNK_NAME_LEN); if (!new_name) { ERR("Failed to allocate buffer for automatically-generated trace chunk name"); goto error; @@ -399,7 +399,7 @@ struct lttng_trace_chunk *lttng_trace_chunk_allocate(void) { struct lttng_trace_chunk *chunk = NULL; - chunk = (lttng_trace_chunk *) zmalloc(sizeof(*chunk)); + chunk = zmalloc(); if (!chunk) { ERR("Failed to allocate trace chunk"); goto end; @@ -1930,7 +1930,7 @@ struct lttng_trace_chunk_registry *lttng_trace_chunk_registry_create(void) { struct lttng_trace_chunk_registry *registry; - registry = (lttng_trace_chunk_registry *) zmalloc(sizeof(*registry)); + registry = zmalloc(); if (!registry) { goto end; } @@ -1966,7 +1966,7 @@ lttng_trace_chunk_registry_element_create_from_chunk( struct lttng_trace_chunk *chunk, uint64_t session_id) { struct lttng_trace_chunk_registry_element *element = - (lttng_trace_chunk_registry_element *) zmalloc(sizeof(*element)); + zmalloc(); if (!element) { goto end; diff --git a/src/common/tracker.cpp b/src/common/tracker.cpp index 6bb84d0ca..181a0a8c9 100644 --- a/src/common/tracker.cpp +++ b/src/common/tracker.cpp @@ -71,7 +71,7 @@ enum lttng_error_code process_attr_value_from_comm( { char *name = NULL; enum lttng_error_code ret = LTTNG_OK; - struct process_attr_value *value = (process_attr_value *) zmalloc(sizeof(*value)); + struct process_attr_value *value = zmalloc(); if (!value) { ret = LTTNG_ERR_NOMEM; @@ -214,7 +214,7 @@ static void process_attr_tracker_value_destructor(void *ptr) struct lttng_process_attr_values *lttng_process_attr_values_create(void) { - struct lttng_process_attr_values *values = (lttng_process_attr_values *) zmalloc(sizeof(*values)); + struct lttng_process_attr_values *values = zmalloc(); if (!values) { goto end; @@ -426,7 +426,7 @@ struct process_attr_value *process_attr_value_copy( goto end; } - new_value = (process_attr_value *) zmalloc(sizeof(*new_value)); + new_value = zmalloc(); if (!new_value) { goto end; } diff --git a/src/common/trigger.cpp b/src/common/trigger.cpp index be2c49b7c..8e6f0734e 100644 --- a/src/common/trigger.cpp +++ b/src/common/trigger.cpp @@ -55,7 +55,7 @@ struct lttng_trigger *lttng_trigger_create( goto end; } - trigger = (lttng_trigger *) zmalloc(sizeof(struct lttng_trigger)); + trigger = zmalloc(); if (!trigger) { goto end; } @@ -505,7 +505,7 @@ struct lttng_triggers *lttng_triggers_create(void) { struct lttng_triggers *triggers = NULL; - triggers = (lttng_triggers *) zmalloc(sizeof(*triggers)); + triggers = zmalloc(); if (!triggers) { goto end; } diff --git a/src/common/uri.cpp b/src/common/uri.cpp index b15fe25db..8e663c46a 100644 --- a/src/common/uri.cpp +++ b/src/common/uri.cpp @@ -306,7 +306,7 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris) } /* Allocate URI array */ - tmp_uris = (lttng_uri *) zmalloc(sizeof(struct lttng_uri) * size); + tmp_uris = calloc(size); if (tmp_uris == NULL) { PERROR("zmalloc uri"); goto error; @@ -634,7 +634,7 @@ ssize_t uri_parse_str_urls(const char *ctrl_url, const char *data_url, goto error; } - tmp_uris = (lttng_uri *) zmalloc(sizeof(struct lttng_uri) * uri_count); + tmp_uris = calloc(uri_count); if (tmp_uris == NULL) { PERROR("zmalloc uris"); goto error; diff --git a/src/common/userspace-probe.cpp b/src/common/userspace-probe.cpp index 7eb4a6d91..99893b643 100644 --- a/src/common/userspace-probe.cpp +++ b/src/common/userspace-probe.cpp @@ -72,7 +72,7 @@ lttng_userspace_probe_location_lookup_method_function_elf_create(void) struct lttng_userspace_probe_location_lookup_method *ret = NULL; struct lttng_userspace_probe_location_lookup_method_elf *elf_method; - elf_method = (lttng_userspace_probe_location_lookup_method_elf *) zmalloc(sizeof(*elf_method)); + elf_method = zmalloc(); if (!elf_method) { PERROR("zmalloc"); goto end; @@ -90,7 +90,7 @@ lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create(void) struct lttng_userspace_probe_location_lookup_method *ret = NULL; struct lttng_userspace_probe_location_lookup_method_sdt *sdt_method; - sdt_method = (lttng_userspace_probe_location_lookup_method_sdt *) zmalloc(sizeof(*sdt_method)); + sdt_method = zmalloc(); if (!sdt_method) { PERROR("zmalloc"); goto end; @@ -311,7 +311,7 @@ lttng_userspace_probe_location_function_create_no_check(const char *binary_path, goto error; } - location = (lttng_userspace_probe_location_function *) zmalloc(sizeof(*location)); + location = zmalloc(); if (!location) { PERROR("Error allocating userspace probe location"); goto error; @@ -448,7 +448,7 @@ lttng_userspace_probe_location_tracepoint_create_no_check(const char *binary_pat goto error; } - location = (lttng_userspace_probe_location_tracepoint *) zmalloc(sizeof(*location)); + location = zmalloc(); if (!location) { PERROR("zmalloc"); goto error; @@ -547,7 +547,7 @@ lttng_userspace_probe_location_lookup_method_function_elf_copy( LTTNG_ASSERT(lookup_method->type == LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF); - elf_method = (lttng_userspace_probe_location_lookup_method_elf *) zmalloc(sizeof(*elf_method)); + elf_method = zmalloc(); if (!elf_method) { PERROR("Error allocating ELF userspace probe lookup method"); goto error; @@ -574,7 +574,7 @@ lttng_userspace_probe_location_lookup_method_tracepoint_sdt_copy( LTTNG_ASSERT(lookup_method->type == LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT); - sdt_method = (lttng_userspace_probe_location_lookup_method_sdt *) zmalloc(sizeof(*sdt_method)); + sdt_method = zmalloc(); if (!sdt_method) { PERROR("zmalloc"); goto error; diff --git a/src/common/ust-consumer/ust-consumer.cpp b/src/common/ust-consumer/ust-consumer.cpp index 5e9a442eb..766c9d30c 100644 --- a/src/common/ust-consumer/ust-consumer.cpp +++ b/src/common/ust-consumer/ust-consumer.cpp @@ -407,7 +407,7 @@ static int create_ust_channel(struct lttng_consumer_channel *channel, nr_stream_fds = 1; else nr_stream_fds = lttng_ust_ctl_get_nr_stream_per_channel(); - stream_fds = (int *) zmalloc(nr_stream_fds * sizeof(*stream_fds)); + stream_fds = calloc(nr_stream_fds); if (!stream_fds) { ret = -1; goto error_alloc; @@ -1286,7 +1286,7 @@ int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset, DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len); - metadata_str = (char *) zmalloc(len * sizeof(char)); + metadata_str = calloc(len); if (!metadata_str) { PERROR("zmalloc metadata string"); ret_code = LTTCOMM_CONSUMERD_ENOMEM; diff --git a/src/common/utils.cpp b/src/common/utils.cpp index eb6b00143..b4b7f749f 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -171,9 +171,8 @@ void utils_close_pipe(int *src) */ char *utils_strdupdelim(const char *begin, const char *end) { - char *str; + char *str = zmalloc(end - begin + 1); - str = (char *) zmalloc(end - begin + 1); if (str == NULL) { PERROR("zmalloc strdupdelim"); goto error; @@ -784,7 +783,7 @@ char *utils_get_user_home_dir(uid_t uid) goto end; } retry: - buf = (char *) zmalloc(buflen); + buf = zmalloc(buflen); if (!buf) { goto end; } @@ -934,7 +933,7 @@ char *utils_generate_optstring(const struct option *long_options, string_len += long_options[i].has_arg ? 1 : 0; } - optstring = (char *) zmalloc(string_len); + optstring = zmalloc(string_len); if (!optstring) { goto end; } @@ -1159,7 +1158,7 @@ enum lttng_error_code utils_user_id_from_name(const char *user_name, uid_t *uid) buflen = FALLBACK_USER_BUFLEN; } - buf = (char *) zmalloc(buflen); + buf = zmalloc(buflen); if (!buf) { ret_val = LTTNG_ERR_NOMEM; goto end; @@ -1173,7 +1172,7 @@ enum lttng_error_code utils_user_id_from_name(const char *user_name, uid_t *uid) case ERANGE: buflen *= 2; free(buf); - buf = (char *) zmalloc(buflen); + buf = zmalloc(buflen); if (!buf) { ret_val = LTTNG_ERR_NOMEM; goto end; @@ -1224,7 +1223,7 @@ enum lttng_error_code utils_group_id_from_name( buflen = FALLBACK_GROUP_BUFLEN; } - buf = (char *) zmalloc(buflen); + buf = zmalloc(buflen); if (!buf) { ret_val = LTTNG_ERR_NOMEM; goto end; @@ -1238,7 +1237,7 @@ enum lttng_error_code utils_group_id_from_name( case ERANGE: buflen *= 2; free(buf); - buf = (char *) zmalloc(buflen); + buf = zmalloc(buflen); if (!buf) { ret_val = LTTNG_ERR_NOMEM; goto end; diff --git a/src/lib/lttng-ctl/channel.cpp b/src/lib/lttng-ctl/channel.cpp index 080dbb443..3f3c53996 100644 --- a/src/lib/lttng-ctl/channel.cpp +++ b/src/lib/lttng-ctl/channel.cpp @@ -143,12 +143,12 @@ struct lttng_notification_channel *lttng_notification_channel_create( goto end; } - sock_path = (char *) zmalloc(LTTNG_PATH_MAX); + sock_path = calloc(LTTNG_PATH_MAX); if (!sock_path) { goto end; } - channel = (lttng_notification_channel *) zmalloc(sizeof(struct lttng_notification_channel)); + channel = zmalloc(); if (!channel) { goto end; } @@ -343,7 +343,7 @@ int enqueue_dropped_notification( goto end; } - pending_notification = (struct pending_notification *) zmalloc(sizeof(*pending_notification)); + pending_notification = zmalloc(); if (!pending_notification) { ret = -1; goto end; @@ -371,7 +371,7 @@ int enqueue_notification_from_current_message( goto end; } - pending_notification = (struct pending_notification *) zmalloc(sizeof(*pending_notification)); + pending_notification = zmalloc(); if (!pending_notification) { ret = -1; goto error; diff --git a/src/lib/lttng-ctl/clear.cpp b/src/lib/lttng-ctl/clear.cpp index 114758b0c..b7a312be5 100644 --- a/src/lib/lttng-ctl/clear.cpp +++ b/src/lib/lttng-ctl/clear.cpp @@ -64,7 +64,7 @@ static struct lttng_clear_handle *lttng_clear_handle_create(int sessiond_socket) { int ret; - struct lttng_clear_handle *handle = (lttng_clear_handle *) zmalloc(sizeof(*handle)); + struct lttng_clear_handle *handle = zmalloc(); if (!handle) { goto end; diff --git a/src/lib/lttng-ctl/destruction-handle.cpp b/src/lib/lttng-ctl/destruction-handle.cpp index d937b2c73..3882a587e 100644 --- a/src/lib/lttng-ctl/destruction-handle.cpp +++ b/src/lib/lttng-ctl/destruction-handle.cpp @@ -69,7 +69,7 @@ struct lttng_destruction_handle *lttng_destruction_handle_create( int sessiond_socket) { int ret; - struct lttng_destruction_handle *handle = (lttng_destruction_handle *) zmalloc(sizeof(*handle)); + struct lttng_destruction_handle *handle = zmalloc(); if (!handle) { goto end; diff --git a/src/lib/lttng-ctl/event.cpp b/src/lib/lttng-ctl/event.cpp index e89aae482..c33adf119 100644 --- a/src/lib/lttng-ctl/event.cpp +++ b/src/lib/lttng-ctl/event.cpp @@ -24,13 +24,13 @@ struct lttng_event *lttng_event_create(void) struct lttng_event *event; struct lttng_event_extended *event_extended; - event = (lttng_event *) zmalloc(sizeof(*event)); + event = zmalloc(); if (!event) { PERROR("Error allocating event structure"); goto end; } - event_extended = (lttng_event_extended *) zmalloc(sizeof(*event_extended)); + event_extended = zmalloc(); if (!event_extended) { PERROR("Error allocating event extended structure"); goto error; diff --git a/src/lib/lttng-ctl/load.cpp b/src/lib/lttng-ctl/load.cpp index 4a07f4212..02e2a6ff9 100644 --- a/src/lib/lttng-ctl/load.cpp +++ b/src/lib/lttng-ctl/load.cpp @@ -22,7 +22,7 @@ struct lttng_load_session_attr *lttng_load_session_attr_create(void) { - return (lttng_load_session_attr *) zmalloc(sizeof(struct lttng_load_session_attr)); + return zmalloc(); } static @@ -272,8 +272,7 @@ int lttng_load_session_attr_set_override_ctrl_url( } if (!attr->override_attr) { - attr->override_attr = (config_load_session_override_attr *) zmalloc( - sizeof(struct config_load_session_override_attr)); + attr->override_attr = zmalloc(); if (!attr->override_attr) { ret = -LTTNG_ERR_NOMEM; goto end; @@ -303,7 +302,7 @@ int lttng_load_session_attr_set_override_ctrl_url( uri[0].port = DEFAULT_NETWORK_CONTROL_PORT; } - url_str = (char *) zmalloc(PATH_MAX); + url_str = calloc(PATH_MAX); if (!url_str) { /* FIXME: return valid error */ ret = -LTTNG_ERR_NOMEM; @@ -357,8 +356,7 @@ int lttng_load_session_attr_set_override_data_url( } if (!attr->override_attr) { - attr->override_attr = (config_load_session_override_attr *) zmalloc( - sizeof(struct config_load_session_override_attr)); + attr->override_attr = zmalloc(); if (!attr->override_attr) { ret = -LTTNG_ERR_NOMEM; goto end; @@ -388,7 +386,7 @@ int lttng_load_session_attr_set_override_data_url( uri[0].port = DEFAULT_NETWORK_DATA_PORT; } - url_str = (char *) zmalloc(PATH_MAX); + url_str = calloc(PATH_MAX); if (!url_str) { ret = -LTTNG_ERR_NOMEM; goto end; @@ -446,8 +444,7 @@ int lttng_load_session_attr_set_override_url( } if (!attr->override_attr) { - attr->override_attr = (config_load_session_override_attr *) zmalloc( - sizeof(struct config_load_session_override_attr)); + attr->override_attr = zmalloc(); if (!attr->override_attr) { ret = -LTTNG_ERR_NOMEM; goto end; @@ -583,8 +580,7 @@ int lttng_load_session_attr_set_override_session_name( } if (!attr->override_attr) { - attr->override_attr = (config_load_session_override_attr *) zmalloc( - sizeof(struct config_load_session_override_attr)); + attr->override_attr = zmalloc(); if (!attr->override_attr) { ret = -LTTNG_ERR_NOMEM; goto end; diff --git a/src/lib/lttng-ctl/lttng-ctl-health.cpp b/src/lib/lttng-ctl/lttng-ctl-health.cpp index 4821548e5..e3234d5d4 100644 --- a/src/lib/lttng-ctl/lttng-ctl-health.cpp +++ b/src/lib/lttng-ctl/lttng-ctl-health.cpp @@ -231,7 +231,7 @@ struct lttng_health *lttng_health_create(enum health_component hc, struct lttng_health *lh; int i; - lh = (lttng_health *) zmalloc(sizeof(*lh) + sizeof(lh->thread[0]) * nr_threads); + lh = zmalloc(sizeof(*lh) + sizeof(lh->thread[0]) * nr_threads); if (!lh) { return NULL; } diff --git a/src/lib/lttng-ctl/lttng-ctl.cpp b/src/lib/lttng-ctl/lttng-ctl.cpp index c5d47ebdc..057a91e3e 100644 --- a/src/lib/lttng-ctl/lttng-ctl.cpp +++ b/src/lib/lttng-ctl/lttng-ctl.cpp @@ -266,7 +266,7 @@ int lttng_check_tracing_group(void) } /* Alloc group list of the right size */ - grp_list = (gid_t *) zmalloc(grp_list_size * sizeof(gid_t)); + grp_list = calloc(grp_list_size); if (!grp_list) { PERROR("malloc"); goto end; @@ -500,7 +500,7 @@ static int recv_sessiond_optional_data(size_t len, void **user_buf, size_t *user_len) { int ret = 0; - void *buf = NULL; + char *buf = NULL; if (len) { if (!user_len) { @@ -508,7 +508,7 @@ static int recv_sessiond_optional_data(size_t len, void **user_buf, goto end; } - buf = zmalloc(len); + buf = zmalloc(len); if (!buf) { ret = -ENOMEM; goto end; @@ -729,7 +729,7 @@ struct lttng_handle *lttng_create_handle(const char *session_name, int ret; struct lttng_handle *handle = NULL; - handle = (lttng_handle *) zmalloc(sizeof(struct lttng_handle)); + handle = zmalloc(); if (handle == NULL) { PERROR("malloc handle"); goto end; @@ -1576,13 +1576,13 @@ int lttng_enable_channel(struct lttng_handle *handle, /* Populate the channel extended attribute if necessary. */ if (!channel->attr.extended.ptr) { struct lttng_channel_extended *extended = - (struct lttng_channel_extended *) zmalloc( - sizeof(*extended)); + zmalloc(); if (!extended) { ret = -LTTNG_ERR_NOMEM; goto end; } + lttng_channel_set_default_extended_attr( &handle->domain, extended); channel->attr.extended.ptr = extended; diff --git a/src/lib/lttng-ctl/rotate.cpp b/src/lib/lttng-ctl/rotate.cpp index b6a66392a..106b8fb48 100644 --- a/src/lib/lttng-ctl/rotate.cpp +++ b/src/lib/lttng-ctl/rotate.cpp @@ -230,7 +230,7 @@ int lttng_rotate_session(const char *session_name, goto end; } - *rotation_handle = (lttng_rotation_handle *) zmalloc(sizeof(struct lttng_rotation_handle)); + *rotation_handle = zmalloc(); if (!*rotation_handle) { ret = -LTTNG_ERR_NOMEM; goto end; @@ -350,7 +350,7 @@ end: static struct lttng_rotation_schedules *lttng_rotation_schedules_create(void) { - return (lttng_rotation_schedules *) zmalloc(sizeof(struct lttng_rotation_schedules)); + return zmalloc(); } static @@ -463,7 +463,7 @@ lttng_rotation_schedule_size_threshold_create(void) { struct lttng_rotation_schedule_size_threshold *schedule; - schedule = (lttng_rotation_schedule_size_threshold *) zmalloc(sizeof(*schedule)); + schedule = zmalloc(); if (!schedule) { goto end; } @@ -529,7 +529,7 @@ lttng_rotation_schedule_periodic_create(void) { struct lttng_rotation_schedule_periodic *schedule; - schedule = (lttng_rotation_schedule_periodic *) zmalloc(sizeof(*schedule)); + schedule = zmalloc(); if (!schedule) { goto end; } diff --git a/src/lib/lttng-ctl/save.cpp b/src/lib/lttng-ctl/save.cpp index 7843df300..32fcb1c2d 100644 --- a/src/lib/lttng-ctl/save.cpp +++ b/src/lib/lttng-ctl/save.cpp @@ -17,7 +17,7 @@ struct lttng_save_session_attr *lttng_save_session_attr_create(void) { - return (lttng_save_session_attr *) zmalloc(sizeof(struct lttng_save_session_attr)); + return zmalloc(); } void lttng_save_session_attr_destroy(struct lttng_save_session_attr *output) diff --git a/src/lib/lttng-ctl/snapshot.cpp b/src/lib/lttng-ctl/snapshot.cpp index b9bac9aa8..baec7357f 100644 --- a/src/lib/lttng-ctl/snapshot.cpp +++ b/src/lib/lttng-ctl/snapshot.cpp @@ -120,7 +120,7 @@ int lttng_snapshot_list_output(const char *session_name, goto error; } - new_list = (lttng_snapshot_output_list *) zmalloc(sizeof(*new_list)); + new_list = zmalloc(); if (!new_list) { ret = -LTTNG_ERR_NOMEM; goto error; @@ -240,7 +240,7 @@ struct lttng_snapshot_output *lttng_snapshot_output_create(void) { struct lttng_snapshot_output *output; - output = (lttng_snapshot_output *) zmalloc(sizeof(struct lttng_snapshot_output)); + output = zmalloc(); if (!output) { goto error; } diff --git a/src/lib/lttng-ctl/tracker.cpp b/src/lib/lttng-ctl/tracker.cpp index 53deced1d..4930e94ce 100644 --- a/src/lib/lttng-ctl/tracker.cpp +++ b/src/lib/lttng-ctl/tracker.cpp @@ -53,7 +53,7 @@ enum lttng_error_code lttng_session_get_tracker_handle(const char *session_name, goto error; } - handle = (lttng_process_attr_tracker_handle *) zmalloc(sizeof(*handle)); + handle = zmalloc(); if (!handle) { ret_code = LTTNG_ERR_NOMEM; goto error; @@ -779,7 +779,7 @@ int lttng_list_tracker_pids(struct lttng_handle *handle, goto end; } - pid_array = (int32_t *) zmalloc(pid_count * sizeof(int32_t)); + pid_array = calloc(pid_count); if (!pid_array) { ret_code = LTTNG_ERR_NOMEM; goto end; diff --git a/tests/regression/tools/live/live_test.cpp b/tests/regression/tools/live/live_test.cpp index 439d862d4..2da5b6fe4 100644 --- a/tests/regression/tools/live/live_test.cpp +++ b/tests/regression/tools/live/live_test.cpp @@ -285,7 +285,7 @@ int attach_session(uint64_t id) int i; ssize_t ret_len; - session = (live_session *) zmalloc(sizeof(struct live_session)); + session = zmalloc(); if (!session) { goto error; } @@ -327,8 +327,7 @@ int attach_session(uint64_t id) diag("Got session stream count == 0"); goto error; } - session->streams = (viewer_stream *) zmalloc(session->stream_count * - sizeof(struct viewer_stream)); + session->streams = calloc(session->stream_count); if (!session->streams) { goto error; } @@ -436,7 +435,7 @@ retry: goto error; } - data = (char *) zmalloc(len); + data = calloc(len); if (!data) { PERROR("relay data zmalloc"); goto error; diff --git a/tests/unit/test_ust_data.cpp b/tests/unit/test_ust_data.cpp index 9cfdac4f3..35d0c82eb 100644 --- a/tests/unit/test_ust_data.cpp +++ b/tests/unit/test_ust_data.cpp @@ -165,7 +165,7 @@ static void test_create_ust_event_exclusion(void) ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; /* set up an exclusion set */ - exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) + + exclusion = zmalloc(sizeof(*exclusion) + LTTNG_SYMBOL_NAME_LEN * exclusion_count); ok(exclusion != NULL, "Create UST exclusion"); if (!exclusion) { @@ -185,7 +185,7 @@ static void test_create_ust_event_exclusion(void) ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails"); - exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) + + exclusion = zmalloc(sizeof(*exclusion) + LTTNG_SYMBOL_NAME_LEN * exclusion_count); ok(exclusion != NULL, "Create UST exclusion"); if (!exclusion) { @@ -193,7 +193,7 @@ static void test_create_ust_event_exclusion(void) goto end; } - exclusion_copy = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) + + exclusion_copy = zmalloc(sizeof(*exclusion) + LTTNG_SYMBOL_NAME_LEN * exclusion_count); if (!exclusion_copy) { skip(2, "zmalloc failed"); diff --git a/tests/unit/test_utils_expand_path.cpp b/tests/unit/test_utils_expand_path.cpp index 3cb1a8160..d11c2d919 100644 --- a/tests/unit/test_utils_expand_path.cpp +++ b/tests/unit/test_utils_expand_path.cpp @@ -132,14 +132,14 @@ static int prepare_valid_results(void) } /* allocate memory for the expected results */ - valid_tests_expected_results = (char **) zmalloc(sizeof(char *) * num_valid_tests); + valid_tests_expected_results = calloc(num_valid_tests); if (!valid_tests_expected_results) { PRINT_ERR("out of memory"); ret = -1; goto end; } for (i = 0; i < num_valid_tests; i++) { - valid_tests_expected_results[i] = (char *) malloc(PATH_MAX); + valid_tests_expected_results[i] = calloc(PATH_MAX); if (valid_tests_expected_results[i] == NULL) { PRINT_ERR("malloc expected results"); ret = -1; -- 2.34.1