shithub: mpl

Download patch

ref: a313478754f5ee50a5cc7545bd3a8dc94d3adeb6
parent: 4d66bfdaa80c6b21509f4150d80c3146dfcea609
author: Jacob Moody <jsmoody@iastate.edu>
date: Thu Dec 5 12:33:13 EST 2019

tidy of hashmap

--- a/dat.c
+++ b/dat.c
@@ -95,7 +95,7 @@
 	return 0;
 }
 
-void
+int
 mapdump(Hmap *h, void **buf, int size)
 {
 	Hnode *n;
@@ -103,19 +103,40 @@
 
 	rlock(h);
 	for(i=c=0;i<h->size;i++)
-		for(n=h->nodes+i;n!=nil && n->key!=nil;n=n->next)
+		for(n=h->nodes+i;n!=nil && n->key!=nil;n=n->next){
+			if(c >= size)
+				return c;
 			buf[c++] = n->val;
+		}
 	runlock(h);
+	return c;
 }
 
+int
+mapdumpkey(Hmap *h, char **buf, int size)
+{
+	Hnode *n;
+	int i, c;
+
+	rlock(h);
+	for(i=c=0;i<h->size;i++)
+		for(n=h->nodes+i;n!=nil && n->key!=nil;n=n->next){
+			if(c >= size)
+				return c;
+			buf[c++] = n->key;
+		}
+	runlock(h);
+	return c;
+}
+
 void
 mapclear(Hmap *h)
 {
 	Hnode *n;
-	int i, c;
+	int i;
 
 	wlock(h);
-	for(i=c=0;i<h->size;i++)
+	for(i=0;i<h->size;i++)
 		for(n=h->nodes+i;n!=nil;n=n->next)
 			if(n->key != nil){
 				free(n->key);
@@ -122,4 +143,12 @@
 				n->key=nil;
 			}
 	wunlock(h);
+}
+
+void
+freemap(Hmap *h)
+{
+	mapclear(h);
+	free(h->nodes);
+	free(h);
 }
\ No newline at end of file