From 1f3655ba2df7ab6fef67cc97b72c0eafacb4fe39 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 21 Jan 2020 16:26:51 +0100 Subject: [PATCH] refactor alloc() / free() to new / delete --- common | 2 +- lib/formats/protobuf.cpp | 14 +++++++------- lib/io.cpp | 14 +++++++------- lib/kernel/if.cpp | 2 +- lib/kernel/tc_netem.cpp | 2 +- lib/mapping.cpp | 2 +- lib/memory.cpp | 2 +- lib/memory/heap.cpp | 4 ++-- lib/memory/ib.cpp | 4 ++-- lib/memory/managed.cpp | 2 +- lib/memory/mmap.cpp | 4 ++-- lib/node.cpp | 4 ++-- lib/nodes/comedi.cpp | 8 ++++---- lib/nodes/file.cpp | 4 ++-- lib/nodes/iec61850.cpp | 2 +- lib/nodes/infiniband.cpp | 2 +- lib/nodes/ngsi.cpp | 2 +- lib/nodes/opal.cpp | 16 ++++++++-------- lib/nodes/shmem.cpp | 2 +- lib/nodes/signal_generator.cpp | 6 +++--- lib/nodes/socket.cpp | 13 ++++++++----- lib/nodes/stats.cpp | 2 +- lib/nodes/test_rtt.cpp | 4 ++-- lib/nodes/uldaq.cpp | 9 ++++++--- lib/nodes/websocket.cpp | 8 ++++---- lib/path.cpp | 10 +++++----- lib/sample.cpp | 4 ++-- lib/signal.cpp | 8 ++++---- lib/socket_addr.cpp | 2 +- lib/super_node.cpp | 4 ++-- 30 files changed, 84 insertions(+), 78 deletions(-) diff --git a/common b/common index c31062d3f..051bab817 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit c31062d3f5ddc09d592ee55e10693093cbb67143 +Subproject commit 051bab81736c187bcc42b561ebeed6f85b1c8d5c diff --git a/lib/formats/protobuf.cpp b/lib/formats/protobuf.cpp index 5619d1e38..668a607b0 100644 --- a/lib/formats/protobuf.cpp +++ b/lib/formats/protobuf.cpp @@ -56,14 +56,14 @@ int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct { unsigned psz; - Villas__Node__Message *pb_msg = (Villas__Node__Message *) alloc(sizeof(Villas__Node__Message)); + auto *pb_msg = new Villas__Node__Message; villas__node__message__init(pb_msg); pb_msg->n_samples = cnt; - pb_msg->samples = (Villas__Node__Sample **) alloc(pb_msg->n_samples * sizeof(Villas__Node__Sample *)); + pb_msg->samples = new Villas__Node__Sample*[pb_msg->n_samples]; for (unsigned i = 0; i < pb_msg->n_samples; i++) { - Villas__Node__Sample *pb_smp = pb_msg->samples[i] = (Villas__Node__Sample *) alloc(sizeof(Villas__Node__Sample)); + Villas__Node__Sample *pb_smp = pb_msg->samples[i] = new Villas__Node__Sample; villas__node__sample__init(pb_smp); struct sample *smp = smps[i]; @@ -76,7 +76,7 @@ int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct } if (io->flags & smp->flags & (int) SampleFlags::HAS_TS_ORIGIN) { - pb_smp->timestamp = (Villas__Node__Timestamp *) alloc(sizeof(Villas__Node__Timestamp)); + pb_smp->timestamp = new Villas__Node__Timestamp; villas__node__timestamp__init(pb_smp->timestamp); pb_smp->timestamp->sec = smp->ts.origin.tv_sec; @@ -84,10 +84,10 @@ int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct } pb_smp->n_values = smp->length; - pb_smp->values = (Villas__Node__Value **) alloc(pb_smp->n_values * sizeof(Villas__Node__Value *)); + pb_smp->values = new Villas__Node__Value*[pb_smp->n_values]; for (unsigned j = 0; j < pb_smp->n_values; j++) { - Villas__Node__Value *pb_val = pb_smp->values[j] = (Villas__Node__Value *) alloc(sizeof(Villas__Node__Value)); + Villas__Node__Value *pb_val = pb_smp->values[j] = new Villas__Node__Value; villas__node__value__init(pb_val); enum SignalType fmt = sample_format(smp, j); @@ -109,7 +109,7 @@ int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct case SignalType::COMPLEX: pb_val->value_case = VILLAS__NODE__VALUE__VALUE_Z; - pb_val->z = (Villas__Node__Complex *) alloc(sizeof(Villas__Node__Complex)); + pb_val->z = new Villas__Node__Complex; villas__node__complex__init(pb_val->z); diff --git a/lib/io.cpp b/lib/io.cpp index 665b37fcf..c90666969 100644 --- a/lib/io.cpp +++ b/lib/io.cpp @@ -90,7 +90,7 @@ int io_init(struct io *io, const struct format_type *fmt, struct vlist *signals, assert(io->state == State::DESTROYED); io->_vt = fmt; - io->_vd = alloc(fmt->size); + io->_vd = new char[fmt->size]; io->flags = flags | (io_type(io)->flags & ~(int) SampleFlags::HAS_ALL); io->delimiter = io_type(io)->delimiter ? io_type(io)->delimiter : '\n'; @@ -99,8 +99,8 @@ int io_init(struct io *io, const struct format_type *fmt, struct vlist *signals, io->in.buflen = io->out.buflen = 4096; - io->in.buffer = (char *) alloc(io->in.buflen); - io->out.buffer = (char *) alloc(io->out.buflen); + io->in.buffer = new char[io->in.buflen]; + io->out.buffer = new char[io->out.buflen]; io->signals = signals; @@ -118,7 +118,7 @@ int io_init2(struct io *io, const struct format_type *fmt, const char *dt, int f int ret; struct vlist *signals; - signals = (struct vlist *) alloc(sizeof(struct vlist)); + signals = new struct vlist; signals->state = State::DESTROYED; ret = vlist_init(signals); @@ -144,9 +144,9 @@ int io_destroy(struct io *io) if (ret) return ret; - free(io->_vd); - free(io->in.buffer); - free(io->out.buffer); + delete[] (char *) io->_vd; + delete[] io->in.buffer; + delete[] io->out.buffer; if (io->flags & (int) IOFlags::DESTROY_SIGNALS) { ret = vlist_destroy(io->signals, (dtor_cb_t) signal_decref, false); diff --git a/lib/kernel/if.cpp b/lib/kernel/if.cpp index 7af9b1669..7586631b7 100644 --- a/lib/kernel/if.cpp +++ b/lib/kernel/if.cpp @@ -160,7 +160,7 @@ struct interface * if_get_egress(struct sockaddr *sa, struct vlist *interfaces) } /* If not found, create a new interface */ - i = (struct interface *) alloc(sizeof(struct interface)); + i = new struct interface; if (!i) return nullptr; diff --git a/lib/kernel/tc_netem.cpp b/lib/kernel/tc_netem.cpp index 40a9e15d6..3ce643725 100644 --- a/lib/kernel/tc_netem.cpp +++ b/lib/kernel/tc_netem.cpp @@ -271,7 +271,7 @@ int tc_netem_set_delay_distribution(struct rtnl_qdisc *qdisc, json_t *json) size_t idx; size_t len = json_array_size(json); - int16_t *data = (int16_t *) alloc(len * sizeof(int16_t)); + int16_t *data = new int16_t[len]; json_array_foreach(json, idx, elm) { if (!json_is_integer(elm)) diff --git a/lib/mapping.cpp b/lib/mapping.cpp index fd9ced677..d1f90c61e 100644 --- a/lib/mapping.cpp +++ b/lib/mapping.cpp @@ -216,7 +216,7 @@ int mapping_list_parse(struct vlist *ml, json_t *cfg, struct vlist *nodes) return -1; json_array_foreach(json_mapping, i, json_entry) { - struct mapping_entry *me = (struct mapping_entry *) alloc(sizeof(struct mapping_entry)); + auto *me = new struct mapping_entry; ret = mapping_parse(me, json_entry, nodes); if (ret) diff --git a/lib/memory.cpp b/lib/memory.cpp index 126b8c7bd..fd19f7af1 100644 --- a/lib/memory.cpp +++ b/lib/memory.cpp @@ -146,7 +146,7 @@ int memory_free(void *ptr) return -1; allocations.erase(iter); - free(ma); + delete ma; return 0; } diff --git a/lib/memory/heap.cpp b/lib/memory/heap.cpp index 20cb616a3..e04b87bb9 100644 --- a/lib/memory/heap.cpp +++ b/lib/memory/heap.cpp @@ -31,7 +31,7 @@ static struct memory_allocation * memory_heap_alloc(size_t len, size_t alignment { int ret; - struct memory_allocation *ma = (struct memory_allocation *) alloc(sizeof(struct memory_allocation)); + auto *ma = new struct memory_allocation; if (!ma) return nullptr; @@ -44,7 +44,7 @@ static struct memory_allocation * memory_heap_alloc(size_t len, size_t alignment ret = posix_memalign(&ma->address, ma->alignment, ma->length); if (ret) { - free(ma); + delete ma; return nullptr; } diff --git a/lib/memory/ib.cpp b/lib/memory/ib.cpp index a2da92242..14c126c6e 100644 --- a/lib/memory/ib.cpp +++ b/lib/memory/ib.cpp @@ -44,7 +44,7 @@ static struct memory_allocation * memory_ib_alloc(size_t len, size_t alignment, { struct memory_ib *mi = (struct memory_ib *) m->_vd; - struct memory_allocation *ma = (struct memory_allocation *) alloc(sizeof(struct memory_allocation)); + auto *ma = new struct memory_allocation; if (!ma) return nullptr; @@ -61,7 +61,7 @@ static struct memory_allocation * memory_ib_alloc(size_t len, size_t alignment, ma->ib.mr = ibv_reg_mr(mi->pd, ma->address, ma->length, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE); if (!ma->ib.mr) { mi->parent->free(ma->parent, mi->parent); - free(ma); + delete ma; return nullptr; } diff --git a/lib/memory/managed.cpp b/lib/memory/managed.cpp index b016cb7af..1c06b1124 100644 --- a/lib/memory/managed.cpp +++ b/lib/memory/managed.cpp @@ -106,7 +106,7 @@ static struct memory_allocation * memory_managed_alloc(size_t len, size_t alignm block->used = true; - struct memory_allocation *ma = (struct memory_allocation *) alloc(sizeof(struct memory_allocation)); + auto *ma = new struct memory_allocation; if (!ma) return nullptr; diff --git a/lib/memory/mmap.cpp b/lib/memory/mmap.cpp index e11085f81..741a0eede 100644 --- a/lib/memory/mmap.cpp +++ b/lib/memory/mmap.cpp @@ -99,7 +99,7 @@ static struct memory_allocation * memory_mmap_alloc(size_t len, size_t alignment int flags, fd; size_t sz; - struct memory_allocation *ma = (struct memory_allocation *) alloc(sizeof(struct memory_allocation)); + auto *ma = new struct memory_allocation; if (!ma) return nullptr; @@ -134,7 +134,7 @@ static struct memory_allocation * memory_mmap_alloc(size_t len, size_t alignment ma->address = mmap(nullptr, ma->length, PROT_READ | PROT_WRITE, flags, fd, 0); if (ma->address == MAP_FAILED) { - free(ma); + delete ma; return nullptr; } diff --git a/lib/node.cpp b/lib/node.cpp index c437c2c29..5fc1e12d0 100644 --- a/lib/node.cpp +++ b/lib/node.cpp @@ -53,7 +53,7 @@ int node_init(struct node *n, struct node_type *vt) assert(n->state == State::DESTROYED); n->_vt = vt; - n->_vd = alloc(vt->size); + n->_vd = new char[vt->size]; //n->stats = nullptr; n->name = nullptr; @@ -390,7 +390,7 @@ int node_destroy(struct node *n) vlist_remove_all(&node_type(n)->instances, n); if (n->_vd) - free(n->_vd); + delete[] (char *) n->_vd; if (n->_name) free(n->_name); diff --git a/lib/nodes/comedi.cpp b/lib/nodes/comedi.cpp index 60b327804..b3a131d03 100644 --- a/lib/nodes/comedi.cpp +++ b/lib/nodes/comedi.cpp @@ -72,10 +72,10 @@ static int comedi_parse_direction(struct comedi *c, struct comedi_direction *d, return 0; } - d->chanlist = (unsigned int*) alloc(d->chanlist_len * sizeof(*d->chanlist)); + d->chanlist = new unsigned int[d->chanlist_len]; assert(d->chanlist != nullptr); - d->chanspecs = (comedi_chanspec *) alloc(d->chanlist_len * sizeof(*d->chanspecs)); + d->chanspecs = new comedi_chanspec[d->chanlist_len]; assert(d->chanspecs != nullptr); json_array_foreach(json_chans, i, json_chan) { @@ -236,7 +236,7 @@ static int comedi_start_in(struct node *n) #if COMEDI_USE_READ /* Be prepared to consume one entire buffer */ - c->buf = (char *) alloc(c->in.buffer_size); + c->buf = new char[c->in.buffer_size]; c->bufptr = c->buf; assert(c->bufptr != nullptr); @@ -326,7 +326,7 @@ static int comedi_start_out(struct node *n) /* Allocate buffer for one complete villas sample */ /** @todo: maybe increase buffer size according to c->vectorize */ const size_t local_buffer_size = d->sample_size * d->chanlist_len; - d->buffer = (char *) alloc(local_buffer_size); + d->buffer = new char[local_buffer_size]; d->bufptr = d->buffer; assert(d->buffer != nullptr); diff --git a/lib/nodes/file.cpp b/lib/nodes/file.cpp index 71f820f70..1c8a153cc 100644 --- a/lib/nodes/file.cpp +++ b/lib/nodes/file.cpp @@ -39,7 +39,7 @@ using namespace villas::utils; static char * file_format_name(const char *format, struct timespec *ts) { struct tm tm; - char *buf = (char *) alloc(FILE_MAX_PATHLEN); + char *buf = new char[FILE_MAX_PATHLEN]; /* Convert time */ gmtime_r(&ts->tv_sec, &tm); @@ -349,7 +349,7 @@ int file_stop(struct node *n) if (ret) return ret; - free(f->uri); + delete f->uri; return 0; } diff --git a/lib/nodes/iec61850.cpp b/lib/nodes/iec61850.cpp index 48e273397..d88026c55 100644 --- a/lib/nodes/iec61850.cpp +++ b/lib/nodes/iec61850.cpp @@ -293,7 +293,7 @@ struct iec61850_receiver * iec61850_receiver_create(enum iec61850_receiver::Type /* Check if there is already a SVReceiver for this interface */ r = iec61850_receiver_lookup(t, intf); if (!r) { - r = (struct iec61850_receiver *) alloc(sizeof(struct iec61850_receiver)); + r = new struct iec61850_receiver; if (!r) return nullptr; diff --git a/lib/nodes/infiniband.cpp b/lib/nodes/infiniband.cpp index e1809bb81..ecb4750dc 100644 --- a/lib/nodes/infiniband.cpp +++ b/lib/nodes/infiniband.cpp @@ -655,7 +655,7 @@ int ib_start(struct node *n) /* Allocate space for 40 Byte GHR. We don't use this. */ if (ib->conn.port_space == RDMA_PS_UDP) { - ib->conn.ud.grh_ptr = alloc(GRH_SIZE); + ib->conn.ud.grh_ptr = new char[GRH_SIZE]; ib->conn.ud.grh_mr = ibv_reg_mr(ib->ctx.pd, ib->conn.ud.grh_ptr, GRH_SIZE, IBV_ACCESS_LOCAL_WRITE); } diff --git a/lib/nodes/ngsi.cpp b/lib/nodes/ngsi.cpp index 905098fcf..17c027dab 100644 --- a/lib/nodes/ngsi.cpp +++ b/lib/nodes/ngsi.cpp @@ -217,7 +217,7 @@ static int ngsi_parse_mapping(struct vlist *mapping, json_t *cfg) if (!token) return -2; - struct ngsi_attribute *a = (struct ngsi_attribute *) alloc(sizeof(struct ngsi_attribute)); + auto *a = new struct ngsi_attribute; a->index = j; diff --git a/lib/nodes/opal.cpp b/lib/nodes/opal.cpp index 25706821a..1f4f13900 100644 --- a/lib/nodes/opal.cpp +++ b/lib/nodes/opal.cpp @@ -80,8 +80,8 @@ int opal_type_start(villas::node::SuperNode *sn) if (err != EOK) error("Failed to get number of recv blocks (%d)", err); - send_ids = alloc(send_icons * sizeof(int)); - recv_ids = alloc(recv_icons * sizeof(int)); + send_ids = new int[send_icons]; + recv_ids = new int[recv_icons]; err = OpalGetAsyncSendIDList(send_ids, send_icons * sizeof(int)); if (err != EOK) @@ -115,8 +115,8 @@ int opal_type_stop() pthread_mutex_destroy(&lock); - free(send_ids); - free(recv_ids); + delete[] send_ids; + delete[] recv_ids; return 0; } @@ -125,8 +125,8 @@ int opal_print_global() { debug(LOG_OPAL | 2, "Controller ID: %u", params.controllerID); - char *sbuf = alloc(send_icons * 5); - char *rbuf = alloc(recv_icons * 5); + auto *sbuf = new char[send_icons * 5]; + auto *rbuf = new char[recv_icons * 5]; for (int i = 0; i < send_icons; i++) strcatf(&sbuf, "%u ", send_ids[i]); @@ -136,8 +136,8 @@ int opal_print_global() debug(LOG_OPAL | 2, "Send Blocks: %s", sbuf); debug(LOG_OPAL | 2, "Receive Blocks: %s", rbuf); - free(sbuf); - free(rbuf); + delete[] sbuf; + delete[] rbuf; debug(LOG_OPAL | 2, "Control Block Parameters:"); for (int i = 0; i < GENASYNC_NB_FLOAT_PARAM; i++) diff --git a/lib/nodes/shmem.cpp b/lib/nodes/shmem.cpp index d4e329bec..e08f88019 100644 --- a/lib/nodes/shmem.cpp +++ b/lib/nodes/shmem.cpp @@ -80,7 +80,7 @@ int shmem_parse(struct node *n, json_t *cfg) if (!json_is_array(json_exec)) error("Setting 'exec' of node %s must be an array of strings", node_name(n)); - shm->exec = (char **) alloc(sizeof(char *) * (json_array_size(json_exec) + 1)); + shm->exec = new char*[json_array_size(json_exec) + 1]; size_t i; json_t *json_val; diff --git a/lib/nodes/signal_generator.cpp b/lib/nodes/signal_generator.cpp index cf2f44d9e..c901e5dd7 100644 --- a/lib/nodes/signal_generator.cpp +++ b/lib/nodes/signal_generator.cpp @@ -91,7 +91,7 @@ int signal_generator_prepare(struct node *n) assert(vlist_length(&n->in.signals) == 0); for (unsigned i = 0; i < s->values; i++) { - struct signal *sig = (struct signal *) alloc(sizeof(struct signal)); + auto *sig = new struct signal; int rtype = s->type == signal_generator::SignalType::MIXED ? i % 7 : (int) s->type; @@ -154,7 +154,7 @@ int signal_generator_start(struct node *n) s->missed_steps = 0; s->counter = 0; s->started = time_now(); - s->last = (double *) alloc(sizeof(double) * s->values); + s->last = new double[s->values]; for (unsigned i = 0; i < s->values; i++) s->last[i] = s->offset; @@ -183,7 +183,7 @@ int signal_generator_stop(struct node *n) if (s->missed_steps > 0 && s->monitor_missed) warning("Node %s missed a total of %d steps.", node_name(n), s->missed_steps); - free(s->last); + delete[] s->last; return 0; } diff --git a/lib/nodes/socket.cpp b/lib/nodes/socket.cpp index 266a50137..7e09e79ca 100644 --- a/lib/nodes/socket.cpp +++ b/lib/nodes/socket.cpp @@ -274,12 +274,12 @@ int socket_start(struct node *n) } s->out.buflen = SOCKET_INITIAL_BUFFER_LEN; - s->out.buf = (char *) alloc(s->out.buflen); + s->out.buf = new char[s->out.buflen]; if (!s->out.buf) return -1; s->in.buflen = SOCKET_INITIAL_BUFFER_LEN; - s->in.buf = (char *) alloc(s->in.buflen); + s->in.buf = new char[s->in.buflen]; if (!s->in.buf) return -1; @@ -320,8 +320,8 @@ int socket_stop(struct node *n) if (ret) return ret; - free(s->in.buf); - free(s->out.buf); + delete[] s->in.buf; + delete[] s->out.buf; return 0; } @@ -405,7 +405,10 @@ retry: ret = io_sprint(&s->io, s->out.buf, s->out.buflen, &wbytes, smps, cnt); if (wbytes > s->out.buflen) { s->out.buflen = wbytes; - s->out.buf = (char *) realloc(s->out.buf, s->out.buflen); + + delete[] s->out.buf; + s->out.buf = new char[s->out.buflen]; + goto retry; } diff --git a/lib/nodes/stats.cpp b/lib/nodes/stats.cpp index f105f26f7..32a685d2e 100644 --- a/lib/nodes/stats.cpp +++ b/lib/nodes/stats.cpp @@ -181,7 +181,7 @@ int stats_node_parse(struct node *n, json_t *cfg) struct signal *sig = (struct signal *) vlist_at(&n->in.signals, i); struct stats_node_signal *stats_sig; - stats_sig = (struct stats_node_signal *) alloc(sizeof(struct stats_node_signal)); + stats_sig = new struct stats_node_signal; if (!stats_sig) return -1; diff --git a/lib/nodes/test_rtt.cpp b/lib/nodes/test_rtt.cpp index f85f13758..dc4ecb914 100644 --- a/lib/nodes/test_rtt.cpp +++ b/lib/nodes/test_rtt.cpp @@ -106,7 +106,7 @@ int test_rtt_prepare(struct node *n) if (c->values > max_values) max_values = c->values; - c->filename_formatted = (char *) alloc(NAME_MAX); + c->filename_formatted = new char[NAME_MAX]; strftime(c->filename_formatted, NAME_MAX, c->filename, &tm); } @@ -223,7 +223,7 @@ int test_rtt_parse(struct node *n, json_t *cfg) for (int i = 0; i < numrates; i++) { for (int j = 0; j < numvalues; j++) { - struct test_rtt_case *c = (struct test_rtt_case *) alloc(sizeof(struct test_rtt_case)); + auto *c = new struct test_rtt_case; c->rate = rates[i]; c->values = values[j]; diff --git a/lib/nodes/uldaq.cpp b/lib/nodes/uldaq.cpp index 5142e0520..835c9dd83 100644 --- a/lib/nodes/uldaq.cpp +++ b/lib/nodes/uldaq.cpp @@ -264,7 +264,7 @@ int uldaq_destroy(struct node *n) struct uldaq *u = (struct uldaq *) n->_vd; if (u->in.queues) - free(u->in.queues); + delete[] u->in.queues; ret = pthread_mutex_destroy(&u->in.mutex); if (ret) @@ -311,8 +311,11 @@ int uldaq_parse(struct node *n, json_t *cfg) u->device_interface_type = (DaqDeviceInterface) iftype; } + if (u->in.queues) + delete[] u->in.queues; + u->in.channel_count = vlist_length(&n->in.signals); - u->in.queues = (struct AiQueueElement *) realloc(u->in.queues, sizeof(struct AiQueueElement) * u->in.channel_count); + u->in.queues = new struct AiQueueElement[u->in.channel_count]; json_array_foreach(json_signals, i, json_signal) { const char *range_str = nullptr, *input_mode_str = nullptr; @@ -521,7 +524,7 @@ int uldaq_start(struct node *n) /* Allocate a buffer to receive the data */ u->in.buffer_len = u->in.channel_count * n->in.vectorize * 50; - u->in.buffer = (double *) alloc(u->in.buffer_len * sizeof(double)); + u->in.buffer = new double[u->in.buffer_len]; if (!u->in.buffer) { warning("Out of memory, unable to create scan buffer"); return -1; diff --git a/lib/nodes/websocket.cpp b/lib/nodes/websocket.cpp index 51cae46c6..977951bc9 100644 --- a/lib/nodes/websocket.cpp +++ b/lib/nodes/websocket.cpp @@ -275,7 +275,7 @@ int websocket_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, voi websocket_connection_destroy(c); if (c->mode == websocket_connection::Mode::CLIENT) - free(c); + delete c; break; @@ -401,8 +401,8 @@ int websocket_start(struct node *n) for (size_t i = 0; i < vlist_length(&w->destinations); i++) { const char *format; - struct websocket_destination *d = (struct websocket_destination *) vlist_at(&w->destinations, i); - struct websocket_connection *c = (struct websocket_connection *) alloc(sizeof(struct websocket_connection)); + auto *d = (struct websocket_destination *) vlist_at(&w->destinations, i); + auto *c = new struct websocket_connection; c->state = websocket_connection::State::CONNECTING; @@ -555,7 +555,7 @@ int websocket_parse(struct node *n, json_t *cfg) if (!uri) error("The 'destinations' setting of node %s must be an array of URLs", node_name(n)); - struct websocket_destination *d = (struct websocket_destination *) alloc(sizeof(struct websocket_destination)); + auto *d = new struct websocket_destination; d->uri = strdup(uri); diff --git a/lib/path.cpp b/lib/path.cpp index ac142942e..93fe1888b 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -172,7 +172,7 @@ static int path_prepare_poll(struct path *p) int fds[16], ret, n = 0, m; if (p->reader.pfds) - free(p->reader.pfds); + delete[] p->reader.pfds; p->reader.pfds = nullptr; p->reader.nfds = 0; @@ -278,7 +278,7 @@ int path_prepare(struct path *p) } /* For other mappings we create new signal descriptors */ else { - sig = (struct signal *) alloc(sizeof(struct signal)); + sig = new struct signal; ret = signal_init_from_mapping(sig, me, j); if (ret) @@ -386,7 +386,7 @@ int path_parse(struct path *p, json_t *cfg, struct vlist *nodes) /* Create new path_source of not existing */ if (!ps) { - ps = (struct path_source *) alloc(sizeof(struct path_source)); + ps = new struct path_source; ps->node = me->node; ps->masked = false; @@ -409,7 +409,7 @@ int path_parse(struct path *p, json_t *cfg, struct vlist *nodes) for (size_t i = 0; i < vlist_length(&destinations); i++) { struct node *n = (struct node *) vlist_at(&destinations, i); - struct path_destination *pd = (struct path_destination *) alloc(sizeof(struct path_destination)); + auto *pd = new struct path_destination; pd->node = n; @@ -713,7 +713,7 @@ int path_destroy(struct path *p) return ret; if (p->reader.pfds) - free(p->reader.pfds); + delete[] p->reader.pfds; if (p->_name) free(p->_name); diff --git a/lib/sample.cpp b/lib/sample.cpp index 076669a38..aa63b4f4f 100644 --- a/lib/sample.cpp +++ b/lib/sample.cpp @@ -64,7 +64,7 @@ struct sample * sample_alloc_mem(int capacity) { size_t sz = SAMPLE_LENGTH(capacity); - char *b = (char *) alloc(sz); + char *b = new char[sz]; if (!b) return nullptr; @@ -86,7 +86,7 @@ void sample_free(struct sample *s) if (p) pool_put(p, s); else - free(s); + delete[] (char *) s; } int sample_alloc_many(struct pool *p, struct sample *smps[], int cnt) diff --git a/lib/signal.cpp b/lib/signal.cpp index ae922e086..0db516885 100644 --- a/lib/signal.cpp +++ b/lib/signal.cpp @@ -106,7 +106,7 @@ struct signal * signal_create(const char *name, const char *unit, enum SignalTyp int ret; struct signal *sig; - sig = (struct signal *) alloc(sizeof(struct signal)); + sig = new struct signal; if (!sig) return nullptr; @@ -133,7 +133,7 @@ int signal_free(struct signal *s) if (ret) return ret; - free(s); + delete s; return 0; } @@ -158,7 +158,7 @@ struct signal * signal_copy(struct signal *s) { struct signal *ns; - ns = (struct signal *) alloc(sizeof(struct signal)); + ns = new struct signal; if (!ns) return nullptr; @@ -254,7 +254,7 @@ int signal_list_parse(struct vlist *list, json_t *cfg) size_t i; json_t *json_signal; json_array_foreach(cfg, i, json_signal) { - s = (struct signal *) alloc(sizeof(struct signal)); + s = new struct signal; if (!s) return -1; diff --git a/lib/socket_addr.cpp b/lib/socket_addr.cpp index f688218a2..945072f60 100644 --- a/lib/socket_addr.cpp +++ b/lib/socket_addr.cpp @@ -34,7 +34,7 @@ using namespace villas::utils; char * socket_print_addr(struct sockaddr *saddr) { union sockaddr_union *sa = (union sockaddr_union *) saddr; - char *buf = (char *) alloc(64); + char *buf = new char[64]; /* Address */ switch (sa->sa.sa_family) { diff --git a/lib/super_node.cpp b/lib/super_node.cpp index da4366b5b..6e10cbcde 100644 --- a/lib/super_node.cpp +++ b/lib/super_node.cpp @@ -154,7 +154,7 @@ void SuperNode::parse(json_t *cfg) if (!nt) throw ConfigError(json_node, "node-config-node-type", "Invalid node type: {}", type); - auto *n = (struct node *) alloc(sizeof(struct node)); + auto *n = new struct node; ret = node_init(n, nt); if (ret) @@ -179,7 +179,7 @@ void SuperNode::parse(json_t *cfg) size_t i; json_t *json_path; json_array_foreach(json_paths, i, json_path) { -parse: path *p = (path *) alloc(sizeof(path)); +parse: path *p = new path; ret = path_init(p); if (ret)