Index: kernel/generic/include/sysinfo/abi.h
===================================================================
--- kernel/generic/include/sysinfo/abi.h	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ kernel/generic/include/sysinfo/abi.h	(revision ba544511759db62eded7601782f553315375b049)
@@ -104,5 +104,4 @@
 	char name[TASK_NAME_BUFLEN];  /**< Task name (in kernel) */
 	size_t virtmem;               /**< Size of VAS (bytes) */
-	size_t resmem;                /**< Size of resident (used) memory (bytes) */
 	size_t threads;               /**< Number of threads */
 	uint64_t ucycles;             /**< Number of CPU cycles in user space */
Index: kernel/generic/include/sysinfo/sysinfo.h
===================================================================
--- kernel/generic/include/sysinfo/sysinfo.h	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ kernel/generic/include/sysinfo/sysinfo.h	(revision ba544511759db62eded7601782f553315375b049)
@@ -148,5 +148,5 @@
 extern sysarg_t sys_sysinfo_get_value(void *, size_t, void *);
 extern sysarg_t sys_sysinfo_get_data_size(void *, size_t, void *);
-extern sysarg_t sys_sysinfo_get_data(void *, size_t, void *, size_t, size_t *);
+extern sysarg_t sys_sysinfo_get_data(void *, size_t, void *, size_t);
 
 #endif
Index: kernel/generic/src/mm/backend_phys.c
===================================================================
--- kernel/generic/src/mm/backend_phys.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ kernel/generic/src/mm/backend_phys.c	(revision ba544511759db62eded7601782f553315375b049)
@@ -81,7 +81,6 @@
 	page_mapping_insert(AS, addr, base + (addr - area->base),
 	    as_area_get_flags(area));
-	
-	if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1))
-		panic("Cannot insert used space.");
+        if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1))
+                panic("Cannot insert used space.");
 
 	return AS_PF_OK;
Index: kernel/generic/src/sysinfo/stats.c
===================================================================
--- kernel/generic/src/sysinfo/stats.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ kernel/generic/src/sysinfo/stats.c	(revision ba544511759db62eded7601782f553315375b049)
@@ -170,5 +170,5 @@
 	 * Note that it may be infinitely better to let the address space
 	 * management code compute these statistics as it proceeds instead of
-	 * having them calculated over and over again here.
+	 * having them calculated here over and over again here.
 	 */
 
@@ -199,64 +199,4 @@
 }
 
-/** Get the resident (used) size of a virtual address space
- *
- * @param as Address space.
- *
- * @return Size of the resident (used) virtual address space (bytes).
- *
- */
-static size_t get_task_resmem(as_t *as)
-{
-	size_t result = 0;
-	
-	/*
-	 * We are holding some spinlocks here and therefore are not allowed to
-	 * block. Only attempt to lock the address space and address space area
-	 * mutexes conditionally. If it is not possible to lock either object,
-	 * allow the statistics to be inexact by skipping the respective object.
-	 *
-	 * Note that it may be infinitely better to let the address space
-	 * management code compute these statistics as it proceeds instead of
-	 * having them calculated over and over again here.
-	 */
-	
-	if (SYNCH_FAILED(mutex_trylock(&as->lock)))
-		return result * PAGE_SIZE;
-	
-	/* Walk the B+ tree of AS areas */
-	link_t *cur;
-	for (cur = as->as_area_btree.leaf_head.next;
-	    cur != &as->as_area_btree.leaf_head; cur = cur->next) {
-		btree_node_t *node =
-		    list_get_instance(cur, btree_node_t, leaf_link);
-		
-		unsigned int i;
-		for (i = 0; i < node->keys; i++) {
-			as_area_t *area = node->value[i];
-			
-			if (SYNCH_FAILED(mutex_trylock(&area->lock)))
-				continue;
-			
-			/* Walk the B+ tree of resident pages */
-			link_t *rcur;
-			for (rcur = area->used_space.leaf_head.next;
-			    rcur != &area->used_space.leaf_head; rcur = rcur->next) {
-				btree_node_t *rnode =
-				    list_get_instance(rcur, btree_node_t, leaf_link);
-				
-				unsigned int j;
-				for (j = 0; j < rnode->keys; j++)
-					result += (size_t) rnode->value[i];
-			}
-			
-			mutex_unlock(&area->lock);
-		}
-	}
-	
-	mutex_unlock(&as->lock);
-	
-	return result * PAGE_SIZE;
-}
-
 /* Produce task statistics
  *
@@ -275,5 +215,4 @@
 	str_cpy(stats_task->name, TASK_NAME_BUFLEN, task->name);
 	stats_task->virtmem = get_task_virtmem(task->as);
-	stats_task->resmem = get_task_resmem(task->as);
 	stats_task->threads = atomic_get(&task->refcount);
 	task_get_accounting(task, &(stats_task->ucycles),
Index: kernel/generic/src/sysinfo/sysinfo.c
===================================================================
--- kernel/generic/src/sysinfo/sysinfo.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ kernel/generic/src/sysinfo/sysinfo.c	(revision ba544511759db62eded7601782f553315375b049)
@@ -40,5 +40,4 @@
 #include <arch/asm.h>
 #include <errno.h>
-#include <macros.h>
 
 /** Maximal sysinfo path length */
