lib: malloc: Flush the cache after allocating on heap

Flush the heap memory to make sure the caches are clean. If there is some
unwanted data in the cache it would get written to main memory when peripheral
drivers invalidate the cache after updating the memory.

Change-Id: Id695b9fb62cfa8dd3db61589565b23f5ace5ec29
diff --git a/lib/libc/malloc.c b/lib/libc/malloc.c
index 6f05f63..9cec44c 100644
--- a/lib/libc/malloc.c
+++ b/lib/libc/malloc.c
@@ -24,6 +24,7 @@
 #include <malloc.h>
 #include <string.h>
 #include <lib/heap.h>
+#include <arch/ops.h>
 
 void *malloc(size_t size)
 {
@@ -32,7 +33,11 @@
 
 void *memalign(size_t boundary, size_t size)
 {
-	return heap_alloc(size, boundary);
+	void *ptr;
+	ptr = heap_alloc(size, boundary);
+	/* Clean the cache before giving the memory */
+	arch_invalidate_cache_range((addr_t) ptr, size);
+	return ptr;
 }
 
 void *calloc(size_t count, size_t size)