From b2852c06894002890aa49322ded6c99022cc6d17 Mon Sep 17 00:00:00 2001
From: Dmitry Kozlov <xeb@mail.ru>
Date: Wed, 22 Dec 2010 23:06:17 +0300
Subject: minor changes

---
 accel-pptpd/CMakeLists.txt    |  4 ++++
 accel-pptpd/triton/mempool.c  | 43 +++++++++++++++++++++++++++++++++++++------
 accel-pptpd/triton/triton.c   |  8 +++++++-
 accel-pptpd/triton/triton_p.h | 14 +++++++-------
 4 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/accel-pptpd/CMakeLists.txt b/accel-pptpd/CMakeLists.txt
index c7ff3c52..3e83d590 100644
--- a/accel-pptpd/CMakeLists.txt
+++ b/accel-pptpd/CMakeLists.txt
@@ -39,6 +39,10 @@ IF (RADIUS)
 	ADD_SUBDIRECTORY(radius)
 ENDIF (RADIUS)
 
+IF (VALGRIND)
+	ADD_DEFINITIONS(-DVALGRIND)
+ENDIF (VALGRIND)
+
 ADD_SUBDIRECTORY(triton)
 ADD_SUBDIRECTORY(ctrl)
 ADD_SUBDIRECTORY(auth)
diff --git a/accel-pptpd/triton/mempool.c b/accel-pptpd/triton/mempool.c
index fb9db089..a2448760 100644
--- a/accel-pptpd/triton/mempool.c
+++ b/accel-pptpd/triton/mempool.c
@@ -9,6 +9,12 @@
 
 #include "memdebug.h"
 
+#ifdef VALGRIND
+#include <valgrind/memcheck.h>
+#endif
+
+#define MMAP_PAGE_SIZE 16
+
 //#define MEMPOOL_DISABLE
 
 #define MAGIC1 0x2233445566778899llu
@@ -28,8 +34,8 @@ struct _mempool_t
 
 struct _item_t
 {
-	struct _mempool_t *owner;
 	struct list_head entry;
+	struct _mempool_t *owner;
 #ifdef MEMDEBUG
 	const char *fname;
 	int line;
@@ -117,10 +123,15 @@ void __export *mempool_alloc_md(mempool_t *pool, const char *fname, int line)
 	struct _mempool_t *p = (struct _mempool_t *)pool;
 	struct _item_t *it;
 	uint32_t size = sizeof(*it) + p->size + 8;
+	int i;
 
 	spin_lock(&p->lock);
 	if (!list_empty(&p->items)) {
 		it = list_entry(p->items.next, typeof(*it), entry);
+#ifdef VALGRIND
+		VALGRIND_MAKE_MEM_DEFINED(&it->owner, size - sizeof(it->entry));
+		VALGRIND_MAKE_MEM_UNDEFINED(it->ptr, p->size);
+#endif
 		list_del(&it->entry);
 		list_add(&it->entry, &p->ditems);
 		spin_unlock(&p->lock);
@@ -136,9 +147,25 @@ void __export *mempool_alloc_md(mempool_t *pool, const char *fname, int line)
 	}
 	spin_unlock(&p->lock);
 
-	if (p->mmap)
-		it = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_32BIT, -1, 0);
-	else
+	if (p->mmap) {
+		it = mmap(NULL, size * MMAP_PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_32BIT, -1, 0);
+		spin_lock(&p->lock);
+		for (i = 0; i < MMAP_PAGE_SIZE - 1; i++, it) {
+			it->owner = p;
+			it->magic2 = p->magic;
+			it->magic1 = MAGIC1;
+			*(uint64_t*)(it->ptr + p->size) = it->magic2;
+			list_add_tail(&it->entry,&p->items);
+#ifdef VALGRIND
+			VALGRIND_MAKE_MEM_NOACCESS(&it->owner, size - sizeof(it->entry));
+#endif
+			it = (struct _item_t *)((char *)it + size);
+		}
+		spin_unlock(&p->lock);
+#ifdef VALGRIND
+		VALGRIND_MAKE_MEM_UNDEFINED(it, size);
+#endif
+	} else
 		it = md_malloc(size, fname, line);
 
 	if (!it) {
@@ -165,6 +192,7 @@ void __export *mempool_alloc_md(mempool_t *pool, const char *fname, int line)
 void __export mempool_free(void *ptr)
 {
 	struct _item_t *it = container_of(ptr, typeof(*it), ptr);
+	struct _mempool_t *p = it->owner;
 	uint32_t size = sizeof(*it) + it->owner->size + 8;
 
 #ifdef MEMDEBUG
@@ -186,14 +214,17 @@ void __export mempool_free(void *ptr)
 	it->magic1 = 0;
 #endif
 
-	spin_lock(&it->owner->lock);
+	spin_lock(&p->lock);
 #ifdef MEMDEBUG
 	list_del(&it->entry);
 #endif
 #ifndef MEMPOOL_DISABLE
 	list_add_tail(&it->entry,&it->owner->items);
 #endif
-	spin_unlock(&it->owner->lock);
+#ifdef VALGRIND
+	VALGRIND_MAKE_MEM_NOACCESS(&it->owner, size - sizeof(it->entry));
+#endif
+	spin_unlock(&p->lock);
 
 #ifdef MEMPOOL_DISABLE
 	if (it->owner->mmap)
diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c
index 15e38882..01c61284 100644
--- a/accel-pptpd/triton/triton.c
+++ b/accel-pptpd/triton/triton.c
@@ -8,6 +8,8 @@
 #include "triton_p.h"
 #include "memdebug.h"
 
+#include <valgrind/drd.h>
+
 int thread_count = 2;
 int max_events = 64;
 
@@ -77,8 +79,12 @@ static void* triton_thread(struct _triton_thread_t *thread)
 			//printf("thread %p: exit sigwait\n", thread);
 			__sync_add_and_fetch(&triton_stat.thread_active, 1);
 
-			if (!thread->ctx)
+			spin_lock(&threads_lock);
+			if (!thread->ctx) {
+				spin_unlock(&threads_lock);
 				continue;
+			}
+			spin_unlock(&threads_lock);
 		}
 
 cont:
diff --git a/accel-pptpd/triton/triton_p.h b/accel-pptpd/triton/triton_p.h
index f6fb6789..77702144 100644
--- a/accel-pptpd/triton/triton_p.h
+++ b/accel-pptpd/triton/triton_p.h
@@ -38,13 +38,13 @@ struct _triton_context_t
 
 	ucontext_t uctx;
 
-	int queued:1;
-	int sleeping:1;
-	int wakeup:1;
-	int need_close:1;
-	int need_free:1;
-	int pending:1;
-	int priority:1;
+	int queued;
+	int sleeping;
+	int wakeup;
+	int need_close;
+	int need_free;
+	int pending;
+	int priority;
 
 	struct triton_context_t *ud;
 	void *bf_arg;
-- 
cgit v1.2.3