@@ -762,13 +761,7 @@
  * character must be null).
  *
- * If the user space buffer size does not equal
- * the actual size of the returned data, the data
- * is truncated. Whether this is actually a fatal
- * error or the data can be still interpreted as valid
- * depends on the nature of the data and has to be
- * decided by the user space.
- *
- * The actual size of data returned is stored to
- * size_ptr.
+ * The user space buffer must be sized exactly according
+ * to the size of the binary data, otherwise the request
+ * fails.
  *
  * @param path_ptr    Sysinfo path in the user address space.
@@ -777,6 +770,4 @@
  *                    to store the binary data.
  * @param buffer_size User space buffer size.
- * @param size_ptr    User space pointer where to store the
- *                    binary data size.
  *
  * @return Error code (EOK in case of no error).
@@ -784,19 +775,19 @@
  */
 sysarg_t sys_sysinfo_get_data(void *path_ptr, size_t path_size,
-    void *buffer_ptr, size_t buffer_size, size_t *size_ptr)
+    void *buffer_ptr, size_t buffer_size)
 {
 	int rc;
 	
 	/* Get the item */
-	sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size,
-	    false);
-	
+	sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, false);
+
 	/* Only constant or generated binary data is considered */
-	if ((ret.tag == SYSINFO_VAL_DATA) ||
-	    (ret.tag == SYSINFO_VAL_FUNCTION_DATA)) {
-		size_t size = min(ret.data.size, buffer_size);
-		rc = copy_to_uspace(buffer_ptr, ret.data.data, size);
-		if (rc == EOK)
-			rc = copy_to_uspace(size_ptr, &size, sizeof(size));
+	if ((ret.tag == SYSINFO_VAL_DATA) || (ret.tag == SYSINFO_VAL_FUNCTION_DATA)) {
+		/* Check destination buffer size */
+		if (ret.data.size == buffer_size)
+			rc = copy_to_uspace(buffer_ptr, ret.data.data,
+			    ret.data.size);
+		else
+			rc = ENOMEM;
 	} else
 		rc = EINVAL;
Index: uspace/app/tasks/tasks.c
===================================================================
--- uspace/app/tasks/tasks.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ uspace/app/tasks/tasks.c	(revision ba544511759db62eded7601782f553315375b049)
@@ -66,5 +66,5 @@
 	}
 	
-	printf(" Task ID  Threads      Mem       uCycles       kCycles   Name\n");
+	printf("      ID  Threads      Mem       uCycles       kCycles   Name\n");
 	
 	size_t i;
@@ -96,5 +96,5 @@
 	}
 	
