IPv6, and I need to learn to compile before commiting
[tpg/acess2.git] / Kernel / heap.c
index 30c812e..61f7881 100644 (file)
@@ -2,11 +2,12 @@
  * AcessOS Microkernel Version
  * heap.c
  */
-#include <common.h>
+#include <acess.h>
 #include <mm_virt.h>
 #include <heap.h>
 
 #define WARNINGS       1
+#define        DEBUG_TRACE     0
 
 // === CONSTANTS ===
 #define HEAP_BASE      0xE0800000
@@ -29,7 +30,7 @@ void  free(void *Ptr);
 void   Heap_Dump();
 
 // === GLOBALS ===
- int   giHeapSpinlock;
+ int   glHeap;
 void   *gHeapStart;
 void   *gHeapEnd;
 
@@ -139,7 +140,7 @@ void *malloc(size_t Bytes)
        Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + BLOCK_SIZE-1) & ~(BLOCK_SIZE-1);
        
        // Lock Heap
-       LOCK(&giHeapSpinlock);
+       LOCK(&glHeap);
        
        // Traverse Heap
        for( head = gHeapStart;
@@ -153,6 +154,7 @@ void *malloc(size_t Bytes)
                        Warning("Size of heap address %p is invalid not aligned (0x%x)", head, head->Size);
                        Heap_Dump();
                        #endif
+                       RELEASE(&glHeap);
                        return NULL;
                }
                
@@ -164,7 +166,7 @@ void *malloc(size_t Bytes)
                        Warning("Magic of heap address %p is invalid (0x%x)", head, head->Magic);
                        Heap_Dump();
                        #endif
-                       RELEASE(&giHeapSpinlock);       // Release spinlock
+                       RELEASE(&glHeap);       // Release spinlock
                        return NULL;
                }
                
@@ -174,8 +176,10 @@ void *malloc(size_t Bytes)
                // Perfect fit
                if(head->Size == Bytes) {
                        head->Magic = MAGIC_USED;
-                       RELEASE(&giHeapSpinlock);       // Release spinlock
+                       RELEASE(&glHeap);       // Release spinlock
+                       #if DEBUG_TRACE
                        LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0));
+                       #endif
                        return best->Data;
                }
                
@@ -199,13 +203,15 @@ void *malloc(size_t Bytes)
                best = Heap_Extend( Bytes );
                // Check for errors
                if(!best) {
-                       RELEASE(&giHeapSpinlock);       // Release spinlock
+                       RELEASE(&glHeap);       // Release spinlock
                        return NULL;
                }
                // Check size
                if(best->Size == Bytes) {
-                       RELEASE(&giHeapSpinlock);       // Release spinlock
+                       RELEASE(&glHeap);       // Release spinlock
+                       #if DEBUG_TRACE
                        LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0));
+                       #endif
                        return best->Data;
                }
        }
@@ -223,8 +229,10 @@ void *malloc(size_t Bytes)
        best->Size = Bytes;             // Update size in old header
        best->Magic = MAGIC_USED;       // Mark block as used
        
-       RELEASE(&giHeapSpinlock);       // Release spinlock
+       RELEASE(&glHeap);       // Release spinlock
+       #if DEBUG_TRACE
        LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0));
+       #endif
        return best->Data;
 }
 
@@ -237,12 +245,14 @@ void free(void *Ptr)
        tHeapHead       *head;
        tHeapFoot       *foot;
        
+       #if DEBUG_TRACE
        LOG("Ptr = %p", Ptr);
        LOG("Returns to %p", __builtin_return_address(0));
+       #endif
        
        // Alignment Check
        if( (Uint)Ptr & (sizeof(Uint)-1) ) {
-               Warning("free - Passed a non-aligned address (%p)\n", Ptr);
+               Warning("free - Passed a non-aligned address (%p)", Ptr);
                return;
        }
        
@@ -256,7 +266,7 @@ void free(void *Ptr)
        // Check memory block - Header
        head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
        if(head->Magic == MAGIC_FREE) {
-               Warning("free - Passed a freed block (%p)\n", head);
+               Warning("free - Passed a freed block (%p) by %p", head, __builtin_return_address(0));
                return;
        }
        if(head->Magic != MAGIC_USED) {
@@ -271,12 +281,12 @@ void free(void *Ptr)
                return;
        }
        if(foot->Magic != MAGIC_FOOT) {
-               Warning("free - Footer magic is invalid (%p, 0x%x)\n", head, foot->Magic);
+               Warning("free - Footer magic is invalid (%p, %p = 0x%x)\n", head, &foot->Magic, foot->Magic);
                return;
        }
        
        // Lock
-       LOCK( &giHeapSpinlock );
+       LOCK( &glHeap );
        
        // Mark as free
        head->Magic = MAGIC_FREE;
@@ -284,7 +294,7 @@ void free(void *Ptr)
        Heap_Merge( head );
        
        // Release
-       RELEASE( &giHeapSpinlock );
+       RELEASE( &glHeap );
 }
 
 /**
@@ -359,6 +369,22 @@ void *realloc(void *__ptr, size_t __size)
        return NULL;
 }
 
+/**
+ * \fn void *calloc(size_t num, size_t size)
+ * \brief Allocate and Zero a buffer in memory
+ * \param num  Number of elements
+ * \param size Size of each element
+ */
+void *calloc(size_t num, size_t size)
+{
+       void    *ret = malloc(num*size);
+       if(ret == NULL) return NULL;
+       
+       memset( ret, 0, num*size );
+       
+       return ret;
+}
+
 /**
  * \fn int IsHeap(void *Ptr)
  * \brief Checks if an address is a heap address
@@ -419,3 +445,8 @@ void Heap_Dump()
        }
 }
 #endif
+
+// === EXPORTS ===
+EXPORT(malloc);
+EXPORT(realloc);
+EXPORT(free);

UCC git Repository :: git.ucc.asn.au