Skip to content

Commit

Permalink
util/oslib: Have qemu_prealloc_mem() handler return a boolean
Browse files Browse the repository at this point in the history
Following the example documented since commit e3fe398 ("error:
Document Error API usage rules"), have qemu_prealloc_mem()
return a boolean indicating whether an error is set or not.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Gavin Shan <gshan@redhat.com>
Message-Id: <20231120213301.24349-19-philmd@linaro.org>
  • Loading branch information
philmd committed Jan 5, 2024
1 parent 3961613 commit b622ee9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion include/qemu/osdep.h
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,10 @@ typedef struct ThreadContext ThreadContext;
* memory area starting at @area with the size of @sz. After a successful call,
* each page in the area was faulted in writable at least once, for example,
* after allocating file blocks for mapped files.
*
* Return: true on success, else false setting @errp with error.
*/
void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
bool qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
ThreadContext *tc, Error **errp);

/**
Expand Down
7 changes: 5 additions & 2 deletions util/oslib-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ static bool madv_populate_write_possible(char *area, size_t pagesize)
errno != EINVAL;
}

void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
bool qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
ThreadContext *tc, Error **errp)
{
static gsize initialized;
Expand All @@ -506,6 +506,7 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
size_t numpages = DIV_ROUND_UP(sz, hpagesize);
bool use_madv_populate_write;
struct sigaction act;
bool rv = true;

/*
* Sense on every invocation, as MADV_POPULATE_WRITE cannot be used for
Expand Down Expand Up @@ -534,7 +535,7 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
qemu_mutex_unlock(&sigbus_mutex);
error_setg_errno(errp, errno,
"qemu_prealloc_mem: failed to install signal handler");
return;
return false;
}
}

Expand All @@ -544,6 +545,7 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
if (ret) {
error_setg_errno(errp, -ret,
"qemu_prealloc_mem: preallocating memory failed");
rv = false;
}

if (!use_madv_populate_write) {
Expand All @@ -555,6 +557,7 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
}
qemu_mutex_unlock(&sigbus_mutex);
}
return rv;
}

char *qemu_get_pid_name(pid_t pid)
Expand Down
4 changes: 3 additions & 1 deletion util/oslib-win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ int getpagesize(void)
return system_info.dwPageSize;
}

void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
bool qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
ThreadContext *tc, Error **errp)
{
int i;
Expand All @@ -274,6 +274,8 @@ void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
for (i = 0; i < sz / pagesize; i++) {
memset(area + pagesize * i, 0, 1);
}

return true;
}

char *qemu_get_pid_name(pid_t pid)
Expand Down

0 comments on commit b622ee9

Please sign in to comment.