-	printf(" ThrID    State  CPU   Prio    [k]uCycles    [k]kcycles   Cycle fault\n");
+	printf("    ID    State  CPU   Prio    [k]uCycles    [k]kcycles   Cycle fault\n");
 	size_t i;
 	for (i = 0; i < count; i++) {
Index: uspace/app/top/screen.c
===================================================================
--- uspace/app/top/screen.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ uspace/app/top/screen.c	(revision ba544511759db62eded7601782f553315375b049)
@@ -274,6 +274,6 @@
 {
 	screen_style_inverted();
-	printf("[taskid] [threads] [resident] [%%resi] [virtual] [%%virt]"
-	    " [%%user] [%%kern] [name");
+	printf("[taskid] [threads] [virtual] [%%virt] [%%user]"
+	    " [%%kernel] [name");
 	screen_newline();
 	screen_style_normal();
@@ -295,20 +295,14 @@
 		perc_task_t *perc = data->tasks_perc + data->tasks_map[i];
 		
-		uint64_t resmem;
-		char resmem_suffix;
-		order_suffix(task->resmem, &resmem, &resmem_suffix);
-		
 		uint64_t virtmem;
 		char virtmem_suffix;
 		order_suffix(task->virtmem, &virtmem, &virtmem_suffix);
 		
-		printf("%-8" PRIu64 " %9zu %9" PRIu64 "%c ",
-		    task->task_id, task->threads, resmem, resmem_suffix);
-		print_percent(perc->resmem, 2);
-		printf(" %8" PRIu64 "%c ", virtmem, virtmem_suffix);
+		printf("%-8" PRIu64 " %9zu %8" PRIu64 "%c ", task->task_id,
+		    task->threads, virtmem, virtmem_suffix);
 		print_percent(perc->virtmem, 2);
 		puts(" ");
 		print_percent(perc->ucycles, 2);
-		puts(" ");
+		puts("   ");
 		print_percent(perc->kcycles, 2);
 		puts(" ");
Index: uspace/app/top/top.c
===================================================================
--- uspace/app/top/top.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ uspace/app/top/top.c	(revision ba544511759db62eded7601782f553315375b049)
@@ -195,5 +195,4 @@
 	
 	uint64_t virtmem_total = 0;
-	uint64_t resmem_total = 0;
 	uint64_t ucycles_total = 0;
 	uint64_t kcycles_total = 0;
@@ -224,5 +223,4 @@
 		
 		virtmem_total += new_data->tasks[i].virtmem;
-		resmem_total += new_data->tasks[i].resmem;
 		ucycles_total += new_data->ucycles_diff[i];
 		kcycles_total += new_data->kcycles_diff[i];
@@ -234,6 +232,4 @@
 		FRACTION_TO_FLOAT(new_data->tasks_perc[i].virtmem,
 		    new_data->tasks[i].virtmem * 100, virtmem_total);
-		FRACTION_TO_FLOAT(new_data->tasks_perc[i].resmem,
-		    new_data->tasks[i].resmem * 100, resmem_total);
 		FRACTION_TO_FLOAT(new_data->tasks_perc[i].ucycles,
 		    new_data->ucycles_diff[i] * 100, ucycles_total);
Index: uspace/app/top/top.h
===================================================================
--- uspace/app/top/top.h	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ uspace/app/top/top.h	(revision ba544511759db62eded7601782f553315375b049)
@@ -77,5 +77,4 @@
 typedef struct {
 	fixed_float virtmem;
-	fixed_float resmem;
 	fixed_float ucycles;
 	fixed_float kcycles;
Index: uspace/app/trace/syscalls.c
===================================================================
--- uspace/app/trace/syscalls.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ uspace/app/trace/syscalls.c	(revision ba544511759db62eded7601782f553315375b049)
@@ -77,5 +77,5 @@
     [SYS_SYSINFO_GET_VALUE] = { "sysinfo_get_value",		3,	V_ERRNO },
     [SYS_SYSINFO_GET_DATA_SIZE] = { "sysinfo_get_data_size",	3,	V_ERRNO },
-    [SYS_SYSINFO_GET_DATA] = { "sysinfo_get_data",		5,	V_ERRNO },
+    [SYS_SYSINFO_GET_DATA] = { "sysinfo_get_data",		4,	V_ERRNO },
 
     [SYS_DEBUG_ENABLE_CONSOLE] = { "debug_enable_console", 0,	V_ERRNO },
Index: uspace/lib/c/generic/stats.c
===================================================================
--- uspace/lib/c/generic/stats.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ uspace/lib/c/generic/stats.c	(revision ba544511759db62eded7601782f553315375b049)
@@ -36,8 +36,8 @@
 #include <stats.h>
 #include <sysinfo.h>
+#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <inttypes.h>
-#include <malloc.h>
 
 #define SYSINFO_STATS_MAX_PATH  64
@@ -71,10 +71,5 @@
 	    (stats_cpu_t *) sysinfo_get_data("system.cpus", &size);
 	
-	if ((size % sizeof(stats_cpu_t)) != 0) {
-		if (stats_cpus != NULL)
-			free(stats_cpus);
-		*count = 0;
-		return NULL;
-	}
+	assert((size % sizeof(stats_cpu_t)) == 0);
 	
 	*count = size / sizeof(stats_cpu_t);
@@ -96,9 +91,5 @@
 	    (stats_physmem_t *) sysinfo_get_data("system.physmem", &size);
 	
-	if (size != sizeof(stats_physmem_t)) {
-		if (stats_physmem != NULL)
-			free(stats_physmem);
-		return NULL;
-	}
+	assert((size == sizeof(stats_physmem_t)) || (size == 0));
 	
 	return stats_physmem;
@@ -120,10 +111,5 @@
 	    (stats_task_t *) sysinfo_get_data("system.tasks", &size);
 	
-	if ((size % sizeof(stats_task_t)) != 0) {
-		if (stats_tasks != NULL)
-			free(stats_tasks);
-		*count = 0;
-		return NULL;
-	}
+	assert((size % sizeof(stats_task_t)) == 0);
 	
 	*count = size / sizeof(stats_task_t);
@@ -149,9 +135,5 @@
 	    (stats_task_t *) sysinfo_get_data(name, &size);
 	
-	if (size != sizeof(stats_task_t)) {
-		if (stats_task != NULL)
-			free(stats_task);
-		return NULL;
-	}
+	assert((size == sizeof(stats_task_t)) || (size == 0));
 	
 	return stats_task;
@@ -173,10 +155,5 @@
 	    (stats_thread_t *) sysinfo_get_data("system.threads", &size);
 	
-	if ((size % sizeof(stats_thread_t)) != 0) {
-		if (stats_threads != NULL)
-			free(stats_threads);
-		*count = 0;
-		return NULL;
-	}
+	assert((size % sizeof(stats_thread_t)) == 0);
 	
 	*count = size / sizeof(stats_thread_t);
@@ -202,9 +179,5 @@
 	    (stats_thread_t *) sysinfo_get_data(name, &size);
 	
-	if (size != sizeof(stats_thread_t)) {
-		if (stats_thread != NULL)
-			free(stats_thread);
-		return NULL;
-	}
+	assert((size == sizeof(stats_thread_t)) || (size == 0));
 	
 	return stats_thread;
@@ -226,10 +199,5 @@
 	    (stats_exc_t *) sysinfo_get_data("system.exceptions", &size);
 	
-	if ((size % sizeof(stats_exc_t)) != 0) {
-		if (stats_exceptions != NULL)
-			free(stats_exceptions);
-		*count = 0;
-		return NULL;
-	}
+	assert((size % sizeof(stats_exc_t)) == 0);
 	
 	*count = size / sizeof(stats_exc_t);
@@ -249,5 +217,5 @@
 {
 	char name[SYSINFO_STATS_MAX_PATH];
-	snprintf(name, SYSINFO_STATS_MAX_PATH, "system.exceptions.%u", excn);
+	snprintf(name, SYSINFO_STATS_MAX_PATH, "system.exceptionss.%u", excn);
 	
 	size_t size = 0;
@@ -255,9 +223,5 @@
 	    (stats_exc_t *) sysinfo_get_data(name, &size);
 	
-	if (size != sizeof(stats_exc_t)) {
-		if (stats_exception != NULL)
-			free(stats_exception);
-		return NULL;
-	}
+	assert((size == sizeof(stats_exc_t)) || (size == 0));
 	
 	return stats_exception;
@@ -279,10 +243,5 @@
 	    (load_t *) sysinfo_get_data("system.load", &size);
 	
-	if ((size % sizeof(load_t)) != 0) {
-		if (load != NULL)
-			free(load);
-		*count = 0;
-		return NULL;
-	}
+	assert((size % sizeof(load_t)) == 0);
 	
 	*count = size / sizeof(load_t);
Index: uspace/lib/c/generic/sysinfo.c
===================================================================
--- uspace/lib/c/generic/sysinfo.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ uspace/lib/c/generic/sysinfo.c	(revision ba544511759db62eded7601782f553315375b049)
@@ -96,38 +96,42 @@
 void *sysinfo_get_data(const char *path, size_t *size)
 {
-	/*
-	 * The binary data size might change during time.
-	 * Unfortunatelly we cannot allocate the buffer
-	 * and transfer the data as a single atomic operation.
-	 */
+	/* The binary data size might change during time.
+	   Unfortunatelly we cannot allocate the buffer
+	   and transfer the data as a single atomic operation.
 	
-	/* Get the binary data size */
-	int ret = sysinfo_get_data_size(path, size);
-	if ((ret != EOK) || (size == 0)) {
-		/*
-		 * Not a binary data item
-		 * or an empty item.
-		 */
-		*size = 0;
-		return NULL;
+	   Let's hope that the number of iterations is bounded
+	   in common cases. */
+	
+	void *data = NULL;
+	
+	while (true) {
+		/* Get the binary data size */
+		int ret = sysinfo_get_data_size(path, size);
+		if ((ret != EOK) || (size == 0)) {
+			/* Not a binary data item
+			   or an empty item */
+			break;
+		}
+		
+		data = realloc(data, *size);
+		if (data == NULL)
+			break;
+		
+		/* Get the data */
+		ret = __SYSCALL4(SYS_SYSINFO_GET_DATA, (sysarg_t) path,
+		    (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size);
+		if (ret == EOK)
+			return data;
+		
+		if (ret != ENOMEM) {
+			/* The failure to get the data was not caused
+			   by wrong buffer size */
+			break;
+		}
 	}
 	
-	void *data = malloc(*size);
-	if (data == NULL) {
-		*size = 0;
-		return NULL;
-	}
+	if (data != NULL)
+		free(data);
 	
-	/* Get the data */
-	size_t sz;
-	ret = __SYSCALL5(SYS_SYSINFO_GET_DATA, (sysarg_t) path,
-	    (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size,
-	    (sysarg_t) &sz);
-	if (ret == EOK) {
-		*size = sz;
-		return data;
-	}
-	
-	free(data);
 	*size = 0;
 	return NULL;
Index: uspace/lib/usb/Makefile
===================================================================
--- uspace/lib/usb/Makefile	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ uspace/lib/usb/Makefile	(revision ba544511759db62eded7601782f553315375b049)
@@ -47,6 +47,5 @@
 	src/usb.c \
 	src/usbdrvreq.c \
-	src/usbdrv.c \
-	src/usbmem.c
+	src/usbdrv.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: pace/lib/usb/include/usb/usbmem.h
===================================================================
--- uspace/lib/usb/include/usb/usbmem.h	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ 	(revision )
@@ -1,81 +1,0 @@
-/*
- * Copyright (c) 2010 Matus Dekanek
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef USBMEM_H
-#define	USBMEM_H
-
-
-// group should be changed - this is not usb specific
-/** @addtogroup usb
- * @{
- */
-/** @file definitions of special memory management, used mostly in usb stack
- *
- * USB HCD needs traslation between physical and virtual addresses. These
- * functions implement such functionality. For each allocated virtual address
- * the memory manager gets also it`s physical translation and remembers it.
- * Addresses allocated byt this manager can be therefore translated from and to
- * physical addresses.
- * Typical use:
- * void * address = mman_malloc(some_size);
- * void * physical_address = mman_getPA(address);
- * void * the_same_address = mman_getVA(physical_address);
- * void * null_address = mman_getPA(non_existing_address);
- * mman_free(address);
- * // physical_address, adress and the_same_address are no longer valid here
- *
- *
- * @note Addresses allocated by this memory manager should be as well
- * deallocated byt it.
- *
- */
-
-#include <sys/types.h>
-
-extern void * mman_malloc(
-		size_t size,
-		size_t alignment,
-		unsigned long max_physical_address);
-
-extern void * mman_getVA(void * addr);
-
-extern void * mman_getPA(void * addr);
-
-extern void mman_free(void * addr);
-
-
-
-
-
-
-/** @}
- */
-
-
-#endif	/* USBMEM_H */
-
Index: pace/lib/usb/src/usbmem.c
===================================================================
--- uspace/lib/usb/src/usbmem.c	(revision 9f0318ce846111224527cfd44c2da266cdec6498)
+++ 	(revision )
@@ -1,201 +1,0 @@
-/*
- * Copyright (c) 2010 Matus Dekanek
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libc
- * @{
- */
-/** @file implementation of special memory management, used mostly in usb stack
- */
-
-#include <adt/hash_table.h>
-#include <adt/list.h>
-#include <as.h>
-#include <errno.h>
-#include <malloc.h>
-
-#include "usb/usbmem.h"
-
-#define ADDR_BUCKETS 1537
-
-//address translation tables
-static hash_table_t * pa2va_table = NULL;
-static hash_table_t * va2pa_table = NULL;
-
-/**
- * address translation hashtable item
- */
-typedef struct{
-	unsigned long addr;
-	unsigned long translation;
-	link_t link;
-
-}addr_node_t;
-
-static hash_index_t addr_hash(unsigned long key[])
-{
-	return (3*key[0]<<4) % ADDR_BUCKETS;
-}
-
-static int addr_compare(unsigned long key[], hash_count_t keys,
-    link_t *item)
-{
-	addr_node_t *addr_node = hash_table_get_instance(item, addr_node_t, link);
-	return (addr_node->addr == key[0]);
-}
-
-static void addr_remove_callback(link_t *item)
-{
-	//delete item
-	addr_node_t *addr_node = hash_table_get_instance(item, addr_node_t, link);
-	free(addr_node);
-}
-
-
-static hash_table_operations_t addr_devices_ops = {
-	.hash = addr_hash,
-	.compare = addr_compare,
-	.remove_callback = addr_remove_callback
-};
-
-/**
- * create node for address translation hashtable
- * @param addr
- * @param translation
- * @return
- */
-static addr_node_t * create_addr_node(void * addr, void * translation){
-	addr_node_t * node = (addr_node_t*)malloc(sizeof(addr_node_t));
-	node->addr = (unsigned long)addr;
-	node->translation = (unsigned long)translation;
-	return node;
-}
-
-/**
- * allocate size on heap and register it`s pa<->va translation
- *
- * If physical address + size is 2GB or higher, nothing is allocated and NULL
- * is returned.
- * @param size
- * @param alignment
- * @return
- */
-void * mman_malloc(
-	size_t size,
-	size_t alignment,
-	unsigned long max_physical_address)
-{
-	if (size == 0)
-		return NULL;
-	if (alignment == 0)
-		return NULL;
-	//check if tables were initialized
-	if(!pa2va_table){
-		pa2va_table = (hash_table_t*)malloc(sizeof(hash_table_t*));
-		va2pa_table = (hash_table_t*)malloc(sizeof(hash_table_t*));
-		hash_table_create(pa2va_table, ADDR_BUCKETS, 1,
-		    &addr_devices_ops);
-		hash_table_create(va2pa_table, ADDR_BUCKETS, 1,
-		    &addr_devices_ops);
-	}
-	//allocate
-	void * vaddr = memalign(alignment, size);
-	
-	//get translation
-	void * paddr = NULL;
-	int opResult = as_get_physical_mapping(vaddr,(uintptr_t*)&paddr);
-	if(opResult != EOK){
-		//something went wrong
-		free(vaddr);
-		return NULL;
-	}
-	if((unsigned long)paddr + size > max_physical_address){
-		//unusable address for usb
-		free(vaddr);
-		return NULL;
-	}
-	//store translation
-	addr_node_t * pa2vaNode = create_addr_node(paddr,vaddr);
-	addr_node_t * va2paNode = create_addr_node(vaddr,paddr);
-
-	unsigned long keypaddr = (unsigned long)paddr;
-	unsigned long keyvaddr = (unsigned long)vaddr;
-	hash_table_insert(pa2va_table, (&keypaddr), &pa2vaNode->link);
-	hash_table_insert(va2pa_table, (&keyvaddr), &va2paNode->link);
-	//return
-	return vaddr;
-
-}
-
-/**
- * get virtual address from physical
- * @param addr
- * @return translated virtual address or null
- */
-void * mman_getVA(void * addr){
-	unsigned long keypaddr = (unsigned long)addr;
-	link_t * link = hash_table_find(pa2va_table, &keypaddr);
-	if(!link) return NULL;
-	addr_node_t * node = hash_table_get_instance(link, addr_node_t, link);
-	return (void*)node->translation;
-}
-
-/**
- * get physical address from virtual
- * @param addr
- * @return physical address or null
- */
-void * mman_getPA(void * addr){
-	unsigned long keyvaddr = (unsigned long)addr;
-	link_t * link = hash_table_find(va2pa_table, &keyvaddr);
-	if(!link) return NULL;
-	addr_node_t * node = hash_table_get_instance(link, addr_node_t, link);
-	return (void*)node->translation;
-}
-
-/**
- * free the address and deregister it from pa<->va translation
- * @param vaddr if NULL, nothing happens
- */
-void mman_free(void * vaddr){
-	if(!vaddr)
-		return;
-	//get paddress
-	void * paddr = mman_getPA(vaddr);
-	unsigned long keypaddr = (unsigned long)paddr;
-	unsigned long keyvaddr = (unsigned long)vaddr;
-	//remove mapping
-	hash_table_remove(pa2va_table,&keypaddr, 1);
-	hash_table_remove(va2pa_table,&keyvaddr, 1);
-	//free address
-	free(vaddr);
-}
-
-
-
-/** @}
- */
