1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-02 01:18:26 +03:00

device_mapper: use on stack allocation

Reduce number of heap allocation calls and for smaller
locally used string, use on stack allocation.
This commit is contained in:
Zdenek Kabelac 2024-10-21 14:16:23 +02:00
parent d8996a2a12
commit f59fa69507
2 changed files with 32 additions and 18 deletions

View File

@ -4484,6 +4484,7 @@ static const char *_get_field_id(struct dm_report *rh, struct dm_report_field *f
static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field *field) static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field *field)
{ {
char buf_local[8192];
char *field_id; char *field_id;
int32_t width; int32_t width;
uint32_t align; uint32_t align;
@ -4491,25 +4492,26 @@ static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field
size_t buf_size = 0; size_t buf_size = 0;
if (rh->flags & DM_REPORT_OUTPUT_FIELD_NAME_PREFIX) { if (rh->flags & DM_REPORT_OUTPUT_FIELD_NAME_PREFIX) {
if (!(field_id = strdup(_get_field_id(rh, field)))) { buf_size = strlen(_get_field_id(rh, field)) + 1;
log_error("dm_report: Failed to copy field name"); if (buf_size >= sizeof(buf_local)) {
/* for field names our buf_local should be enough */
log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG);
return 0; return 0;
} }
field_id = buf_local;
memcpy(field_id, _get_field_id(rh, field), buf_size);
if (!dm_pool_grow_object(rh->mem, rh->output_field_name_prefix, 0)) { if (!dm_pool_grow_object(rh->mem, rh->output_field_name_prefix, 0)) {
log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG); log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG);
free(field_id);
return 0; return 0;
} }
if (!dm_pool_grow_object(rh->mem, _toupperstr(field_id), 0)) { if (!dm_pool_grow_object(rh->mem, _toupperstr(field_id), 0)) {
log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG); log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG);
free(field_id);
return 0; return 0;
} }
free(field_id);
if (!dm_pool_grow_object(rh->mem, STANDARD_PAIR, 1)) { if (!dm_pool_grow_object(rh->mem, STANDARD_PAIR, 1)) {
log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG); log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG);
return 0; return 0;
@ -4532,7 +4534,10 @@ static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field
/* Including trailing '\0'! */ /* Including trailing '\0'! */
buf_size = width + 1; buf_size = width + 1;
if (!(buf = malloc(buf_size))) { if (buf_size < sizeof(buf_local))
/* Use local buffer on stack for smaller strings */
buf = buf_local;
else if (!(buf = malloc(buf_size))) {
log_error("dm_report: Could not allocate memory for output line buffer."); log_error("dm_report: Could not allocate memory for output line buffer.");
return 0; return 0;
} }
@ -4574,10 +4579,12 @@ static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field
} }
} }
free(buf); if (buf != buf_local)
free(buf);
return 1; return 1;
bad: bad:
free(buf); if (buf != buf_local)
free(buf);
return 0; return 0;
} }

View File

@ -4482,6 +4482,7 @@ static const char *_get_field_id(struct dm_report *rh, struct dm_report_field *f
static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field *field) static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field *field)
{ {
char buf_local[8192];
char *field_id; char *field_id;
int32_t width; int32_t width;
uint32_t align; uint32_t align;
@ -4489,25 +4490,26 @@ static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field
size_t buf_size = 0; size_t buf_size = 0;
if (rh->flags & DM_REPORT_OUTPUT_FIELD_NAME_PREFIX) { if (rh->flags & DM_REPORT_OUTPUT_FIELD_NAME_PREFIX) {
if (!(field_id = strdup(_get_field_id(rh, field)))) { buf_size = strlen(_get_field_id(rh, field)) + 1;
log_error("dm_report: Failed to copy field name"); if (buf_size >= sizeof(buf_local)) {
/* for field names our buf_local should be enough */
log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG);
return 0; return 0;
} }
field_id = buf_local;
memcpy(field_id, _get_field_id(rh, field), buf_size);
if (!dm_pool_grow_object(rh->mem, rh->output_field_name_prefix, 0)) { if (!dm_pool_grow_object(rh->mem, rh->output_field_name_prefix, 0)) {
log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG); log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG);
dm_free(field_id);
return 0; return 0;
} }
if (!dm_pool_grow_object(rh->mem, _toupperstr(field_id), 0)) { if (!dm_pool_grow_object(rh->mem, _toupperstr(field_id), 0)) {
log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG); log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG);
dm_free(field_id);
return 0; return 0;
} }
dm_free(field_id);
if (!dm_pool_grow_object(rh->mem, STANDARD_PAIR, 1)) { if (!dm_pool_grow_object(rh->mem, STANDARD_PAIR, 1)) {
log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG); log_error(UNABLE_TO_EXTEND_OUTPUT_LINE_MSG);
return 0; return 0;
@ -4530,7 +4532,10 @@ static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field
/* Including trailing '\0'! */ /* Including trailing '\0'! */
buf_size = width + 1; buf_size = width + 1;
if (!(buf = dm_malloc(buf_size))) { if (buf_size < sizeof(buf_local))
/* Use local buffer on stack for smaller strings */
buf = buf_local;
else if (!(buf = dm_malloc(buf_size))) {
log_error("dm_report: Could not allocate memory for output line buffer."); log_error("dm_report: Could not allocate memory for output line buffer.");
return 0; return 0;
} }
@ -4572,10 +4577,12 @@ static int _output_field_basic_fmt(struct dm_report *rh, struct dm_report_field
} }
} }
dm_free(buf); if (buf != buf_local)
dm_free(buf);
return 1; return 1;
bad: bad:
dm_free(buf); if (buf != buf_local)
dm_free(buf);
return 0; return 0;
} }