Refactor SrcLoc and SrcSpan
[ghc-hetmet.git] / rts / win32 / OSMem.c
index 4edb5bf..ad897e5 100644 (file)
@@ -7,17 +7,13 @@
  * ---------------------------------------------------------------------------*/
 
 #include "Rts.h"
-#include "OSMem.h"
+#include "sm/OSMem.h"
 #include "RtsUtils.h"
-#include "RtsMessages.h"
 
 #if HAVE_WINDOWS_H
 #include <windows.h>
 #endif
 
-/* alloc_rec keeps the info we need to have matching VirtualAlloc and
-   VirtualFree calls.
-*/
 typedef struct alloc_rec_ {
     char* base;     /* non-aligned base address, directly from VirtualAlloc */
     int size;       /* Size in bytes */
@@ -30,7 +26,12 @@ typedef struct block_rec_ {
     struct block_rec_* next;
 } block_rec;
 
+/* allocs are kept in ascending order, and are the memory regions as
+   returned by the OS as we need to have matching VirtualAlloc and
+   VirtualFree calls. */
 static alloc_rec* allocs = NULL;
+
+/* free_blocks are kept in ascending order, and adjacent blocks are merged */
 static block_rec* free_blocks = NULL;
 
 void
@@ -46,7 +47,7 @@ allocNew(nat n) {
     alloc_rec* rec;
     rec = (alloc_rec*)stgMallocBytes(sizeof(alloc_rec),"getMBlocks: allocNew");
     rec->size = (n+1)*MBLOCK_SIZE;
-    rec->base = 
+    rec->base =
         VirtualAlloc(NULL, rec->size, MEM_RESERVE, PAGE_READWRITE);
     if(rec->base==0) {
         stgFree((void*)rec);
@@ -59,8 +60,8 @@ allocNew(nat n) {
                 "getMBlocks: VirtualAlloc MEM_RESERVE %d blocks failed", n);
         }
     } else {
-               alloc_rec temp;
-               temp.base=0; temp.size=0; temp.next=allocs;
+        alloc_rec temp;
+        temp.base=0; temp.size=0; temp.next=allocs;
 
         alloc_rec* it;
         it=&temp;
@@ -68,7 +69,7 @@ allocNew(nat n) {
         rec->next=it->next;
         it->next=rec;
 
-               allocs=temp.next;
+        allocs=temp.next;
     }
     return rec;
 }
@@ -169,8 +170,8 @@ commitBlocks(char* base, int size) {
         temp = VirtualAlloc(base, size_delta, MEM_COMMIT, PAGE_READWRITE);
         if(temp==0) {
             sysErrorBelch("getMBlocks: VirtualAlloc MEM_COMMIT failed");
-           stg_exit(EXIT_FAILURE);
-       }
+            stg_exit(EXIT_FAILURE);
+        }
         size-=size_delta;
         base+=size_delta;
     }
