All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH 2/2] decorate: add "clear_decoration()"
Date: Sun, 07 Apr 2013 23:14:17 -0700	[thread overview]
Message-ID: <7v1ual35xi.fsf@alter.siamese.dyndns.org> (raw)

So far, all the users of the decoration API used decoration that
only grows and discarded at the end of the program execution.

Introduce for_each_decoration() that lets the caller iterate over
all defined decorations and use it to implement clear_decoration()
function.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/technical/api-decorate.txt | 22 ++++++++++++++++++++++
 decorate.c                               | 19 +++++++++++++++++++
 decorate.h                               |  4 ++++
 3 files changed, 45 insertions(+)

diff --git a/Documentation/technical/api-decorate.txt b/Documentation/technical/api-decorate.txt
index 0cc2b64..600366d 100644
--- a/Documentation/technical/api-decorate.txt
+++ b/Documentation/technical/api-decorate.txt
@@ -35,3 +35,25 @@ the `obj`.  You cannot tell if `obj` does not appear in the hashtable
 at all, or if `obj` has decoration whose value is NULL, so if you want
 to use the decoration API for "Did I see this object?" hashtable,
 use decoration value that is _not_ NULL.
+
+Iterating
+---------
+
+`for_each_decoration(struct decoration *deco, for_each_decoration_fn fn)`
+iterates over all the entries in the hashtable, and calls `fn` on each
+entry.  The `fn` callback function takes a single `struct object_decoration`
+as its parameter, that has `base` field that points at the `obj`
+given to an earlier call to `add_decoration` and `decoration` field
+that remembers the `decoration`.
+
+Clearing
+--------
+
+`clear_decoration(struct decoration *deco, for_each_decoration_fn fn)`,
+when `fn` is not NULL, iterates over all the entries and calls the
+callback function `fn` using `for_each_decoration`, and then frees
+the memory used for the hashtable but not the `struct decoration` itself.
+
+The callback function can be used to release the resource used by
+the `decoration` the earlier `add_decoration` registered to the
+hashtable.
diff --git a/decorate.c b/decorate.c
index 2f8a63e..3e15358 100644
--- a/decorate.c
+++ b/decorate.c
@@ -86,3 +86,22 @@ void *lookup_decoration(struct decoration *n, const struct object *obj)
 			j = 0;
 	}
 }
+
+void for_each_decoration(struct decoration *n, for_each_decoration_fn fn)
+{
+	int i;
+
+	for (i = 0; i < n->size; i++) {
+		struct object_decoration *entry = &n->hash[i];
+		if (!entry->base)
+			continue;
+		fn(entry);
+	}
+}
+
+void clear_decoration(struct decoration *n, for_each_decoration_fn fn)
+{
+	if (fn)
+		for_each_decoration(n, fn);
+	free(n->hash);
+}
diff --git a/decorate.h b/decorate.h
index e732804..4a0d37e 100644
--- a/decorate.h
+++ b/decorate.h
@@ -15,4 +15,8 @@ struct decoration {
 extern void *add_decoration(struct decoration *n, const struct object *obj, void *decoration);
 extern void *lookup_decoration(struct decoration *n, const struct object *obj);
 
+typedef void (*for_each_decoration_fn)(struct object_decoration *);
+extern void for_each_decoration(struct decoration *, for_each_decoration_fn);
+extern void clear_decoration(struct decoration *, for_each_decoration_fn);
+
 #endif
-- 
1.8.2.1-450-gd047976

             reply	other threads:[~2013-04-08  6:49 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-08  6:14 Junio C Hamano [this message]
2013-04-08 21:09 ` [PATCH 2/2] decorate: add "clear_decoration()" Jeff King
2013-04-08 21:22   ` Junio C Hamano
2013-04-08 23:01   ` [PATCH 0/3] Using a bit more decoration Junio C Hamano
2013-04-08 23:01     ` [PATCH 1/3] commit: shrink "indegree" field Junio C Hamano
2013-04-09  3:52       ` Jeff King
2013-04-08 23:01     ` [PATCH 2/3] commit: rename parse_commit_date() Junio C Hamano
2013-04-08 23:01     ` [PATCH 3/3] commit: add get_commit_encoding() Junio C Hamano
2013-04-09  3:51     ` [PATCH 0/3] Using a bit more decoration Jeff King
2013-04-09  4:17       ` Junio C Hamano
2013-04-09  4:39         ` Jeff King
2013-04-09  4:54           ` Junio C Hamano
2013-04-09  6:52             ` Jeff King
2013-04-09 20:06               ` Junio C Hamano
2013-04-09  4:37       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7v1ual35xi.fsf@alter.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.