* Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
* Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
*
- * SPDX-License-Identifier: GPL-2.0-only
+ * SPDX-License-Identifier: LGPL-2.1-only
*
*/
-#include "common/macros.h"
#define _LGPL_SOURCE
#include <ctype.h>
#include <fcntl.h>
+#include <grp.h>
+#include <inttypes.h>
#include <limits.h>
+#include <pwd.h>
#include <stdlib.h>
+#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
-#include <inttypes.h>
-#include <grp.h>
-#include <pwd.h>
-#include <sys/file.h>
-#include <unistd.h>
-#include <common/common.h>
-#include <common/readwrite.h>
-#include <common/runas.h>
-#include <common/compat/getenv.h>
-#include <common/compat/string.h>
-#include <common/compat/dirent.h>
-#include <common/compat/directory-handle.h>
-#include <common/dynamic-buffer.h>
-#include <common/string-utils/format.h>
+#include <common/common.hpp>
+#include <common/compat/directory-handle.hpp>
+#include <common/compat/dirent.hpp>
+#include <common/compat/getenv.hpp>
+#include <common/compat/string.hpp>
+#include <common/dynamic-buffer.hpp>
+#include <common/readwrite.hpp>
+#include <common/runas.hpp>
+#include <common/string-utils/format.hpp>
#include <lttng/constant.h>
-#include "utils.h"
-#include "defaults.h"
-#include "time.h"
+#include "defaults.hpp"
+#include "time.hpp"
+#include "utils.hpp"
#define PROC_MEMINFO_PATH "/proc/meminfo"
#define PROC_MEMINFO_MEMAVAILABLE_LINE "MemAvailable:"
*/
char *utils_strdupdelim(const char *begin, const char *end)
{
- char *str;
+ char *str = zmalloc<char>(end - begin + 1);
- str = (char *) zmalloc(end - begin + 1);
if (str == NULL) {
PERROR("zmalloc strdupdelim");
goto error;
goto end;
}
retry:
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
goto end;
}
string_len += long_options[i].has_arg ? 1 : 0;
}
- optstring = (char *) zmalloc(string_len);
+ optstring = zmalloc<char>(string_len);
if (!optstring) {
goto end;
}
}
static
-int read_proc_meminfo_field(const char *field, size_t *value)
+int read_proc_meminfo_field(const char *field, uint64_t *value)
{
int ret;
FILE *proc_meminfo;
* field.
*/
while (!feof(proc_meminfo)) {
- unsigned long value_kb;
+ uint64_t value_kb;
ret = fscanf(proc_meminfo,
- "%" MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "s %lu kB\n",
+ "%" MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "s %" SCNu64 " kB\n",
name, &value_kb);
if (ret == EOF) {
/*
* This number is displayed in kilo-bytes. Return the
* number of bytes.
*/
- *value = ((size_t) value_kb) * 1024;
+ if (value_kb > UINT64_MAX / 1024) {
+ ERR("Overflow on kb to bytes conversion");
+ break;
+ }
+
+ *value = value_kb * 1024;
ret = 0;
goto found;
}
* the information in `/proc/meminfo`. The number returned by this function is
* a best guess.
*/
-int utils_get_memory_available(size_t *value)
+int utils_get_memory_available(uint64_t *value)
{
return read_proc_meminfo_field(PROC_MEMINFO_MEMAVAILABLE_LINE, value);
}
* Returns the total size of the memory on the system in bytes based on the
* the information in `/proc/meminfo`.
*/
-int utils_get_memory_total(size_t *value)
+int utils_get_memory_total(uint64_t *value)
{
return read_proc_meminfo_field(PROC_MEMINFO_MEMTOTAL_LINE, value);
}
buflen = FALLBACK_USER_BUFLEN;
}
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
ret_val = LTTNG_ERR_NOMEM;
goto end;
case ERANGE:
buflen *= 2;
free(buf);
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
ret_val = LTTNG_ERR_NOMEM;
goto end;
buflen = FALLBACK_GROUP_BUFLEN;
}
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
ret_val = LTTNG_ERR_NOMEM;
goto end;
case ERANGE:
buflen *= 2;
free(buf);
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
ret_val = LTTNG_ERR_NOMEM;
goto end;