tag: introduce handy items_size() function

Trying to read or remember
  "tag->numOfItems * sizeof(*tag->items)"
requires too much thinking and mental effort on my part.

Also, favor "sizeof(struct mpd_tag)" over "sizeof(*tag->items)"
because the former is easier to read and follow, even though
the latter is easier to modify if the items member changes
to a different type.
This commit is contained in:
Eric Wong 2008-09-07 19:14:43 +02:00 committed by Max Kellermann
parent 4dd9d4b2fd
commit 194c8c3c0f

View File

@ -55,6 +55,11 @@ const char *mpdTagItemKeys[TAG_NUM_OF_ITEM_TYPES] = {
mpd_sint8 ignoreTagItems[TAG_NUM_OF_ITEM_TYPES]; mpd_sint8 ignoreTagItems[TAG_NUM_OF_ITEM_TYPES];
static size_t items_size(const struct tag *tag)
{
return (tag->numOfItems * sizeof(struct tag_item));
}
void tag_lib_init(void) void tag_lib_init(void)
{ {
int quit = 0; int quit = 0;
@ -248,8 +253,7 @@ static void deleteItem(struct tag *tag, int idx)
} }
if (tag->numOfItems > 0) { if (tag->numOfItems > 0) {
tag->items = xrealloc(tag->items, tag->items = xrealloc(tag->items, items_size(tag));
tag->numOfItems * sizeof(*tag->items));
} else { } else {
free(tag->items); free(tag->items);
tag->items = NULL; tag->items = NULL;
@ -311,7 +315,7 @@ struct tag *tag_dup(const struct tag *tag)
ret = tag_new(); ret = tag_new();
ret->time = tag->time; ret->time = tag->time;
ret->numOfItems = tag->numOfItems; ret->numOfItems = tag->numOfItems;
ret->items = xmalloc(ret->numOfItems * sizeof(ret->items[0])); ret->items = ret->numOfItems > 0 ? xmalloc(items_size(tag)) : NULL;
for (i = 0; i < tag->numOfItems; i++) { for (i = 0; i < tag->numOfItems; i++) {
ret->items[i] = tag_pool_dup_item(tag->items[i]); ret->items[i] = tag_pool_dup_item(tag->items[i]);
@ -381,10 +385,8 @@ void tag_end_add(struct tag *tag)
if (tag->numOfItems > 0) { if (tag->numOfItems > 0) {
/* copy the tag items from the bulk list over /* copy the tag items from the bulk list over
to a new list (which fits exactly) */ to a new list (which fits exactly) */
tag->items = xmalloc(tag->numOfItems * tag->items = xmalloc(items_size(tag));
sizeof(tag->items[0])); memcpy(tag->items, bulk.items, items_size(tag));
memcpy(tag->items, bulk.items,
tag->numOfItems * sizeof(tag->items[0]));
} else } else
tag->items = NULL; tag->items = NULL;
} }
@ -416,15 +418,14 @@ static void appendToTagItems(struct tag *tag, enum tag_type type,
if (tag->items != bulk.items) if (tag->items != bulk.items)
/* bulk mode disabled */ /* bulk mode disabled */
tag->items = xrealloc(tag->items, tag->items = xrealloc(tag->items, items_size(tag));
tag->numOfItems * sizeof(*tag->items));
else if (tag->numOfItems >= BULK_MAX) { else if (tag->numOfItems >= BULK_MAX) {
/* bulk list already full - switch back to non-bulk */ /* bulk list already full - switch back to non-bulk */
assert(bulk.busy); assert(bulk.busy);
tag->items = xmalloc(tag->numOfItems * sizeof(tag->items[0])); tag->items = xmalloc(items_size(tag));
memcpy(tag->items, bulk.items, memcpy(tag->items, bulk.items,
(tag->numOfItems - 1) * sizeof(tag->items[0])); items_size(tag) - sizeof(struct tag_item));
} }
tag->items[i] = tag_pool_get_item(type, p, len); tag->items[i] = tag_pool_get_item(type, p, len);