@@ -184,12 +185,12 @@ osGetMBlocks(nat n) {
         alloc_rec* alloc;
         alloc = allocNew(n);
         /* We already belch in allocNew if it fails */
-       if (alloc == 0) {
-           stg_exit(EXIT_FAILURE);
-       } else {
+        if (alloc == 0) {
+            stg_exit(EXIT_FAILURE);
+        } else {
             insertFree(alloc->base, alloc->size);
             ret = findFreeBlocks(n);
-       }
+        }
     }
 
     if(ret!=0) {
@@ -204,6 +205,135 @@ osGetMBlocks(nat n) {
     return ret;
 }
 
+void osFreeMBlocks(char *addr, nat n)
+{
+    alloc_rec *p;
+    lnat nBytes = (lnat)n * MBLOCK_SIZE;
+
+    insertFree(addr, nBytes);
+
+    p = allocs;
+    while ((p != NULL) && (addr >= (p->base + p->size))) {
+        p = p->next;
+    }
+    while (nBytes > 0) {
+        if ((p == NULL) || (p->base > addr)) {
+            errorBelch("Memory to be freed isn't allocated\n");
+            stg_exit(EXIT_FAILURE);
+        }
+        if (p->base + p->size >= addr + nBytes) {
+            if (!VirtualFree(addr, nBytes, MEM_DECOMMIT)) {
+                sysErrorBelch("osFreeMBlocks: VirtualFree MEM_DECOMMIT failed");
+                stg_exit(EXIT_FAILURE);
+            }
+            nBytes = 0;
+        }
+        else {
+            lnat bytesToFree = p->base + p->size - addr;
+            if (!VirtualFree(addr, bytesToFree, MEM_DECOMMIT)) {
+                sysErrorBelch("osFreeMBlocks: VirtualFree MEM_DECOMMIT failed");
+                stg_exit(EXIT_FAILURE);
+            }
+            addr += bytesToFree;
+            nBytes -= bytesToFree;
+            p = p->next;
+        }
+    }
+}
+
+void osReleaseFreeMemory(void)
+{
+    alloc_rec *prev_a, *a;
+    alloc_rec head_a;
+    block_rec *prev_fb, *fb;
+    block_rec head_fb;
+    char *a_end, *fb_end;
+
+    /* go through allocs and free_blocks in lockstep, looking for allocs
+       that are completely free, and uncommit them */
+
+    head_a.base = 0;
+    head_a.size = 0;
+    head_a.next = allocs;
+    head_fb.base = 0;
+    head_fb.size = 0;
+    head_fb.next = free_blocks;
+    prev_a = &head_a;
+    a = allocs;
+    prev_fb = &head_fb;
+    fb = free_blocks;
+
+    while (a != NULL) {
+        a_end = a->base + a->size;
+        /* If a is freeable then there is a single freeblock in fb that
+           covers it. The end of this free block must be >= the end of
+           a, so skip anything in fb that ends before a. */
+        while (fb != NULL && fb->base + fb->size < a_end) {
+            prev_fb = fb;
+            fb = fb->next;
+        }
+
+        if (fb == NULL) {
+            /* If we have nothing left in fb, then neither a nor
+               anything later in the list is freeable, so we are done. */
+            break;
+        }
+        else {
+            fb_end = fb->base + fb->size;
+            /* We have a candidate fb. But does it really cover a? */
+            if (fb->base <= a->base) {
+                /* Yes, the alloc is within the free block. Now we need
+                   to know if it sticks out at either end. */
+                if (fb_end == a_end) {
+                    if (fb->base == a->base) {
+                        /* fb and a are identical, so just free fb */
+                        prev_fb->next = fb->next;
+                        stgFree(fb);
+                        fb = prev_fb->next;
+                    }
+                    else {
+                        /* fb begins earlier, so truncate it to not include a */
+                        fb->size = a->base - fb->base;
+                    }
+                }
+                else {
+                    /* fb ends later, so we'll make fb just be the part
+                       after a. First though, if it also starts earlier,
+                       we make a new free block record for the before bit. */
+                    if (fb->base != a->base) {
+                        block_rec *new_fb;
+
+                        new_fb = (block_rec *)stgMallocBytes(sizeof(block_rec),"osReleaseFreeMemory");
+                        new_fb->base = fb->base;
+                        new_fb->size = a->base - fb->base;
+                        new_fb->next = fb;
+                        prev_fb->next = new_fb;
+                    }
+                    fb->size = fb_end - a_end;
+                    fb->base = a_end;
+                }
+                /* Now we can free the alloc */
+                prev_a->next = a->next;
+                if(!VirtualFree((void *)a->base, 0, MEM_RELEASE)) {
+                    sysErrorBelch("freeAllMBlocks: VirtualFree MEM_RELEASE failed");
+                    stg_exit(EXIT_FAILURE);
+                }
+                stgFree(a);
+                a = prev_a->next;
+            }
+            else {
+                /* Otherwise this alloc is not freeable, so go on to the
+                   next one */
+                prev_a = a;
+                a = a->next;
+            }
+        }
+    }
+
+    allocs = head_a.next;
+    free_blocks = head_fb.next;
+}
+
 void
 osFreeAllMBlocks(void)
 {
@@ -226,7 +356,7 @@ osFreeAllMBlocks(void)
         for(; it!=0; ) {
             if(!VirtualFree((void*)it->base, 0, MEM_RELEASE)) {
                 sysErrorBelch("freeAllMBlocks: VirtualFree MEM_RELEASE failed");
-               stg_exit(EXIT_FAILURE);
+                stg_exit(EXIT_FAILURE);
             }
             next = it->next;
             stgFree(it);
@@ -239,23 +369,23 @@ lnat getPageSize (void)
 {
     static lnat pagesize = 0;
     if (pagesize) {
-       return pagesize;
+        return pagesize;
     } else {
-       SYSTEM_INFO sSysInfo;
-       GetSystemInfo(&sSysInfo);
-       pagesize = sSysInfo.dwPageSize;
-       return pagesize;
+        SYSTEM_INFO sSysInfo;
+        GetSystemInfo(&sSysInfo);
+        pagesize = sSysInfo.dwPageSize;
+        return pagesize;
     }
 }
 
 void setExecutable (void *p, lnat len, rtsBool exec)
 {
     DWORD dwOldProtect = 0;
-    if (VirtualProtect (p, len, 
-                       exec ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, 
-                       &dwOldProtect) == 0)
+    if (VirtualProtect (p, len,
+                        exec ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE,
+                        &dwOldProtect) == 0)
     {
-       sysErrorBelch("setExecutable: failed to protect 0x%p; old protection: %lu\n",
+        sysErrorBelch("setExecutable: failed to protect 0x%p; old protection: %lu\n",
                       p, (unsigned long)dwOldProtect);
         stg_exit(EXIT_FAILURE);
     }