1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-06 16:59:03 +03:00

shared/sleep-config: get rid of explicit allocation size calculation

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-04-10 11:23:46 +02:00
parent aad69cce2d
commit 6524f1a8eb

View File

@ -274,11 +274,11 @@ static bool enough_memory_for_hibernation(void) {
int read_fiemap(int fd, struct fiemap **ret) { int read_fiemap(int fd, struct fiemap **ret) {
_cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL; _cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL;
int extents_size;
struct stat statinfo; struct stat statinfo;
uint32_t result_extents = 0; uint32_t result_extents = 0;
uint64_t fiemap_start = 0, fiemap_length; uint64_t fiemap_start = 0, fiemap_length;
size_t fiemap_size = 1, result_fiemap_size = 1; const size_t n_extra = DIV_ROUND_UP(sizeof(struct fiemap), sizeof(struct fiemap_extent));
size_t fiemap_allocated = n_extra, result_fiemap_allocated = n_extra;
if (fstat(fd, &statinfo) < 0) if (fstat(fd, &statinfo) < 0)
return log_debug_errno(errno, "Cannot determine file size: %m"); return log_debug_errno(errno, "Cannot determine file size: %m");
@ -286,12 +286,12 @@ int read_fiemap(int fd, struct fiemap **ret) {
return -ENOTTY; return -ENOTTY;
fiemap_length = statinfo.st_size; fiemap_length = statinfo.st_size;
/* zero this out in case we run on a file with no extents */ /* Zero this out in case we run on a file with no extents */
fiemap = new0(struct fiemap, 1); fiemap = calloc(n_extra, sizeof(struct fiemap_extent));
if (!fiemap) if (!fiemap)
return -ENOMEM; return -ENOMEM;
result_fiemap = new(struct fiemap, 1); result_fiemap = malloc_multiply(n_extra, sizeof(struct fiemap_extent));
if (!result_fiemap) if (!result_fiemap)
return -ENOMEM; return -ENOMEM;
@ -315,12 +315,10 @@ int read_fiemap(int fd, struct fiemap **ret) {
if (fiemap->fm_mapped_extents == 0) if (fiemap->fm_mapped_extents == 0)
break; break;
/* Result fiemap has to hold all the extents for the whole file */ /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all
extents_size = DIV_ROUND_UP(sizeof(struct fiemap_extent) * fiemap->fm_mapped_extents, * the extents for the whole file. Add space for the initial struct fiemap. */
sizeof(struct fiemap)); if (!greedy_realloc0((void**) &fiemap, &fiemap_allocated,
n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
/* Resize fiemap to allow us to read in the extents */
if (!GREEDY_REALLOC0(fiemap, fiemap_size, extents_size))
return -ENOMEM; return -ENOMEM;
fiemap->fm_extent_count = fiemap->fm_mapped_extents; fiemap->fm_extent_count = fiemap->fm_mapped_extents;
@ -329,12 +327,9 @@ int read_fiemap(int fd, struct fiemap **ret) {
if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0)
return log_debug_errno(errno, "Failed to read extents: %m"); return log_debug_errno(errno, "Failed to read extents: %m");
extents_size = DIV_ROUND_UP(sizeof(struct fiemap_extent) * (result_extents + fiemap->fm_mapped_extents), /* Resize result_fiemap to allow us to copy in the extents */
sizeof(struct fiemap)); if (!greedy_realloc((void**) &result_fiemap, &result_fiemap_allocated,
n_extra + result_extents + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
/* Resize result_fiemap to allow us to read in the extents */
if (!GREEDY_REALLOC(result_fiemap, result_fiemap_size,
extents_size))
return -ENOMEM; return -ENOMEM;
memcpy(result_fiemap->fm_extents + result_extents, memcpy(result_fiemap->fm_extents + result_extents,
@ -344,7 +339,7 @@ int read_fiemap(int fd, struct fiemap **ret) {
result_extents += fiemap->fm_mapped_extents; result_extents += fiemap->fm_mapped_extents;
/* Highly unlikely that it is zero */ /* Highly unlikely that it is zero */
if (fiemap->fm_mapped_extents > 0) { if (_likely_(fiemap->fm_mapped_extents > 0)) {
uint32_t i = fiemap->fm_mapped_extents - 1; uint32_t i = fiemap->fm_mapped_extents - 1;
fiemap_start = fiemap->fm_extents[i].fe_logical + fiemap_start = fiemap->fm_extents[i].fe_logical +