Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions util/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,32 @@ void table_add_row(struct table *t, int row_id)

struct table *table_init(void)
{
struct table *t;
struct table *t = malloc(sizeof(struct table));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function could just be return calloc(1, sizeof(struct table));. Maybe we should drop the function as there is no real value in it and use calloc directly where currently table_init is used. Or do I miss something?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I see, table_free is doing some work and to keep the symetry table_init makes sense. But I think the name shouldbe table_create or so. It's not initializing anything, it's just allocation it.


t = malloc(sizeof(struct table));
if (!t)
return NULL;

memset(t, 0, sizeof(struct table));

return t;
}

struct table *table_init_with_columns(struct table_column *c, int num_columns)
{
struct table *t = table_init();

if (!t)
return NULL;

if (table_add_columns(t, c, num_columns)) {
table_free(t);
return NULL;
}

return t;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary empy line.

}

static int table_add_column(struct table *t, struct table_column *c)
{
struct table_column *new_columns;
Expand Down
12 changes: 12 additions & 0 deletions util/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,16 @@ void table_add_row(struct table *t, int row);
void table_print(struct table *t);
void table_free(struct table *t);

/**
* table_init_with_columns() - Allocate a table instance with column definitions
* @c: Column definitions
* @num_columns:Number of columns
*
* This is a function combined table_init() and table_add_columns().
*
* Return: The table instance, or NULL if unsuccessful. If allocated, the caller
* is responsible to free the table.
*/
struct table *table_init_with_columns(struct table_column *c, int num_columns);

#endif /* _TABLE_H_ */