Skip to content

Commit ad06ef0

Browse files
committed
util: add qemu_get_host_physmem utility function
This will be used in a future patch. For POSIX systems _SC_PHYS_PAGES isn't standardised but at least appears in the man pages for Open/FreeBSD. The result is advisory so any users of it shouldn't just fail if we can't work it out. The win32 stub currently returns 0 until someone with a Windows system can develop and test a patch. Signed-off-by: Alex Bennée <[email protected]> Reviewed-by: Richard Henderson <[email protected]> Cc: BALATON Zoltan <[email protected]> Cc: Christian Ehrhardt <[email protected]> Message-Id: <[email protected]>
1 parent 7d2d652 commit ad06ef0

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

include/qemu/osdep.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,4 +668,16 @@ static inline void qemu_reset_optind(void)
668668
*/
669669
char *qemu_get_host_name(Error **errp);
670670

671+
/**
672+
* qemu_get_host_physmem:
673+
*
674+
* Operating system agnostic way of querying host memory.
675+
*
676+
* Returns amount of physical memory on the system. This is purely
677+
* advisery and may return 0 if we can't work it out. At the other
678+
* end we saturate to SIZE_MAX if you are lucky enough to have that
679+
* much memory.
680+
*/
681+
size_t qemu_get_host_physmem(void);
682+
671683
#endif

util/oslib-posix.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,3 +841,18 @@ char *qemu_get_host_name(Error **errp)
841841

842842
return g_steal_pointer(&hostname);
843843
}
844+
845+
size_t qemu_get_host_physmem(void)
846+
{
847+
#ifdef _SC_PHYS_PAGES
848+
long pages = sysconf(_SC_PHYS_PAGES);
849+
if (pages > 0) {
850+
if (pages > SIZE_MAX / qemu_real_host_page_size) {
851+
return SIZE_MAX;
852+
} else {
853+
return pages * qemu_real_host_page_size;
854+
}
855+
}
856+
#endif
857+
return 0;
858+
}

util/oslib-win32.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,9 @@ char *qemu_get_host_name(Error **errp)
828828

829829
return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
830830
}
831+
832+
size_t qemu_get_host_physmem(void)
833+
{
834+
/* currently unimplemented */
835+
return 0;
836+
}

0 commit comments

Comments
 (0)