git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/6] attr: remove the union in struct match_attr

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Oct 4, 2012, 07:39 UTC
Message-ID
<1349336392-1772-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1349336392-1772-1-git-send-email-pclouds@gmail.com>

We're going to add more attributes to u.pattern so it'll become bigger in size than a pointer. There's no point in sharing the same room with u.attr.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 attr.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
diff --git a/attr.c b/attr.c
index 15ebaa1..48df800 100644
--- a/attr.c
+++ b/attr.c
@@ -119,10 +119,10 @@ struct attr_state {
 /*
  * One rule, as from a .gitattributes file.
  *
- * If is_macro is true, then u.attr is a pointer to the git_attr being
+ * If is_macro is true, then attr is a pointer to the git_attr being
  * defined.
  *
- * If is_macro is false, then u.pattern points at the filename pattern
+ * If is_macro is false, then pattern points at the filename pattern
  * to which the rule applies.  (The memory pointed to is part of the
  * memory block allocated for the match_attr instance.)
  *
@@ -131,10 +131,8 @@ struct attr_state {
  * listed as they appear in the file (macros unexpanded).
  */
 struct match_attr {
-	union {
-		char *pattern;
-		struct git_attr *attr;
-	} u;
+	const char *pattern;
+	struct git_attr *attr;
 	char is_macro;
 	unsigned num_attr;
 	struct attr_state state[FLEX_ARRAY];
@@ -240,11 +238,12 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 		      sizeof(struct attr_state) * num_attr +
 		      (is_macro ? 0 : namelen + 1));
 	if (is_macro)
-		res->u.attr = git_attr_internal(name, namelen);
+		res->attr = git_attr_internal(name, namelen);
 	else {
-		res->u.pattern = (char *)&(res->state[num_attr]);
-		memcpy(res->u.pattern, name, namelen);
-		res->u.pattern[namelen] = 0;
+		char *p = (char *)&(res->state[num_attr]);
+		memcpy(p, name, namelen);
+		p[namelen] = 0;
+		res->pattern = p;
 	}
 	res->is_macro = is_macro;
 	res->num_attr = num_attr;
@@ -682,7 +681,7 @@ static int fill_one(const char *what, struct match_attr *a, int rem)
 
 		if (*n == ATTR__UNKNOWN) {
 			debug_set(what,
-				  a->is_macro ? a->u.attr->name : a->u.pattern,
+				  a->is_macro ? a->attr->name : a->pattern,
 				  attr, v);
 			*n = v;
 			rem--;
@@ -702,7 +701,7 @@ static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
 		if (a->is_macro)
 			continue;
 		if (path_matches(path, pathlen,
-				 a->u.pattern, base, strlen(base)))
+				 a->pattern, base, strlen(base)))
 			rem = fill_one("fill", a, rem);
 	}
 	return rem;
@@ -722,7 +721,7 @@ static int macroexpand_one(int attr_nr, int rem)
 			struct match_attr *ma = stk->attrs[i];
 			if (!ma->is_macro)
 				continue;
-			if (ma->u.attr->attr_nr == attr_nr)
+			if (ma->attr->attr_nr == attr_nr)
 				a = ma;
 		}
 
-- 
1.7.12.1.405.gb727dc9
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 7 of 28 in “What's cooking in git.git (Oct 2012, #01; Tue, 2)”
  1. Junio C HamanoOct 2, 2012
  2. Nguyen Thai Ngoc DuyOct 3, 2012
  3. Junio C HamanoOct 3, 2012
  4. Nguyen Thai Ngoc DuyOct 4, 2012
  5. Junio C HamanoOct 4, 2012
  6. 0/6 wildmatch part 2Nguyễn Thái Ngọc Duy, Oct 4, 2012
  7. 1/6 attr: remove the union in struct match_attrNguyễn Thái Ngọc Duy, Oct 4, 2012
  8. 2/6 attr: avoid strlen() on every matchNguyễn Thái Ngọc Duy, Oct 4, 2012
  9. 3/6 attr: avoid searching for basename on every matchNguyễn Thái Ngọc Duy, Oct 4, 2012
  10. 4/6 attr: more matching optimizations from .gitignoreNguyễn Thái Ngọc Duy, Oct 4, 2012
  11. 5/6 gitignore: do not do basename match with patterns that have '**'Nguyễn Thái Ngọc Duy, Oct 4, 2012
  12. Junio C HamanoOct 4, 2012
  13. Johannes SixtOct 5, 2012
  14. Nguyen Thai Ngoc DuyOct 5, 2012
  15. 6/6 t3001: note about expected "**" behaviorNguyễn Thái Ngọc Duy, Oct 4, 2012
  16. Junio C HamanoOct 4, 2012
  17. Junio C HamanoOct 4, 2012
  18. Michael HaggertyOct 4, 2012
  19. Nguyen Thai Ngoc DuyOct 4, 2012
  20. Michael HaggertyOct 4, 2012
  21. Junio C HamanoOct 4, 2012
  22. Andreas SchwabOct 5, 2012
  23. Matthieu MoyOct 5, 2012
  24. Andreas SchwabOct 5, 2012
  25. Nguyen Thai Ngoc DuyOct 5, 2012
  26. David Michael BarrOct 4, 2012
  27. Junio C HamanoOct 4, 2012
  28. Florian AchleitnerOct 30, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.