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

Add dm_zalloc and use it and dm_pool_zalloc throughout.

This commit is contained in:
Alasdair Kergon 2010-09-30 21:06:50 +00:00
parent 0ca1492ca5
commit ac0252ca07
21 changed files with 87 additions and 93 deletions

View File

@ -1,8 +1,9 @@
Version 2.02.75 - Version 2.02.75 -
===================================== =====================================
Add dm_zalloc and use it and dm_pool_zalloc throughout.
Add pv_get_property and create generic internal _get_property function. Add pv_get_property and create generic internal _get_property function.
Add 'get' functions for pv and vg properties/fields. Add 'get' functions for pv and vg properties/fields.
Make generic GET_*_PROPERTY_FN macros and define secondary macro for vg, pv, lv. Make generic GET_*_PROPERTY_FN macros with secondary macro for vg, pv & lv.
Add tags_format_and_copy() common function and call from _tags_disp. Add tags_format_and_copy() common function and call from _tags_disp.
Add id_format_and_copy() common function and call from _uuid_disp. Add id_format_and_copy() common function and call from _uuid_disp.
Simplify logic to create '{pv|vg|lv}_attr' strings. Simplify logic to create '{pv|vg|lv}_attr' strings.

View File

@ -223,12 +223,11 @@ static void _debuglog(const char *fmt, ...)
static struct thread_status *_alloc_thread_status(struct message_data *data, static struct thread_status *_alloc_thread_status(struct message_data *data,
struct dso_data *dso_data) struct dso_data *dso_data)
{ {
struct thread_status *ret = (typeof(ret)) dm_malloc(sizeof(*ret)); struct thread_status *ret = (typeof(ret)) dm_zalloc(sizeof(*ret));
if (!ret) if (!ret)
return NULL; return NULL;
memset(ret, 0, sizeof(*ret));
if (!(ret->device.uuid = dm_strdup(data->device_uuid))) { if (!(ret->device.uuid = dm_strdup(data->device_uuid))) {
dm_free(ret); dm_free(ret);
return NULL; return NULL;
@ -259,12 +258,11 @@ static void _free_thread_status(struct thread_status *thread)
/* Allocate/free DSO data. */ /* Allocate/free DSO data. */
static struct dso_data *_alloc_dso_data(struct message_data *data) static struct dso_data *_alloc_dso_data(struct message_data *data)
{ {
struct dso_data *ret = (typeof(ret)) dm_malloc(sizeof(*ret)); struct dso_data *ret = (typeof(ret)) dm_zalloc(sizeof(*ret));
if (!ret) if (!ret)
return NULL; return NULL;
memset(ret, 0, sizeof(*ret));
if (!(ret->dso_name = dm_strdup(data->dso_name))) { if (!(ret->dso_name = dm_strdup(data->dso_name))) {
dm_free(ret); dm_free(ret);
return NULL; return NULL;

View File

@ -1064,11 +1064,10 @@ static int _lvmcache_update_vgname(struct lvmcache_info *info,
} }
} else { } else {
***/ ***/
if (!(vginfo = dm_malloc(sizeof(*vginfo)))) { if (!(vginfo = dm_zalloc(sizeof(*vginfo)))) {
log_error("lvmcache_update_vgname: list alloc failed"); log_error("lvmcache_update_vgname: list alloc failed");
return 0; return 0;
} }
memset(vginfo, 0, sizeof(*vginfo));
if (!(vginfo->vgname = dm_strdup(vgname))) { if (!(vginfo->vgname = dm_strdup(vgname))) {
dm_free(vginfo); dm_free(vginfo);
log_error("cache vgname alloc failed for %s", vgname); log_error("cache vgname alloc failed for %s", vgname);
@ -1261,12 +1260,11 @@ struct lvmcache_info *lvmcache_add(struct labeller *labeller, const char *pvid,
!(existing = info_from_pvid(dev->pvid, 0))) { !(existing = info_from_pvid(dev->pvid, 0))) {
if (!(label = label_create(labeller))) if (!(label = label_create(labeller)))
return_NULL; return_NULL;
if (!(info = dm_malloc(sizeof(*info)))) { if (!(info = dm_zalloc(sizeof(*info)))) {
log_error("lvmcache_info allocation failed"); log_error("lvmcache_info allocation failed");
label_destroy(label); label_destroy(label);
return NULL; return NULL;
} }
memset(info, 0, sizeof(*info));
label->info = info; label->info = info;
info->label = label; info->label = label;

View File

@ -1121,11 +1121,10 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
init_syslog(DEFAULT_LOG_FACILITY); init_syslog(DEFAULT_LOG_FACILITY);
if (!(cmd = dm_malloc(sizeof(*cmd)))) { if (!(cmd = dm_zalloc(sizeof(*cmd)))) {
log_error("Failed to allocate command context"); log_error("Failed to allocate command context");
return NULL; return NULL;
} }
memset(cmd, 0, sizeof(*cmd));
cmd->is_long_lived = is_long_lived; cmd->is_long_lived = is_long_lived;
cmd->handles_missing_pvs = 0; cmd->handles_missing_pvs = 0;
cmd->handles_unknown_segments = 0; cmd->handles_unknown_segments = 0;

View File

@ -724,6 +724,8 @@ static void _get_token(struct parser *p, int tok_prev)
{ {
int values_allowed = 0; int values_allowed = 0;
const char *te;
p->tb = p->te; p->tb = p->te;
_eat_space(p); _eat_space(p);
if (p->tb == p->fe || !*p->tb) { if (p->tb == p->fe || !*p->tb) {
@ -738,59 +740,61 @@ static void _get_token(struct parser *p, int tok_prev)
p->t = TOK_INT; /* fudge so the fall through for p->t = TOK_INT; /* fudge so the fall through for
floats works */ floats works */
switch (*p->te) {
te = p->te;
switch (*te) {
case SECTION_B_CHAR: case SECTION_B_CHAR:
p->t = TOK_SECTION_B; p->t = TOK_SECTION_B;
p->te++; te++;
break; break;
case SECTION_E_CHAR: case SECTION_E_CHAR:
p->t = TOK_SECTION_E; p->t = TOK_SECTION_E;
p->te++; te++;
break; break;
case '[': case '[':
p->t = TOK_ARRAY_B; p->t = TOK_ARRAY_B;
p->te++; te++;
break; break;
case ']': case ']':
p->t = TOK_ARRAY_E; p->t = TOK_ARRAY_E;
p->te++; te++;
break; break;
case ',': case ',':
p->t = TOK_COMMA; p->t = TOK_COMMA;
p->te++; te++;
break; break;
case '=': case '=':
p->t = TOK_EQ; p->t = TOK_EQ;
p->te++; te++;
break; break;
case '"': case '"':
p->t = TOK_STRING_ESCAPED; p->t = TOK_STRING_ESCAPED;
p->te++; te++;
while ((p->te != p->fe) && (*p->te) && (*p->te != '"')) { while ((te != p->fe) && (*te) && (*te != '"')) {
if ((*p->te == '\\') && (p->te + 1 != p->fe) && if ((*te == '\\') && (te + 1 != p->fe) &&
*(p->te + 1)) *(te + 1))
p->te++; te++;
p->te++; te++;
} }
if ((p->te != p->fe) && (*p->te)) if ((te != p->fe) && (*te))
p->te++; te++;
break; break;
case '\'': case '\'':
p->t = TOK_STRING; p->t = TOK_STRING;
p->te++; te++;
while ((p->te != p->fe) && (*p->te) && (*p->te != '\'')) while ((te != p->fe) && (*te) && (*te != '\''))
p->te++; te++;
if ((p->te != p->fe) && (*p->te)) if ((te != p->fe) && (*te))
p->te++; te++;
break; break;
case '.': case '.':
@ -808,28 +812,30 @@ static void _get_token(struct parser *p, int tok_prev)
case '+': case '+':
case '-': case '-':
if (values_allowed) { if (values_allowed) {
p->te++; te++;
while ((p->te != p->fe) && (*p->te)) { while ((te != p->fe) && (*te)) {
if (*p->te == '.') { if (*te == '.') {
if (p->t == TOK_FLOAT) if (p->t == TOK_FLOAT)
break; break;
p->t = TOK_FLOAT; p->t = TOK_FLOAT;
} else if (!isdigit((int) *p->te)) } else if (!isdigit((int) *te))
break; break;
p->te++; te++;
} }
break; break;
} }
default: default:
p->t = TOK_IDENTIFIER; p->t = TOK_IDENTIFIER;
while ((p->te != p->fe) && (*p->te) && !isspace(*p->te) && while ((te != p->fe) && (*te) && !isspace(*te) &&
(*p->te != '#') && (*p->te != '=') && (*te != '#') && (*te != '=') &&
(*p->te != SECTION_B_CHAR) && (*te != SECTION_B_CHAR) &&
(*p->te != SECTION_E_CHAR)) (*te != SECTION_E_CHAR))
p->te++; te++;
break; break;
} }
p->te = te;
} }
static void _eat_space(struct parser *p) static void _eat_space(struct parser *p)
@ -859,22 +865,12 @@ static void _eat_space(struct parser *p)
*/ */
static struct config_value *_create_value(struct dm_pool *mem) static struct config_value *_create_value(struct dm_pool *mem)
{ {
struct config_value *v = dm_pool_alloc(mem, sizeof(*v)); return dm_pool_zalloc(mem, sizeof(struct config_value));
if (v)
memset(v, 0, sizeof(*v));
return v;
} }
static struct config_node *_create_node(struct dm_pool *mem) static struct config_node *_create_node(struct dm_pool *mem)
{ {
struct config_node *n = dm_pool_alloc(mem, sizeof(*n)); return dm_pool_zalloc(mem, sizeof(struct config_node));
if (n)
memset(n, 0, sizeof(*n));
return n;
} }
static char *_dup_tok(struct parser *p) static char *_dup_tok(struct parser *p)

View File

@ -318,9 +318,8 @@ struct dev_filter *persistent_filter_create(struct dev_filter *real,
struct dev_filter *f = NULL; struct dev_filter *f = NULL;
struct stat info; struct stat info;
if (!(pf = dm_malloc(sizeof(*pf)))) if (!(pf = dm_zalloc(sizeof(*pf))))
return_NULL; return_NULL;
memset(pf, 0, sizeof(*pf));
if (!(pf->file = dm_malloc(strlen(file) + 1))) if (!(pf->file = dm_malloc(strlen(file) + 1)))
goto_bad; goto_bad;

View File

@ -624,13 +624,12 @@ static int _write_pvd(struct disk_list *data)
/* Make sure that the gap between the PV structure and /* Make sure that the gap between the PV structure and
the next one is zeroed in order to make non LVM tools the next one is zeroed in order to make non LVM tools
happy (idea from AED) */ happy (idea from AED) */
buf = dm_malloc(size); buf = dm_zalloc(size);
if (!buf) { if (!buf) {
log_error("Couldn't allocate temporary PV buffer."); log_error("Couldn't allocate temporary PV buffer.");
return 0; return 0;
} }
memset(buf, 0, size);
memcpy(buf, &data->pvd, sizeof(struct pv_disk)); memcpy(buf, &data->pvd, sizeof(struct pv_disk));
log_debug("Writing %s PV metadata to %s at %" PRIu64 " len %" log_debug("Writing %s PV metadata to %s at %" PRIu64 " len %"

View File

@ -157,7 +157,7 @@ static struct volume_group *_build_vg(struct format_instance *fid,
struct dm_list *pvs, struct dm_list *pvs,
struct dm_pool *mem) struct dm_pool *mem)
{ {
struct volume_group *vg = dm_pool_alloc(mem, sizeof(*vg)); struct volume_group *vg = dm_pool_zalloc(mem, sizeof(*vg));
struct disk_list *dl; struct disk_list *dl;
if (!vg) if (!vg)
@ -166,8 +166,6 @@ static struct volume_group *_build_vg(struct format_instance *fid,
if (dm_list_empty(pvs)) if (dm_list_empty(pvs))
goto_bad; goto_bad;
memset(vg, 0, sizeof(*vg));
vg->cmd = fid->fmt->cmd; vg->cmd = fid->fmt->cmd;
vg->vgmem = mem; vg->vgmem = mem;
vg->fid = fid; vg->fid = fid;

View File

@ -506,9 +506,8 @@ int export_lvs(struct disk_list *dl, struct volume_group *vg,
* setup the pv's extents array * setup the pv's extents array
*/ */
len = sizeof(struct pe_disk) * dl->pvd.pe_total; len = sizeof(struct pe_disk) * dl->pvd.pe_total;
if (!(dl->extents = dm_pool_alloc(dl->mem, len))) if (!(dl->extents = dm_pool_zalloc(dl->mem, len)))
goto_out; goto_out;
memset(dl->extents, 0, len);
dm_list_iterate_items(ll, &vg->lvs) { dm_list_iterate_items(ll, &vg->lvs) {
if (ll->lv->status & SNAPSHOT) if (ll->lv->status & SNAPSHOT)

View File

@ -742,10 +742,9 @@ int text_vg_export_file(struct volume_group *vg, const char *desc, FILE *fp)
_init(); _init();
if (!(f = dm_malloc(sizeof(*f)))) if (!(f = dm_zalloc(sizeof(*f))))
return_0; return_0;
memset(f, 0, sizeof(*f));
f->data.fp = fp; f->data.fp = fp;
f->indent = 0; f->indent = 0;
f->header = 1; f->header = 1;
@ -767,11 +766,9 @@ int text_vg_export_raw(struct volume_group *vg, const char *desc, char **buf)
_init(); _init();
if (!(f = dm_malloc(sizeof(*f)))) if (!(f = dm_zalloc(sizeof(*f))))
return_0; return_0;
memset(f, 0, sizeof(*f));
f->data.buf.size = 65536; /* Initial metadata limit */ f->data.buf.size = 65536; /* Initial metadata limit */
if (!(f->data.buf.start = dm_malloc(f->data.buf.size))) { if (!(f->data.buf.start = dm_malloc(f->data.buf.size))) {
log_error("text_export buffer allocation failed"); log_error("text_export buffer allocation failed");

View File

@ -383,11 +383,10 @@ struct label *label_create(struct labeller *labeller)
{ {
struct label *label; struct label *label;
if (!(label = dm_malloc(sizeof(*label)))) { if (!(label = dm_zalloc(sizeof(*label)))) {
log_error("label allocaction failed"); log_error("label allocaction failed");
return NULL; return NULL;
} }
memset(label, 0, sizeof(*label));
label->labeller = labeller; label->labeller = labeller;

View File

@ -26,8 +26,8 @@ dm_bitset_t dm_bitset_create(struct dm_pool *mem, unsigned num_bits)
if (mem) if (mem)
bs = dm_pool_zalloc(mem, size); bs = dm_pool_zalloc(mem, size);
else if ((bs = dm_malloc(size))) else
memset(bs, 0, size); bs = dm_zalloc(size);
if (!bs) if (!bs)
return NULL; return NULL;

View File

@ -90,14 +90,10 @@ struct dm_hash_table *dm_hash_create(unsigned size_hint)
{ {
size_t len; size_t len;
unsigned new_size = 16u; unsigned new_size = 16u;
struct dm_hash_table *hc = dm_malloc(sizeof(*hc)); struct dm_hash_table *hc = dm_zalloc(sizeof(*hc));
if (!hc) { if (!hc)
stack; return_0;
return 0;
}
memset(hc, 0, sizeof(*hc));
/* round size hint up to a power of two */ /* round size hint up to a power of two */
while (new_size < size_hint) while (new_size < size_hint)

View File

@ -511,6 +511,8 @@ uint32_t dm_tree_get_cookie(struct dm_tree_node *node);
void *dm_malloc_aux(size_t s, const char *file, int line); void *dm_malloc_aux(size_t s, const char *file, int line);
void *dm_malloc_aux_debug(size_t s, const char *file, int line); void *dm_malloc_aux_debug(size_t s, const char *file, int line);
void *dm_zalloc_aux(size_t s, const char *file, int line);
void *dm_zalloc_aux_debug(size_t s, const char *file, int line);
char *dm_strdup_aux(const char *str, const char *file, int line); char *dm_strdup_aux(const char *str, const char *file, int line);
void dm_free_aux(void *p); void dm_free_aux(void *p);
void *dm_realloc_aux(void *p, unsigned int s, const char *file, int line); void *dm_realloc_aux(void *p, unsigned int s, const char *file, int line);
@ -520,6 +522,7 @@ void dm_bounds_check_debug(void);
#ifdef DEBUG_MEM #ifdef DEBUG_MEM
# define dm_malloc(s) dm_malloc_aux_debug((s), __FILE__, __LINE__) # define dm_malloc(s) dm_malloc_aux_debug((s), __FILE__, __LINE__)
# define dm_zalloc(s) dm_zalloc_aux_debug((s), __FILE__, __LINE__)
# define dm_strdup(s) dm_strdup_aux((s), __FILE__, __LINE__) # define dm_strdup(s) dm_strdup_aux((s), __FILE__, __LINE__)
# define dm_free(p) dm_free_aux(p) # define dm_free(p) dm_free_aux(p)
# define dm_realloc(p, s) dm_realloc_aux(p, s, __FILE__, __LINE__) # define dm_realloc(p, s) dm_realloc_aux(p, s, __FILE__, __LINE__)
@ -529,6 +532,7 @@ void dm_bounds_check_debug(void);
#else #else
# define dm_malloc(s) dm_malloc_aux((s), __FILE__, __LINE__) # define dm_malloc(s) dm_malloc_aux((s), __FILE__, __LINE__)
# define dm_zalloc(s) dm_zalloc_aux((s), __FILE__, __LINE__)
# define dm_strdup(s) strdup(s) # define dm_strdup(s) strdup(s)
# define dm_free(p) free(p) # define dm_free(p) free(p)
# define dm_realloc(p, s) realloc(p, s) # define dm_realloc(p, s) realloc(p, s)

View File

@ -166,7 +166,7 @@ int dm_get_library_version(char *version, size_t size)
struct dm_task *dm_task_create(int type) struct dm_task *dm_task_create(int type)
{ {
struct dm_task *dmt = dm_malloc(sizeof(*dmt)); struct dm_task *dmt = dm_zalloc(sizeof(*dmt));
if (!dmt) { if (!dmt) {
log_error("dm_task_create: malloc(%" PRIsize_t ") failed", log_error("dm_task_create: malloc(%" PRIsize_t ") failed",
@ -179,8 +179,6 @@ struct dm_task *dm_task_create(int type)
return NULL; return NULL;
} }
memset(dmt, 0, sizeof(*dmt));
dmt->type = type; dmt->type = type;
dmt->minor = -1; dmt->minor = -1;
dmt->major = -1; dmt->major = -1;

View File

@ -197,12 +197,11 @@ struct dm_tree *dm_tree_create(void)
{ {
struct dm_tree *dtree; struct dm_tree *dtree;
if (!(dtree = dm_malloc(sizeof(*dtree)))) { if (!(dtree = dm_zalloc(sizeof(*dtree)))) {
log_error("dm_tree_create malloc failed"); log_error("dm_tree_create malloc failed");
return NULL; return NULL;
} }
memset(dtree, 0, sizeof(*dtree));
dtree->root.dtree = dtree; dtree->root.dtree = dtree;
dm_list_init(&dtree->root.uses); dm_list_init(&dtree->root.uses);
dm_list_init(&dtree->root.used_by); dm_list_init(&dtree->root.used_by);

View File

@ -571,11 +571,10 @@ struct dm_report *dm_report_init(uint32_t *report_types,
struct dm_report *rh; struct dm_report *rh;
const struct dm_report_object_type *type; const struct dm_report_object_type *type;
if (!(rh = dm_malloc(sizeof(*rh)))) { if (!(rh = dm_zalloc(sizeof(*rh)))) {
log_error("dm_report_init: dm_malloc failed"); log_error("dm_report_init: dm_malloc failed");
return 0; return 0;
} }
memset(rh, 0, sizeof(*rh));
/* /*
* rh->report_types is updated in _parse_fields() and _parse_keys() * rh->report_types is updated in _parse_fields() and _parse_keys()

View File

@ -119,6 +119,16 @@ void *dm_malloc_aux_debug(size_t s, const char *file, int line)
return nb + 1; return nb + 1;
} }
void *dm_zalloc_aux_debug(size_t s, const char *file, int line)
{
void *ptr = dm_malloc_aux_debug(s, file, line);
if (ptr)
memset(ptr, 0, s);
return ptr;
}
void dm_free_aux(void *p) void dm_free_aux(void *p)
{ {
char *ptr; char *ptr;
@ -250,3 +260,13 @@ void *dm_malloc_aux(size_t s, const char *file __attribute__((unused)),
return malloc(s); return malloc(s);
} }
void *dm_zalloc_aux(size_t s, const char *file, int line)
{
void *ptr = dm_malloc_aux(s, file, line);
if (ptr)
memset(ptr, 0, s);
return ptr;
}

View File

@ -43,14 +43,13 @@ static void _free_chunk(struct chunk *c);
struct dm_pool *dm_pool_create(const char *name, size_t chunk_hint) struct dm_pool *dm_pool_create(const char *name, size_t chunk_hint)
{ {
size_t new_size = 1024; size_t new_size = 1024;
struct dm_pool *p = dm_malloc(sizeof(*p)); struct dm_pool *p = dm_zalloc(sizeof(*p));
if (!p) { if (!p) {
log_error("Couldn't create memory pool %s (size %" log_error("Couldn't create memory pool %s (size %"
PRIsize_t ")", name, sizeof(*p)); PRIsize_t ")", name, sizeof(*p));
return 0; return 0;
} }
memset(p, 0, sizeof(*p));
/* round chunk_hint up to the next power of 2 */ /* round chunk_hint up to the next power of 2 */
p->chunk_size = chunk_hint + sizeof(struct chunk); p->chunk_size = chunk_hint + sizeof(struct chunk);

View File

@ -320,11 +320,9 @@ struct dm_regex *dm_regex_create(struct dm_pool *mem, const char **patterns,
struct dm_regex *m; struct dm_regex *m;
struct dm_pool *scratch = mem; struct dm_pool *scratch = mem;
if (!(m = dm_pool_alloc(mem, sizeof(*m)))) if (!(m = dm_pool_zalloc(mem, sizeof(*m))))
return_NULL; return_NULL;
memset(m, 0, sizeof(*m));
/* join the regexps together, delimiting with zero */ /* join the regexps together, delimiting with zero */
for (i = 0; i < num_patterns; i++) for (i = 0; i < num_patterns; i++)
len += strlen(patterns[i]) + 8; len += strlen(patterns[i]) + 8;

View File

@ -730,13 +730,11 @@ static int _message(int argc, char **argv, void *data __attribute__((unused)))
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
sz += strlen(argv[i]) + 1; sz += strlen(argv[i]) + 1;
if (!(str = dm_malloc(sz))) { if (!(str = dm_zalloc(sz))) {
err("message string allocation failed"); err("message string allocation failed");
goto out; goto out;
} }
memset(str, 0, sz);
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (i) if (i)
strcat(str, " "); strcat(str, " ");