X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=rts%2Fwin32%2FOSMem.c;h=99c8b111b230ca420ea51223808654093f6fddfc;hb=c53c1c16f47f331e3c1608f66e0b760d140105e6;hp=f4c126403abfa852fd8d4e80880509e9f6d84fe5;hpb=016bac72c041e8589ece82fe86906fc9cf8f950c;p=ghc-hetmet.git diff --git a/rts/win32/OSMem.c b/rts/win32/OSMem.c index f4c1264..99c8b11 100644 --- a/rts/win32/OSMem.c +++ b/rts/win32/OSMem.c @@ -7,9 +7,8 @@ * ---------------------------------------------------------------------------*/ #include "Rts.h" -#include "OSMem.h" +#include "sm/OSMem.h" #include "RtsUtils.h" -#include "RtsMessages.h" #if HAVE_WINDOWS_H #include @@ -46,7 +45,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 +58,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 +67,7 @@ allocNew(nat n) { rec->next=it->next; it->next=rec; - allocs=temp.next; + allocs=temp.next; } return rec; } @@ -169,8 +168,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 +183,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 +203,42 @@ 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 osFreeAllMBlocks(void) { @@ -226,7 +261,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 +274,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("makeExecutable: 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); }