Commit afe61e5f authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼

refactor malloc()/free() by new/delete

parent b82ce318
......@@ -177,9 +177,6 @@ char * vstrcatf(char **dest, const char *fmt, va_list va)
char * strf(const char *fmt, ...);
char * vstrf(const char *fmt, va_list va);
/** Allocate and initialize memory. */
void * alloc(size_t bytes);
/** Allocate and copy memory. */
void * memdup(const void *src, size_t bytes);
......
......@@ -49,6 +49,10 @@ add_library(villas-common SHARED
popen.cpp
)
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
target_link_libraries(villas-common PUBLIC "-lstdc++fs")
endif()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
target_sources(villas-common PRIVATE tsc.cpp)
endif()
......
......@@ -22,6 +22,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#if __GNUC__ <= 7 && !defined(__clang__)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
......@@ -198,10 +206,9 @@ int aislocal(const char *uri)
AFILE * afopen(const char *uri, const char *mode)
{
int ret;
char *cwd;
const char *sep;
AFILE *af = (AFILE *) alloc(sizeof(AFILE));
AFILE *af = new AFILE;
snprintf(af->mode, sizeof(af->mode), "%s", mode);
......@@ -216,13 +223,8 @@ AFILE * afopen(const char *uri, const char *mode)
return nullptr;
/* Handle relative paths */
if (uri[0] != '/') {
cwd = getcwd(nullptr, 0);
af->uri = strf("file://%s/%s", cwd, uri);
free(cwd);
}
if (uri[0] != '/')
af->uri = strf("file://%s/%s", fs::current_path().c_str(), uri);
else
af->uri = strf("file://%s", uri);
}
......@@ -264,7 +266,8 @@ AFILE * afopen(const char *uri, const char *mode)
out0: curl_easy_cleanup(af->curl);
out1: fclose(af->file);
out2: free(af->uri);
free(af);
delete af;
return nullptr;
}
......@@ -284,7 +287,8 @@ int afclose(AFILE *af)
return ret;
free(af->uri);
free(af);
delete af;
return 0;
}
......
......@@ -30,7 +30,7 @@ int buffer_init(struct buffer *b, size_t size)
{
b->len = 0;
b->size = size;
b->buf = (char *) malloc(size);
b->buf = new char[size];
if (!b->buf)
return -1;
......@@ -42,7 +42,7 @@ int buffer_init(struct buffer *b, size_t size)
int buffer_destroy(struct buffer *b)
{
if (b->buf)
free(b->buf);
delete[] b->buf;
b->state = State::DESTROYED;
......
......@@ -143,7 +143,7 @@ void Hist::print(bool details) const
info("Stddev: %g", getStddev());
if (details && total - higher - lower > 0) {
char *buf =dump();
char *buf = dump();
info("Matlab: %s", buf);
free(buf);
......@@ -187,7 +187,7 @@ void Hist::plot() const
char * Hist::dump() const
{
char *buf = (char *) alloc(128);
char *buf = new char[128];
strcatf(&buf, "[ ");
......
......@@ -57,7 +57,7 @@ int pci_init(struct pci *p)
(strcmp(e->d_name, "..") == 0) )
continue;
struct pci_device *d = (struct pci_device *) alloc(sizeof(struct pci_device));
struct pci_device *d = new struct pci_device;
struct { const char *s; int *p; } map[] = {
{ "vendor", &d->id.vendor },
......@@ -227,8 +227,7 @@ int pci_device_parse_id(struct pci_device *f, const char *str, const char **erro
return 0;
fail:
free(tmp);
fail: free(tmp);
return -1;
}
......@@ -314,9 +313,8 @@ size_t pci_get_regions(const struct pci_device *d, struct pci_region** regions)
}
if (valid_regions > 0) {
const size_t len = valid_regions * sizeof (struct pci_region);
*regions = (struct pci_region *) malloc(len);
memcpy(*regions, _regions, len);
*regions = new struct pci_region[valid_regions];
memcpy(*regions, _regions, valid_regions * sizeof (struct pci_region));
}
return valid_regions;
......
......@@ -579,7 +579,7 @@ VfioDevice::pciHotReset()
const size_t reset_infolen = sizeof(struct vfio_pci_hot_reset_info) +
sizeof(struct vfio_pci_dependent_device) * 64;
auto reset_info = reinterpret_cast<struct vfio_pci_hot_reset_info*>
auto reset_info = reinterpret_cast<struct vfio_pci_hot_reset_info *>
(calloc(1, reset_infolen));
reset_info->argsz = reset_infolen;
......
......@@ -117,7 +117,7 @@ int vfio_device_destroy(struct vfio_device *d)
debug(5, "VFIO: closed device: name=%s, fd=%d", d->name, d->fd);
free(d->mappings);
delete[] d->mappings;
free(d->name);
return 0;
......@@ -252,7 +252,7 @@ int vfio_device_attach(struct vfio_device *d, struct vfio_container *c, const ch
}
if (!g) {
g = alloc(sizeof(struct vfio_group));
g = new struct vfio_group;
/* Aquire group ownership */
ret = vfio_group_attach(g, c, index);
......@@ -277,9 +277,9 @@ int vfio_device_attach(struct vfio_device *d, struct vfio_container *c, const ch
if (ret < 0)
serror("Failed to get VFIO device info for: %s", d->name);
d->irqs = alloc(d->info.num_irqs * sizeof(struct vfio_irq_info));
d->regions = alloc(d->info.num_regions * sizeof(struct vfio_region_info));
d->mappings = alloc(d->info.num_regions * sizeof(void *));
d->irqs = new struct vfio_irq_info[d->info.num_irqs];
d->regions = new struct vfio_region_info[d->info.num_regions];
d->mappings = new void *[d->info.num_regions];
/* Get device regions */
for (int i = 0; i < d->info.num_regions && i < 8; i++) {
......@@ -321,8 +321,8 @@ int vfio_pci_reset(struct vfio_device *d)
size_t reset_infolen = sizeof(struct vfio_pci_hot_reset_info) + sizeof(struct vfio_pci_dependent_device) * 64;
size_t resetlen = sizeof(struct vfio_pci_hot_reset) + sizeof(int32_t) * 1;
struct vfio_pci_hot_reset_info *reset_info = (struct vfio_pci_hot_reset_info *) alloc(reset_infolen);
struct vfio_pci_hot_reset *reset = (struct vfio_pci_hot_reset *) alloc(resetlen);
struct vfio_pci_hot_reset_info *reset_info = new char[reset_infolen];
struct vfio_pci_hot_reset *reset = new char[resetlen];
reset_info->argsz = reset_infolen;
reset->argsz = resetlen;
......@@ -400,7 +400,7 @@ int vfio_pci_msi_deinit(struct vfio_device *d, int efds[32])
return -1;
irq_setlen = sizeof(struct vfio_irq_set) + sizeof(int) * irq_count;
irq_set = alloc(irq_setlen);
irq_set = new char[irq_setlen];
irq_set->argsz = irq_setlen;
irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
......@@ -434,7 +434,7 @@ int vfio_pci_msi_init(struct vfio_device *d, int efds[32])
return -1;
irq_setlen = sizeof(struct vfio_irq_set) + sizeof(int) * irq_count;
irq_set = alloc(irq_setlen);
irq_set = new char[irq_setlen];
irq_set->argsz = irq_setlen;
irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
......
......@@ -212,17 +212,6 @@ double randf()
return (double) random() / RAND_MAX;
}
void * alloc(size_t bytes)
{
void *p = malloc(bytes);
if (!p)
error("Failed to allocate memory");
memset(p, 0, bytes);
return p;
}
char * vstrcatf(char **dest, const char *fmt, va_list ap)
{
char *tmp;
......@@ -271,7 +260,7 @@ char * vstrf(const char *fmt, va_list va)
void * memdup(const void *src, size_t bytes)
{
void *dst = alloc(bytes);
void *dst = new char[bytes];
memcpy(dst, src, bytes);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment