Bitu XMS_FreeMemory(Bitu handle) { if (InvalidHandle(handle)) return XMS_INVALID_HANDLE; MEM_ReleasePages(xms_handles[handle].mem); xms_handles[handle].mem=-1; xms_handles[handle].size=0; xms_handles[handle].free=true; return 0; }
static Bit8u EMM_AllocateSystemHandle(Bit16u pages) { /* Check for enough free pages */ if ((MEM_FreeTotal()/ 4) < pages) { return EMM_OUT_OF_LOG;} Bit16u handle = 0; // emm system handle (reserved for OS usage) /* Release memory if already allocated */ if (emm_handles[handle].pages != NULL_HANDLE) { MEM_ReleasePages(emm_handles[handle].mem); } MemHandle mem = MEM_AllocatePages(pages*4,false); if (!mem) E_Exit("EMS:System handle memory allocation failure"); emm_handles[handle].pages = pages; emm_handles[handle].mem = mem; return EMM_NO_ERROR; }
/** * @brief Allocates a number of pages. * @param _address_space [in] The address space where the page will be mapped. * @param _address [in] The virtual address where the page will be mapped. * @param _number_of_pages [in] The number of pages to allocate. * @return The virtual address reserved, 0 otherwise. */ PUBLIC VIRTUAL XKY_PAGE_Alloc(IN ADDRESS_SPACE _address_space, IN VIRTUAL _address, IN dword _number_of_pages) { ADDRESS_SPACE current = ADDRESS_SPACE_GetCurrent(); ADDRESS_SPACE_ResetToKernelSpace(); //Do page allocation PHYSICAL pages = MEM_AllocPages(_number_of_pages, UserMode); if(!pages) { ADDRESS_SPACE_SwitchTo(current); return false; } bool success = ADDRESS_SPACE_Map(_address_space, pages, _address, _number_of_pages, UserMode, ReadWrite, true); if(!success) { MEM_ReleasePages(pages, _number_of_pages); } ADDRESS_SPACE_SwitchTo(current); return success?_address:0; }
bool MEM_ReAllocatePages(MemHandle & handle,Bitu pages,bool sequence) { if (handle<=0) { if (!pages) return true; handle=MEM_AllocatePages(pages,sequence); return (handle>0); } if (!pages) { MEM_ReleasePages(handle); handle=-1; return true; } MemHandle index=handle; MemHandle last;Bitu old_pages=0; while (index>0) { old_pages++; last=index; index=memory.mhandles[index]; } if (old_pages == pages) return true; if (old_pages > pages) { /* Decrease size */ pages--;index=handle;old_pages--; while (pages) { index=memory.mhandles[index]; pages--;old_pages--; } MemHandle next=memory.mhandles[index]; memory.mhandles[index]=-1; index=next; while (old_pages) { next=memory.mhandles[index]; memory.mhandles[index]=0; index=next; old_pages--; } return true; } else { /* Increase size, check for enough free space */ Bitu need=pages-old_pages; if (sequence) { index=last+1; Bitu free=0; while ((index<(MemHandle)memory.pages) && !memory.mhandles[index]) { index++;free++; } if (free>=need) { /* Enough space allocate more pages */ index=last; while (need) { memory.mhandles[index]=index+1; need--;index++; } memory.mhandles[index]=-1; return true; } else { /* Not Enough space allocate new block and copy */ MemHandle newhandle=MEM_AllocatePages(pages,true); if (!newhandle) return false; MEM_BlockCopy(newhandle*4096,handle*4096,old_pages*4096); MEM_ReleasePages(handle); handle=newhandle; return true; } } else { MemHandle rem=MEM_AllocatePages(need,false); if (!rem) return false; memory.mhandles[last]=rem; return true; } } return 0; }