svn commit: r201573 - user/luigi/ipfw3-head/sys/netinet/ipfw

Luigi Rizzo luigi at FreeBSD.org
Tue Jan 5 12:29:03 UTC 2010


Author: luigi
Date: Tue Jan  5 12:29:03 2010
New Revision: 201573
URL: http://svn.freebsd.org/changeset/base/201573

Log:
  add testing code (to be run in userland)

Modified:
  user/luigi/ipfw3-head/sys/netinet/ipfw/dn_heap.c

Modified: user/luigi/ipfw3-head/sys/netinet/ipfw/dn_heap.c
==============================================================================
--- user/luigi/ipfw3-head/sys/netinet/ipfw/dn_heap.c	Tue Jan  5 12:03:30 2010	(r201572)
+++ user/luigi/ipfw3-head/sys/netinet/ipfw/dn_heap.c	Tue Jan  5 12:29:03 2010	(r201573)
@@ -32,10 +32,27 @@ __FBSDID("$FreeBSD$");
  */
 
 #include <sys/param.h>
+#ifdef _KERNEL
 #include <sys/systm.h>
 #include <sys/malloc.h>
 #include <sys/kernel.h>
 #include <netinet/ipfw/dn_heap.h>
+#define log(x, arg...)
+
+#else /* !_KERNEL */
+
+#include <stdio.h>
+#include <strings.h>
+#include <stdlib.h>
+#include  "dn_heap.h"
+#define log(x, arg...)	fprintf(stderr, ## arg)
+#define panic(x...)	fprintf(stderr, ## x), exit(1)
+#define MALLOC_DEFINE(a, b, c)
+static void *my_malloc(int s) {	return malloc(s); }
+static void my_free(void *p) {	free(p); }
+#define malloc(s, t, w)	my_malloc(s)
+#define free(p, t)	my_free(p)
+#endif /* !_KERNEL */
 
 MALLOC_DEFINE(M_DN_HEAP, "dummynet", "dummynet heap");
 
@@ -108,6 +125,7 @@ heap_insert(struct dn_heap *h, uint64_t 
 {
 	int son = h->elements;
 
+	//log("%s key %llu p %p\n", __FUNCTION__, key1, p);
 	if (p == NULL)	/* data already there, set starting point */
 		son = key1;
 	else {/* insert new element at the end, possibly resize */
@@ -249,3 +267,32 @@ heap_free(struct dn_heap *h)
 		free(h->p, M_DN_HEAP);
 	bzero(h, sizeof(*h) );
 }
+
+#ifndef _KERNEL
+/*
+ * testing code for the heap
+ */
+int
+main(int argc, char *argv[])
+{
+	struct dn_heap h;
+	int i, n;
+	struct timeval tv;
+	uint64_t k;
+
+	n = (argc > 1) ? atoi(argv[1]) : 0;
+	if (n <= 0 || n > 10000)
+		n = 100;
+	bzero(&h, sizeof(h));
+	gettimeofday(&tv, NULL);
+	srandom(tv.tv_usec);
+	heap_init(&h, 0);
+	for (i=0; i < n; i++)
+		heap_insert(&h, random(), (void *)(100+i));
+	for (i=0; h.elements > 0; i++) {
+		printf("%d key %llu, val %p\n",
+			i, h.p[0].key, h.p[0].object);
+		heap_extract(&h, NULL);
+	}
+}
+#endif


More information about the svn-src-user mailing list