[Tux3] [PATCH 02/10] don't use preallocation if BUFFER_PARANOIA_DEBUG is defined

OGAWA Hirofumi hirofumi at mail.parknet.co.jp
Fri Oct 17 02:43:38 PDT 2008


If buffer was allocated linearly, hard to detect invalid buffer access.
So, don't use preallocation if BUFFER_PARANOIA_DEBUG is defined.

---

 user/test/buffer.c |   31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff -puN user/test/buffer.c~buffer-debug user/test/buffer.c
--- tux3/user/test/buffer.c~buffer-debug	2008-10-16 01:20:41.000000000 +0900
+++ tux3-hirofumi/user/test/buffer.c	2008-10-16 01:20:41.000000000 +0900
@@ -6,6 +6,8 @@
 #include "buffer.h"
 #include "trace.h"
 
+#define BUFFER_PARANOIA_DEBUG
+
 #define buftrace trace_off
 
 /*
@@ -283,7 +285,7 @@ alloc_buffer:
 	if (!buffer)
 		return NULL;
 	*buffer = (struct buffer){ .state = BUFFER_STATE_EMPTY };
-	if ((err = posix_memalign((void **)&(buffer->data), SECTOR_SIZE, bufsize(buffer)))) {
+	if ((err = posix_memalign((void **)&(buffer->data), SECTOR_SIZE, 1 << map->dev->bits))) {
 		warn("Error: %s unable to expand buffer pool", strerror(err));
 		free(buffer);
 		return NULL;
@@ -399,6 +401,28 @@ int flush_buffers(struct map *map) // !!
 	return err;
 }
 
+#ifdef BUFFER_PARANOIA_DEBUG
+static void __destroy_buffers(void)
+{
+#define buffer_entry(x)		list_entry(x, struct buffer, lrulink)
+	struct list_head *heads[] = { &free_buffers, &lru_buffers, NULL, };
+	struct list_head **head = heads;
+	while (*head) {
+		while (!list_empty(*head)) {
+			struct buffer *buffer = buffer_entry((*head)->next);
+			list_del(&buffer->lrulink);
+			free(buffer->data);
+			free(buffer);
+		}
+		head++;
+	}
+}
+static void destroy_buffers(void)
+{
+	atexit(__destroy_buffers);
+}
+#endif
+
 int preallocate_buffers(unsigned bufsize) {
 	struct buffer *buffers = (struct buffer *)malloc(max_buffers*sizeof(struct buffer));
 	unsigned char *data_pool = NULL;
@@ -432,11 +456,14 @@ void init_buffers(struct dev *dev, unsig
 	INIT_LIST_HEAD(&lru_buffers);
 	INIT_LIST_HEAD(&free_buffers);
 	INIT_LIST_HEAD(&journaled_buffers);
-
+#ifdef BUFFER_PARANOIA_DEBUG
+	destroy_buffers();
+#else
 	unsigned bufsize = 1 << dev->bits;
 	max_buffers = poolsize / bufsize;
 	max_evict = max_buffers / 10;
 	preallocate_buffers(bufsize);
+#endif
 }
 
 int dev_bread(struct buffer *buffer)
_

_______________________________________________
Tux3 mailing list
Tux3 at tux3.org
http://tux3.org/cgi-bin/mailman/listinfo/tux3



More information about the Tux3 mailing list