Index: src/lib/sort.c
===================================================================
--- src/lib/sort.c	(revision 0a50f5941e7572881e764db343f5a363d93b313f)
+++ src/lib/sort.c	(revision a5556b4c962a9b456fc78bea47df3b031c8b8da2)
@@ -34,4 +34,18 @@
 #define EBUFSIZE	32
 
+void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot);
+void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot);
+
+/** Quicksort wrapper
+ *
+ * This is only a wrapper that takes care of memory allocations for storing
+ * the pivot and temporary elements for generic quicksort algorithm.
+ * 
+ * @param data Pointer to data to be sorted.
+ * @param n Number of elements to be sorted.
+ * @param e_size Size of one element.
+ * @param cmp Comparator function.
+ * 
+ */
 void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b))
 {
@@ -50,4 +64,26 @@
 	}
 
+	_qsort(data, n, e_size, cmp, tmp, pivot);
+	
+	if (e_size > EBUFSIZE) {
+		free(tmp);
+		free(pivot);
+	}
+}
+
+/** Quicksort
+ *
+ * Apply generic quicksort algorithm on supplied data, using pre-allocated buffers.
+ * 
+ * @param data Pointer to data to be sorted.
+ * @param n Number of elements to be sorted.
+ * @param e_size Size of one element.
+ * @param cmp Comparator function.
+ * @param tmp Pointer to scratch memory buffer e_size bytes long.
+ * @param pivot Pointer to scratch memory buffer e_size bytes long.
+ * 
+ */
+void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot)
+{
 	if (n > 4) {
 		int i = 0, j = n - 1;
@@ -67,23 +103,25 @@
 		}
 
-		qsort(data, j + 1, e_size, cmp);
-		qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp);
+		_qsort(data, j + 1, e_size, cmp, tmp, pivot);
+		_qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp, tmp, pivot);
 	} else {
-		bubblesort(data, n, e_size, cmp);
-	}
-
-	
-	if (e_size > EBUFSIZE) {
-		free(tmp);
-		free(pivot);
+		_bubblesort(data, n, e_size, cmp, tmp);
 	}
 }
 
-
+/** Bubblesort wrapper
+ *
+ * This is only a wrapper that takes care of memory allocation for storing
+ * the slot element for generic bubblesort algorithm.
+ * 
+ * @param data Pointer to data to be sorted.
+ * @param n Number of elements to be sorted.
+ * @param e_size Size of one element.
+ * @param cmp Comparator function.
+ * 
+ */
 void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b))
 {
 	__u8 buf_slot[EBUFSIZE];
-	bool done = false;
-	void * p;
 	void * slot = buf_slot;
 	
@@ -95,4 +133,27 @@
 		}
 	}
+
+	_bubblesort(data, n, e_size, cmp, slot);
+	
+	if (e_size > EBUFSIZE) {
+		free(slot);
+	}
+}
+
+/** Bubblesort
+ *
+ * Apply generic bubblesort algorithm on supplied data, using pre-allocated buffer.
+ * 
+ * @param data Pointer to data to be sorted.
+ * @param n Number of elements to be sorted.
+ * @param e_size Size of one element.
+ * @param cmp Comparator function.
+ * @param slot Pointer to scratch memory buffer e_size bytes long.
+ * 
+ */
+void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot)
+{
+	bool done = false;
+	void * p;
 
 	while (!done) {
@@ -107,8 +168,5 @@
 		}
 	}
-	
-	if (e_size > EBUFSIZE) {	
-		free(slot);
-	}
+
 }
 
