Skip to contentSkip to conversation

const correctness, first cut

PATCH Jim Meyering 3 participants 6 messages Aug 6, 2007 – Aug 7, 2007

lore
Share & tools
lore
#0 lore raw

I find it easier to read code that uses "const" whenever possible. Then, absence of "const" implies that there is at least one code path through which the pointed-to data can be modified. Here's a first cut at adding some "const" attributes:

In ensuring that things make sense and there are still no compile-time warnings, I ended up factoring out the function pointer types for a couple of the iterators, e.g., process_each_lv_in_vg. Here are the names I chose:

typedef int (*process_single_pv) (struct cmd_context *cmd,
...

typedef int (*process_single_lv) (struct cmd_context *cmd,
...

I was tempted to use the _fn_t suffix, but technically _t is a reserved suffix. However, there is precedent:

fd_callback_t
checksum_fn_t
lvm2_log_fn_t

In writing this, I've decided to go with the _fn_t suffix after all:

typedef int (*process_single_pv_fn_t) (struct cmd_context *cmd,
...

typedef int (*process_single_lv_fn_t) (struct cmd_context *cmd,
...

Speak now if you'd like something else. Barring objections, I'll commit this in a couple hours.

---
 daemons/clvmd/lvm-functions.c    |    2 +-
 daemons/clvmd/lvm-functions.h    |    2 +-
 daemons/clvmd/refresh_clvmd.c    |    2 +-
 lib/activate/activate.c          |    4 +-
 lib/activate/activate.h          |    2 +-
 lib/datastruct/list.c            |   14 +++++-----
 lib/datastruct/list.h            |   14 +++++-----
 lib/datastruct/str_list.c        |    9 ++++---
 lib/datastruct/str_list.h        |    9 ++++---
 lib/display/display.c            |   49 ++++++++++++++++++++-----------------
 lib/display/display.h            |   36 ++++++++++++++-------------
 lib/metadata/metadata-exported.h |    2 +-
 lib/metadata/metadata.c          |    2 +-
 tools/toollib.c                  |   19 ++++++--------
 tools/toollib.h                  |   28 +++++++++++++--------
 tools/tools.h                    |    2 +-
 tools/vgdisplay.c                |    7 +++--
 17 files changed, 107 insertions(+), 96 deletions(-)
daemons/clvmd/lvm-functions.c +1 -1
index 56219ac..b8da034 100644
@@ -431,7 +431,7 @@ int post_lock_lv(unsigned char command, unsigned char lock_flags,
 }
 
 /* Check if a VG is un use by LVM1 so we don't stomp on it */
-int do_check_lvm1(char *vgname)
+int do_check_lvm1(const char *vgname)
 {
 	int status;
 
daemons/clvmd/lvm-functions.h +1 -1
index d04964b..b1e4da1 100644
@@ -24,7 +24,7 @@ extern int do_lock_lv(unsigned char lock_cmd, unsigned char lock_flags,
 		      char *resource);
 extern int post_lock_lv(unsigned char lock_cmd, unsigned char lock_flags,
 			char *resource);
-extern int do_check_lvm1(char *vgname);
+extern int do_check_lvm1(const char *vgname);
 extern int do_refresh_cache(void);
 extern int init_lvm(int using_gulm);
 extern void init_lvhash(void);
daemons/clvmd/refresh_clvmd.c +1 -1
index 5103ec3..3a455b8 100644
@@ -79,7 +79,7 @@ static int _open_local_sock(void)
 }
 
 /* Send a request and return the status */
-static int _send_request(char *inbuf, int inlen, char **retbuf)
+static int _send_request(const char *inbuf, int inlen, char **retbuf)
 {
 	char outbuf[PIPE_BUF];
 	struct clvm_header *outheader = (struct clvm_header *) outbuf;
lib/activate/activate.c +2 -2
index c262ff3..51f393c 100644
@@ -642,9 +642,9 @@ int lvs_in_vg_activated(struct volume_group *vg)
 	return _lvs_in_vg_activated(vg, 0);
 }
 
-int lvs_in_vg_opened(struct volume_group *vg)
+int lvs_in_vg_opened(const struct volume_group *vg)
 {
-	struct lv_list *lvl;
+	const struct lv_list *lvl;
 	int count = 0;
 
 	if (!activation())
lib/activate/activate.h +1 -1
index c3e9700..76d27e2 100644
@@ -84,7 +84,7 @@ int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv,
  */
 int lvs_in_vg_activated(struct volume_group *vg);
 int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg);
-int lvs_in_vg_opened(struct volume_group *vg);
+int lvs_in_vg_opened(const struct volume_group *vg);
 
 
 int monitor_dev_for_events(struct cmd_context *cmd,
lib/datastruct/list.c +7 -7
index 17ee0e2..3918cf5 100644
@@ -68,7 +68,7 @@ void list_del(struct list *elem)
 /*
  * Is the list empty?
  */
-int list_empty(struct list *head)
+int list_empty(const struct list *head)
 {
 	return head->n == head;
 }
@@ -76,7 +76,7 @@ int list_empty(struct list *head)
 /*
  * Is this the first element of the list?
  */
-int list_start(struct list *head, struct list *elem)
+int list_start(const struct list *head, const struct list *elem)
 {
 	return elem->p == head;
 }
@@ -84,7 +84,7 @@ int list_start(struct list *head, struct list *elem)
 /*
  * Is this the last element of the list?
  */
-int list_end(struct list *head, struct list *elem)
+int list_end(const struct list *head, const struct list *elem)
 {
 	return elem->n == head;
 }
@@ -92,7 +92,7 @@ int list_end(struct list *head, struct list *elem)
 /*
  * Return first element of the list or NULL if empty
  */
-struct list *list_first(struct list *head)
+struct list *list_first(const struct list *head)
 {
 	return (list_empty(head) ? NULL : head->n);
 }
@@ -100,7 +100,7 @@ struct list *list_first(struct list *head)
 /*
  * Return last element of the list or NULL if empty
  */
-struct list *list_last(struct list *head)
+struct list *list_last(const struct list *head)
 {
 	return (list_empty(head) ? NULL : head->p);
 }
@@ -108,7 +108,7 @@ struct list *list_last(struct list *head)
 /*
  * Return the previous element of the list, or NULL if we've reached the start.
  */
-struct list *list_prev(struct list *head, struct list *elem)
+struct list *list_prev(const struct list *head, const struct list *elem)
 {
 	return (list_start(head, elem) ? NULL : elem->p);
 }
@@ -116,7 +116,7 @@ struct list *list_prev(struct list *head, struct list *elem)
 /*
  * Return the next element of the list, or NULL if we've reached the end.
  */
-struct list *list_next(struct list *head, struct list *elem)
+struct list *list_next(const struct list *head, const struct list *elem)
 {
 	return (list_end(head, elem) ? NULL : elem->n);
 }
lib/datastruct/list.h +7 -7
index 316c537..00d4d74 100644
@@ -57,37 +57,37 @@ void list_del(struct list *elem);
 /*
  * Is the list empty?
  */
-int list_empty(struct list *head);
+int list_empty(const struct list *head);
 
 /*
  * Is this the first element of the list?
  */
-int list_start(struct list *head, struct list *elem);
+int list_start(const struct list *head, const struct list *elem);
 
 /*
  * Is this the last element of the list?
  */
-int list_end(struct list *head, struct list *elem);
+int list_end(const struct list *head, const struct list *elem);
 
 /*
  * Return first element of the list or NULL if empty
  */
-struct list *list_first(struct list *head);
+struct list *list_first(const struct list *head);
 
 /*
  * Return last element of the list or NULL if empty
  */
-struct list *list_last(struct list *head);
+struct list *list_last(const struct list *head);
 
 /*
  * Return the previous element of the list, or NULL if we've reached the start.
  */
-struct list *list_prev(struct list *head, struct list *elem);
+struct list *list_prev(const struct list *head, const struct list *elem);
 
 /*
  * Return the next element of the list, or NULL if we've reached the end.
  */
-struct list *list_next(struct list *head, struct list *elem);
+struct list *list_next(const struct list *head, const struct list *elem);
 
 /*
  * Given the address v of an instance of 'struct list' called 'head' 
lib/datastruct/str_list.c +5 -4
index 94705dc..68660d9 100644
@@ -66,7 +66,8 @@ int str_list_del(struct list *sll, const char *str)
 	return 1;
 }
 
-int str_list_dup(struct dm_pool *mem, struct list *sllnew, struct list *sllold)
+int str_list_dup(struct dm_pool *mem, struct list *sllnew,
+		 const struct list *sllold)
 {
 	struct str_list *sl;
 
@@ -85,7 +86,7 @@ int str_list_dup(struct dm_pool *mem, struct list *sllnew, struct list *sllold)
 /*
  * Is item on list?
  */
-int str_list_match_item(struct list *sll, const char *str)
+int str_list_match_item(const struct list *sll, const char *str)
 {
 	struct str_list *sl;
 
@@ -99,7 +100,7 @@ int str_list_match_item(struct list *sll, const char *str)
 /*
  * Is at least one item on both lists?
  */
-int str_list_match_list(struct list *sll, struct list *sll2)
+int str_list_match_list(const struct list *sll, const struct list *sll2)
 {
 	struct str_list *sl;
 
@@ -113,7 +114,7 @@ int str_list_match_list(struct list *sll, struct list *sll2)
 /*
  * Do both lists contain the same set of items?
  */
-int str_list_lists_equal(struct list *sll, struct list *sll2)
+int str_list_lists_equal(const struct list *sll, const struct list *sll2)
 {
 	struct str_list *sl;
 
lib/datastruct/str_list.h +5 -4
index 329a1f6..46fb76d 100644
@@ -19,9 +19,10 @@
 struct list *str_list_create(struct dm_pool *mem);
 int str_list_add(struct dm_pool *mem, struct list *sll, const char *str);
 int str_list_del(struct list *sll, const char *str);
-int str_list_match_item(struct list *sll, const char *str);
-int str_list_match_list(struct list *sll, struct list *sll2);
-int str_list_lists_equal(struct list *sll, struct list *sll2);
-int str_list_dup(struct dm_pool *mem, struct list *sllnew, struct list *sllold);
+int str_list_match_item(const struct list *sll, const char *str);
+int str_list_match_list(const struct list *sll, const struct list *sll2);
+int str_list_lists_equal(const struct list *sll, const struct list *sll2);
+int str_list_dup(struct dm_pool *mem, struct list *sllnew,
+		 const struct list *sllold);
 
 #endif
lib/display/display.c +26 -23

147 lines not on this page · raw message

lib/display/display.h +19 -17
index 7605e15..94f9c0e 100644
@@ -23,32 +23,34 @@
 uint64_t units_to_bytes(const char *units, char *unit_type);
 
 /* Specify size in KB */
-const char *display_size(struct cmd_context *cmd, uint64_t size);
-const char *display_size_long(struct cmd_context *cmd, uint64_t size);
-const char *display_size_units(struct cmd_context *cmd, uint64_t size);
+const char *display_size(const struct cmd_context *cmd, uint64_t size);
+const char *display_size_long(const struct cmd_context *cmd, uint64_t size);
+const char *display_size_units(const struct cmd_context *cmd, uint64_t size);
 
 char *display_uuid(char *uuidstr);
 void display_stripe(const struct lv_segment *seg, uint32_t s, const char *pre);
 
-void pvdisplay_colons(struct physical_volume *pv);
-void pvdisplay_segments(struct physical_volume *pv);
-void pvdisplay_full(struct cmd_context *cmd, struct physical_volume *pv,
+void pvdisplay_colons(const struct physical_volume *pv);
+void pvdisplay_segments(const struct physical_volume *pv);
+void pvdisplay_full(const struct cmd_context *cmd,
+		    const struct physical_volume *pv,
 		    void *handle);
-int pvdisplay_short(struct cmd_context *cmd, struct volume_group *vg,
-		    struct physical_volume *pv, void *handle);
+int pvdisplay_short(const struct cmd_context *cmd,
+		    const struct volume_group *vg,
+		    const struct physical_volume *pv, void *handle);
 
-void lvdisplay_colons(struct logical_volume *lv);
-int lvdisplay_segments(struct logical_volume *lv);
-int lvdisplay_full(struct cmd_context *cmd, struct logical_volume *lv,
+void lvdisplay_colons(const struct logical_volume *lv);
+int lvdisplay_segments(const struct logical_volume *lv);
+int lvdisplay_full(struct cmd_context *cmd, const struct logical_volume *lv,
 		   void *handle);
 
-void vgdisplay_extents(struct volume_group *vg);
-void vgdisplay_full(struct volume_group *vg);
-void vgdisplay_colons(struct volume_group *vg);
-void vgdisplay_short(struct volume_group *vg);
+void vgdisplay_extents(const struct volume_group *vg);
+void vgdisplay_full(const struct volume_group *vg);
+void vgdisplay_colons(const struct volume_group *vg);
+void vgdisplay_short(const struct volume_group *vg);
 
-void display_formats(struct cmd_context *cmd);
-void display_segtypes(struct cmd_context *cmd);
+void display_formats(const struct cmd_context *cmd);
+void display_segtypes(const struct cmd_context *cmd);
 
 /*
  * Allocation policy display conversion routines.
lib/metadata/metadata-exported.h +1 -1
index 5cc7ead..2e7160e 100644
@@ -397,7 +397,7 @@ int vg_add_snapshot(struct format_instance *fid, const char *name,
 
 int vg_remove_snapshot(struct logical_volume *cow);
 
-int vg_check_status(struct volume_group *vg, uint32_t status);
+int vg_check_status(const struct volume_group *vg, uint32_t status);
 
 /*
 * Mirroring functions
lib/metadata/metadata.c +1 -1
index de42ed3..1a4cc0f 100644
@@ -1763,7 +1763,7 @@ int pv_analyze(struct cmd_context *cmd, const char *pv_name,
  * 0 - fail
  * 1 - success
  */
-int vg_check_status(struct volume_group *vg, uint32_t status)
+int vg_check_status(const struct volume_group *vg, uint32_t status)
 {
 	if ((status & CLUSTERED) &&
 	    (vg->status & CLUSTERED) && !locking_is_clustered() &&
tools/toollib.c +8 -11
index 5510e8e..5753aff 100644
@@ -25,7 +25,7 @@
 #define MIRROR_DISK_VERSION 2
 
 /* Command line args */
-unsigned arg_count(struct cmd_context *cmd, int a)
+unsigned arg_count(const struct cmd_context *cmd, int a)
 {
 	return cmd->args[a].count;
 }
@@ -142,12 +142,12 @@ char *skip_dev_dir(struct cmd_context *cmd, const char *vg_name,
 /*
  * Metadata iteration functions
  */
-int process_each_lv_in_vg(struct cmd_context *cmd, struct volume_group *vg,
-			  struct list *arg_lvnames, struct list *tags,
+int process_each_lv_in_vg(struct cmd_context *cmd,
+			  const struct volume_group *vg,
+			  const struct list *arg_lvnames,
+			  const struct list *tags,
 			  void *handle,
-			  int (*process_single) (struct cmd_context * cmd,
-						 struct logical_volume * lv,
-						 void *handle))
+			  process_single_lv_fn_t process_single)
 {
 	int ret_max = 0;
 	int ret = 0;
@@ -603,11 +603,8 @@ int process_each_vg(struct cmd_context *cmd, int argc, char **argv,
 }
 
 int process_each_pv_in_vg(struct cmd_context *cmd, struct volume_group *vg,
-			  struct list *tags, void *handle,
-			  int (*process_single) (struct cmd_context * cmd,
-						 struct volume_group * vg,
-						 struct physical_volume * pv,
-						 void *handle))
+			  const struct list *tags, void *handle,
+			  process_single_pv_fn_t process_single)
 {
 	int ret_max = 0;
 	int ret = 0;
tools/toollib.h +17 -11
index b67fc40..93ab5b3 100644
@@ -60,19 +60,25 @@ int process_each_segment_in_lv(struct cmd_context *cmd,
 						      struct lv_segment * seg,
 						      void *handle));
 
+typedef int (*process_single_pv_fn_t) (struct cmd_context *cmd,
+				  struct volume_group *vg,
+				  struct physical_volume *pv,
+				  void *handle);
+
 int process_each_pv_in_vg(struct cmd_context *cmd, struct volume_group *vg,
-			  struct list *tags, void *handle,
-			  int (*process_single) (struct cmd_context * cmd,
-						 struct volume_group * vg,
-						 struct physical_volume * pv,
-						 void *handle));
-
-int process_each_lv_in_vg(struct cmd_context *cmd, struct volume_group *vg,
-			  struct list *arg_lvnames, struct list *tags,
+			  const struct list *tags, void *handle,
+			  process_single_pv_fn_t process_single);
+
+typedef int (*process_single_lv_fn_t) (struct cmd_context *cmd,
+				  struct logical_volume *lv,
+				  void *handle);
+
+int process_each_lv_in_vg(struct cmd_context *cmd,
+			  const struct volume_group *vg,
+			  const struct list *arg_lvnames,
+			  const struct list *tags,
 			  void *handle,
-			  int (*process_single) (struct cmd_context * cmd,
-						 struct logical_volume * lv,
-						 void *handle));
+			  process_single_lv_fn_t process_single);
 
 char *default_vgname(struct cmd_context *cmd);
 const char *extract_vgname(struct cmd_context *cmd, const char *lv_name);
tools/tools.h +1 -1
index bba7e9e..eac7c02 100644
@@ -147,7 +147,7 @@ int alloc_arg(struct cmd_context *cmd, struct arg *a);
 char yes_no_prompt(const char *prompt, ...);
 
 /* we use the enums to access the switches */
-unsigned int arg_count(struct cmd_context *cmd, int a);
+unsigned int arg_count(const struct cmd_context *cmd, int a);
 const char *arg_value(struct cmd_context *cmd, int a);
 const char *arg_str_value(struct cmd_context *cmd, int a, const char *def);
 int32_t arg_int_value(struct cmd_context *cmd, int a, const int32_t def); 
tools/vgdisplay.c +4 -3
index aa02129..0881906 100644
@@ -46,10 +46,11 @@ static int vgdisplay_single(struct cmd_context *cmd, const char *vg_name,
 		vgdisplay_extents(vg);
 
 		process_each_lv_in_vg(cmd, vg, NULL, NULL, NULL,
-				      &lvdisplay_full);
+				      (process_single_lv_fn_t)lvdisplay_full);
 
 		log_print("--- Physical volumes ---");
-		process_each_pv_in_vg(cmd, vg, NULL, NULL, &pvdisplay_short);
+		process_each_pv_in_vg(cmd, vg, NULL, NULL,
+				      (process_single_pv_fn_t)pvdisplay_short);
 	}
 
 	check_current_backup(vg);
@@ -98,7 +99,7 @@ int vgdisplay(struct cmd_context *cmd, int argc, char **argv)
 **********/
 
 	process_each_vg(cmd, argc, argv, LCK_VG_READ, 0, NULL,
-			&vgdisplay_single);
+			vgdisplay_single);
 
 /******** FIXME Need to count number processed 
 	  Add this to process_each_vg if arg_count(cmd,activevolumegroups_ARG) ? 
#1 lore raw
Quoted 1 line · Jim Meyering

I find it easier to read code that uses "const" whenever possible.

All looks sane, thanks.

tools/vgdisplay.c static int vgdisplay_single(struct cmd_context *cmd, const char *vg_name, · line 49

Quoted 3 lines
tools/vgdisplay.c +1 -1
 		process_each_lv_in_vg(cmd, vg, NULL, NULL, NULL,
-				      &lvdisplay_full);
+				      (process_single_lv_fn_t)lvdisplay_full);

Is there a nice way to avoid those casts?

Alasdair

Signature · 1 line
agk at redhat.com
#2 lore raw
Quoted 10 lines · Alasdair G Kergon
Quoted 1 line · Jim Meyering

I find it easier to read code that uses "const" whenever possible.

All looks sane, thanks.

tools/vgdisplay.c static int vgdisplay_single(struct cmd_context *cmd, const char *vg_name, · line 49

Quoted 3 lines
tools/vgdisplay.c +1 -1
 		process_each_lv_in_vg(cmd, vg, NULL, NULL, NULL,
-				      &lvdisplay_full);
+				      (process_single_lv_fn_t)lvdisplay_full);

Is there a nice way to avoid those casts?

This is where you wish for features of other languages.

I wrote an earlier iteration that instead added a new function, process_each_lv_in_vg_ro (read-only), with the appropriate signature for the fn-parameter, but IMHO, that was worse, and besides, wasn't worth it, for just one use.

#3 lore raw
Quoted 6 lines
Quoted 5 lines

tools/vgdisplay.c static int vgdisplay_single(struct cmd_context *cmd, const char *vg_name, · line 49

Quoted 3 lines
tools/vgdisplay.c +1 -1
 		process_each_lv_in_vg(cmd, vg, NULL, NULL, NULL,
-				      &lvdisplay_full);
+				      (process_single_lv_fn_t)lvdisplay_full);

Is there a nice way to avoid those casts?

Quoted 6 lines

This is where you wish for features of other languages.

I wrote an earlier iteration that instead added a new function, process_each_lv_in_vg_ro (read-only), with the appropriate signature for the fn-parameter, but IMHO, that was worse, and besides, wasn't worth it, for just one use.

You could still add a new function that just confines the ugly cast? (Assuming that's not what you have done). Maybe _const instead of _ro, too.

Yours, Peter.

Signature · 6 lines
Peter Rockai | me()mornfall!net | prockai()redhat!com
 http://blog.mornfall.net | http://web.mornfall.net

"In My Egotistical Opinion, most people's C programs should be
 indented six feet downward and covered with dirt."
     -- Blair P. Houghton on the subject of C program indentation
#4 lore raw
Quoted 8 lines · Petr Rockai
Quoted 8 lines
Quoted 5 lines

tools/vgdisplay.c static int vgdisplay_single(struct cmd_context *cmd, const char *vg_name, · line 49

Quoted 3 lines
tools/vgdisplay.c +1 -1
 		process_each_lv_in_vg(cmd, vg, NULL, NULL, NULL,
-				      &lvdisplay_full);
+				      (process_single_lv_fn_t)lvdisplay_full);

Is there a nice way to avoid those casts?

This is where you wish for features of other languages.

Quoted 8 lines
Quoted 4 lines

I wrote an earlier iteration that instead added a new function, process_each_lv_in_vg_ro (read-only), with the appropriate signature for the fn-parameter, but IMHO, that was worse, and besides, wasn't worth it, for just one use.

You could still add a new function that just confines the ugly cast? (Assuming that's not what you have done). Maybe _const instead of _ro, too.

That's what I did.
But *it* then has to call the original, process_each_lv_in_vg_ro, and in so doing, it needs its own casts, in addition to the added layer of indirection.

Adding one little cast (per call) is not pretty, but less ugly than the alternative, since there's only one use of each.

#5 lore raw
In reply to Alasdair G Kergon
Quoted 4 lines · Alasdair G Kergon
Quoted 1 line · Jim Meyering

I find it easier to read code that uses "const" whenever possible.

All looks sane, thanks.

Thanks for the review.
I committed that, along with const-adding changes to btree.[ch].

The sole cast I added there (in _lookup) is unavoidable, given a const-correct interface. It's analogous to the strstr case, where the signature forces you to return char*, yet the pointer you actually return is offset from the "const char *" parameter.