![]() |
ZenWINX Architecture - Reference Manual - Guides |
|
00001 /* 00002 * ZenWINX - WIndows Native eXtended library. 00003 * Copyright (c) 2007-2010 by Dmitri Arkhangelski (dmitriar@gmail.com). 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00027 #include "ntndk.h" 00028 #include "zenwinx.h" 00029 00030 HANDLE hGlobalHeap = NULL; /* for winx_heap_alloc call */ 00031 00032 /* 00033 * NOTE: Never call winx_dbg_print_ex() and 00034 * winx_dbg_print() from these functions! 00035 */ 00036 00047 void * __stdcall winx_virtual_alloc(SIZE_T size) 00048 { 00049 void *addr = NULL; 00050 NTSTATUS Status; 00051 00052 Status = NtAllocateVirtualMemory(NtCurrentProcess(),&addr,0, 00053 &size,MEM_COMMIT | MEM_RESERVE,PAGE_READWRITE); 00054 return (NT_SUCCESS(Status)) ? addr : NULL; 00055 } 00056 00062 void __stdcall winx_virtual_free(void *addr,SIZE_T size) 00063 { 00064 (void)NtFreeVirtualMemory(NtCurrentProcess(),&addr,&size,MEM_RELEASE); 00065 } 00066 00072 void * __stdcall winx_heap_alloc(SIZE_T size) 00073 { 00074 if(hGlobalHeap == NULL) return NULL; 00075 return RtlAllocateHeap(hGlobalHeap,0/*HEAP_ZERO_MEMORY*/,size); 00076 } 00077 00082 void __stdcall winx_heap_free(void *addr) 00083 { 00084 if(hGlobalHeap && addr) (void)RtlFreeHeap(hGlobalHeap,0,addr); 00085 } 00086 00087 /* internal code */ 00088 void winx_create_global_heap(void) 00089 { 00090 /* create growable heap with initial size of 100 kb */ 00091 if(hGlobalHeap == NULL){ 00092 hGlobalHeap = RtlCreateHeap(HEAP_GROWABLE,NULL,0,100 * 1024,NULL,NULL); 00093 } 00094 if(hGlobalHeap == NULL){ 00095 DebugPrint("Cannot create global memory heap!\n"); 00096 winx_printf("\nCannot create global memory heap!\n"); 00097 } 00098 } 00099 00100 void winx_destroy_global_heap(void) 00101 { 00102 if(hGlobalHeap) (void)RtlDestroyHeap(hGlobalHeap); 00103 } 00104