WINCE内存分配操作的类(C#)

来源:互联网 发布:提速软件 编辑:程序博客网 时间:2024/05/22 03:45
  1. 转帖一个WINCE下C#写的内存分配的类,需要注意的是在WINCE的帮助文档上提到一些函数可能以后系统就不支持了,谨慎使用。
  2. MemoryAllocator v1.1:
  3. ======== Just for the search facility in OpenNetCF.org =============
  4. KeyWords: handle , memory , allocate , virtual , ram , rom , storage card , cf card , cfcard , program in use .
  5. ====================================================================
  6. ---------------------------------------------------------------------
  7. using System;
  8. using System.Runtime.InteropServices;
  9. namespace MemoryAllocator
  10. {
  11. ///
  12. ///###########################################################################################
  13. ///## Name of function/class/method...: MemoryAllocator
  14. ///## Version of function/class/method: v1.1
  15. ///## Version of software.............: v1.0
  16. ///## Author..........................: Gabriel Renom
  17. ///## Date............................: 1/December/2006 (Last update 4/12/2006)
  18. ///## Company.........................: Intellident
  19. ///##-----------------------------------------------------------------------------------------
  20. ///## Description: MemoryAllocater gives you the ability of setting your own memory (Mb/KB)
  21. ///## - SetMemoryInMegaBit ()
  22. ///## - SetMemoryInKB ()
  23. ///## It gets information about any kind of memory available/total from the system:
  24. ///## - Virtual Memory
  25. ///## - Physical Memory
  26. ///## - Page File
  27. ///## - Page Size
  28. ///## - CF CardFree Space
  29. ///##-----------------------------------------------------------------------------------------
  30. ///## IN: -
  31. ///##-----------------------------------------------------------------------------------------
  32. ///## OUT: -
  33. ///##-----------------------------------------------------------------------------------------
  34. ///## Dependencies: By default it uses API calls to "coredll.dll" and .NET libraries; System
  35. ///## and System.Runtime.InteropServices.
  36. ///###########################################################################################
  37. ///
  38. public class MemoryAllocator
  39. #region [ Variables ]
  40. private int iStoragePages = 0;
  41. private int iRamPages = 0;
  42. private int iPageSize = 0;
  43. #endregion
  44. #region [ Enums ]
  45. public enum ProcessorType : int
  46. {
  47. /// <summary>
  48. /// Processor is Intel 80386.
  49. /// </summary>
  50. Intel_386 = 386,
  51. /// <summary>
  52. /// Processor is Intel 80486.
  53. /// </summary>
  54. Intel_486 = 486,
  55. /// <summary>
  56. /// Processor is Intel Pentium (80586).
  57. /// </summary>
  58. Intel_Pentium = 586,
  59. /// <summary>
  60. /// Processor is Intel Pentium II (80686).
  61. /// </summary>
  62. Intel_PentiumII = 686,
  63. /// <summary>
  64. /// Processor is Intel 64bit (IA64).
  65. /// </summary>
  66. Intel_IA64 = 2200,
  67. /// <summary>
  68. /// Processor is MIPS R4000.
  69. /// </summary>
  70. MIPS_R4000 = 4000,
  71. /// <summary>
  72. /// Processor is Alpha 21064.
  73. /// </summary>
  74. Alpha_21064 = 21064,
  75. /// <summary>
  76. /// Processor is Power PC 403.
  77. /// </summary>
  78. PPC_403 = 403,
  79. /// <summary>
  80. /// Processor is Power PC 601.
  81. /// </summary>
  82. PPC_601 = 601,
  83. /// <summary>
  84. /// Processor is Power PC 603.
  85. /// </summary>
  86. PPC_603 = 603,
  87. /// <summary>
  88. /// Processor is Power PC 604.
  89. /// </summary>
  90. PPC_604 = 604,
  91. /// <summary>
  92. /// Processor is Power PC 620.
  93. /// </summary>
  94. PPC_620 = 620,
  95. /// <summary>
  96. /// Processor is Hitachi SH3.
  97. /// </summary>
  98. Hitachi_SH3 = 10003,
  99. /// <summary>
  100. /// Processor is Hitachi SH3E.
  101. /// </summary>
  102. Hitachi_SH3E = 10004,
  103. /// <summary>
  104. /// Processor is Hitachi SH4.
  105. /// </summary>
  106. Hitachi_SH4 = 10005,
  107. /// <summary>
  108. /// Processor is Motorola 821.
  109. /// </summary>
  110. Motorola_821 = 821,
  111. /// <summary>
  112. /// Processor is SH3.
  113. /// </summary>
  114. SHx_SH3 = 103,
  115. /// <summary>
  116. /// Processor is SH4.
  117. /// </summary>
  118. SHx_SH4 = 104,
  119. /// <summary>
  120. /// Processor is StrongARM.
  121. /// </summary>
  122. StrongARM = 2577,
  123. /// <summary>
  124. /// Processor is ARM 720.
  125. /// </summary>
  126. ARM720 = 1824,
  127. /// <summary>
  128. /// Processor is ARM 820.
  129. /// </summary>
  130. ARM820 = 2080,
  131. /// <summary>
  132. /// Processor is ARM 920.
  133. /// </summary>
  134. ARM920 = 2336,
  135. /// <summary>
  136. /// Processor is ARM 7 TDMI.
  137. /// </summary>
  138. ARM_7TDMI = 70001
  139. }
  140. public enum ProcessorArchitecture : short
  141. {
  142. /// <summary>
  143. /// Processor is Intel x86 based.
  144. /// </summary>
  145. Intel = 0,
  146. /// <summary>
  147. /// Processor is MIPS based.
  148. /// </summary>
  149. MIPS = 1,
  150. /// <summary>
  151. /// Processor is Alpha based.
  152. /// </summary>
  153. Alpha = 2,
  154. /// <summary>
  155. /// Processor is Power PC based.
  156. /// </summary>
  157. PPC = 3,
  158. /// <summary>
  159. /// Processor is SH3, SH4 etc.
  160. /// </summary>
  161. SHX = 4,
  162. /// <summary>
  163. /// Processor is ARM based.
  164. /// </summary>
  165. ARM = 5,
  166. /// <summary>
  167. /// Processor is Intel 64bit.
  168. /// </summary>
  169. IA64 = 6,
  170. /// <summary>
  171. /// Processor is Alpha 64bit.
  172. /// </summary>
  173. Alpha64 = 7,
  174. /// <summary>
  175. /// Unknown processor architecture.
  176. /// </summary>
  177. Unknown = -1,
  178. }
  179. #endregion
  180. #region [ DLL Calls ]
  181. [DllImport("coredll.dll", SetLastError=true)]
  182. internal static extern bool SetSystemMemoryDivision(int dwStorePages );
  183. [DllImport("coredll", SetLastError = true)] 
  184. private static extern bool GetSystemMemoryDivision(ref int storagepages, ref int rampages, ref int pageSizepages); 
  185. [DllImport("coredll", EntryPoint="GetSystemInfo",SetLastError=true)]
  186. internal static extern void GetSystemInfoCE(out SystemInfo pSI); 
  187. [DllImport("coredll", EntryPoint="GlobalMemoryStatus", SetLastError=false)]
  188. internal static extern void GlobalMemoryStatusCE(out MemoryStatus msce);
  189. [DllImport("coredll", EntryPoint="GetDiskFreeSpaceEx", SetLastError = true)]
  190. internal static extern bool GetFreeSpace(string DirectoryName,
  191. ref long FreeBytesAvailableToCaller,
  192. ref long TotalNumberOfBytes,
  193. ref long TotalNumberOfFreeBytes);
  194. #endregion
  195. #region [ Structures ]
  196. public struct MemoryStatus
  197. {
  198. internal uint dwLength;
  199. /// <summary>
  200. /// Specifies a number between 0 and 100 that gives a general idea of current memory utilization, in which 0 indicates no memory use and 100 indicates full memory use.
  201. /// </summary>
  202. public int MemoryLoad;
  203. /// <summary>
  204. /// Indicates the total number of bytes of physical memory.
  205. /// </summary>
  206. public int TotalPhysical;
  207. /// <summary>
  208. /// Indicates the number of bytes of physical memory available.
  209. /// </summary>
  210. public int AvailablePhysical;
  211. /// <summary>
  212. /// Indicates the total number of bytes that can be stored in the paging file. Note that this number does not represent the actual physical size of the paging file on disk.
  213. /// </summary>
  214. public int TotalPageFile; 
  215. /// <summary>
  216. /// Indicates the number of bytes available in the paging file.
  217. /// </summary>
  218. public int AvailablePageFile; 
  219. /// <summary>
  220. /// Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.
  221. /// </summary>
  222. public int TotalVirtual; 
  223. /// <summary>
  224. /// Indicates the number of bytes of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process.
  225. /// </summary>
  226. public int AvailableVirtual;
  227. }
  228. public struct SystemInfo
  229. {
  230. /// <summary>
  231. /// The system's processor architecture.
  232. /// </summary>
  233. public ProcessorArchitecture ProcessorArchitecture;
  234. internal ushort wReserved;
  235. /// <summary>
  236. /// The page size and the granularity of page protection and commitment.
  237. /// </summary>
  238. public int PageSize;
  239. /// <summary>
  240. /// Pointer to the lowest memory address accessible to applications and dynamic-link libraries (DLLs). 
  241. /// </summary>
  242. public int MinimumApplicationAddress;
  243. /// <summary>
  244. /// Pointer to the highest memory address accessible to applications and DLLs.
  245. /// </summary>
  246. public int MaximumApplicationAddress;
  247. /// <summary>
  248. /// Specifies a mask representing the set of processors configured into the system. Bit 0 is processor 0; bit 31 is processor 31. 
  249. /// </summary>
  250. public int ActiveProcessorMask;
  251. /// <summary>
  252. /// Specifies the number of processors in the system.
  253. /// </summary>
  254. public int NumberOfProcessors;
  255. /// <summary>
  256. /// Specifies the type of processor in the system.
  257. /// </summary>
  258. public ProcessorType ProcessorType;
  259. /// <summary>
  260. /// Specifies the granularity with which virtual memory is allocated.
  261. /// </summary>
  262. public int AllocationGranularity;
  263. /// <summary>
  264. /// Specifies the system抯 architecture-dependent processor level.
  265. /// </summary>
  266. public short ProcessorLevel;
  267. /// <summary>
  268. /// Specifies an architecture-dependent processor revision.
  269. /// </summary>
  270. public short ProcessorRevision;
  271. }
  272. #endregion
  273. #region [ Properties ]
  274. /// <summary>
  275. /// It gets available virtual memory.
  276. /// </summary>
  277. public int AvailableVirtualMemory
  278. {
  279. get{return(this.GetAvailableVirtual());}
  280. }
  281. /// <summary>
  282. /// It gets available physical memory.
  283. /// </summary>
  284. public int AvailablePhysicalMemory
  285. {
  286. get{return(this.GetAvailablePhysical());}
  287. }
  288. /// <summary>
  289. /// It gets available page file.
  290. /// </summary>
  291. public int AvailablePageFile
  292. {
  293. get{return(this.GetAvailablePageFile());}
  294. }
  295. /// <summary>
  296. /// It gets total virtual memory.
  297. /// </summary>
  298. public int TotalVirtual
  299. {
  300. get{return(this.GetTotalVirtual());}
  301. }
  302. /// <summary>
  303. /// It gets total physical memory.
  304. /// </summary>
  305. public int TotalPhysical
  306. {
  307. get{return(this.GetTotalPhysical());}
  308. }
  309. /// <summary>
  310. /// It gets total page file.
  311. /// </summary>
  312. public int TotalPageFile
  313. {
  314. get{return(this.GetTotalPageFile());}
  315. }
  316. /// <summary>
  317. /// It gets page size.
  318. /// </summary>
  319. public int PageSize
  320. {
  321. get{return(this.GetPageSize());}
  322. }
  323. /// <summary>
  324. /// It gets Memory Division (API 32) Storage Pages.
  325. /// </summary>
  326. public int MDStoragePages
  327. {
  328. get
  329. {
  330. SystemMemoryDivision();
  331. return(this.iStoragePages);
  332. }
  333. }
  334. /// <summary>
  335. /// It gets Memory Division (API 32) Ram Pages.
  336. /// </summary>
  337. public int MDRamPages
  338. {
  339. get
  340. {
  341. SystemMemoryDivision();
  342. return(this.iRamPages);
  343. }
  344. }
  345. /// <summary>
  346. /// It gets Memory Division (API 32) Page Size.
  347. /// </summary>
  348. public int MDPageSize
  349. {
  350. get
  351. {
  352. SystemMemoryDivision();
  353. return(this.iPageSize);
  354. }
  355. }
  356. /// <summary>
  357. /// It gets the memory available in Ram (return bytes).
  358. /// </summary>
  359. public int MemoryInUse
  360. {
  361. get
  362. return(GetTotalPhysical()-GetAvailablePhysical());
  363. }
  364. }
  365. /// <summary>
  366. /// It gets the free space available in the storage card.
  367. /// </summary>
  368. public long CFCardFreeSpace
  369. {
  370. get
  371. {
  372. long lUserBytesFree = 0;
  373. long lTotalBytes = 0;
  374. long lTotalBytesFree = 0;
  375. GetFreeSpace(@"/Storage Card"ref lUserBytesFree, ref lTotalBytes, ref lTotalBytesFree);
  376. return lTotalBytesFree;
  377. }
  378. }
  379. #endregion
  380. #region [ Constructors ]
  381. public MemoryAllocator()
  382. {
  383. }
  384. #endregion
  385. #region [ Private Methods ]
  386. /// <summary>
  387. /// Gets the Global Memory Status....
  388. /// </summary>
  389. /// <returns></returns>
  390. private static MemoryStatus GlobalMemoryStatus()
  391. {
  392. MemoryStatus ms = new MemoryStatus();
  393. GlobalMemoryStatusCE( out ms );
  394. return ms;
  395. }
  396. /// <summary>
  397. /// Gets the System Info...
  398. /// </summary>
  399. /// <returns></returns>
  400. private static SystemInfo GetSystemInfo()
  401. {
  402. SystemInfo pSI = new SystemInfo();
  403. try 
  404. {
  405. GetSystemInfoCE(out pSI);
  406. }
  407. catch(Exception)
  408. {
  409. //throw new WinAPIException("Error retrieving system info.");
  410. }
  411. return pSI;
  412. }
  413. /// <summary>
  414. /// Get the available Virtual memory from the system.
  415. /// </summary>
  416. /// <returns>Memory...</returns>
  417. private int GetAvailableVirtual ()
  418. {
  419. MemoryStatus mem = new MemoryStatus();
  420. mem = GlobalMemoryStatus();
  421. return(mem.AvailableVirtual);
  422. }
  423. /// <summary>
  424. /// Get the available Physical memory from the system.
  425. /// </summary>
  426. /// <returns>Memory...</returns>
  427. private int GetAvailablePhysical()
  428. {
  429. MemoryStatus mem = new MemoryStatus();
  430. mem = GlobalMemoryStatus();
  431. return(mem.AvailablePhysical);
  432. }
  433. /// <summary>
  434. /// Get the available Page File memory from the system.
  435. /// </summary>
  436. /// <returns>Memory...</returns>
  437. private int GetAvailablePageFile()
  438. {
  439. MemoryStatus mem = new MemoryStatus();
  440. mem = GlobalMemoryStatus();
  441. return(mem.AvailablePageFile);
  442. }
  443. /// <summary>
  444. /// Get total Virtual memory from the system.
  445. /// </summary>
  446. /// <returns>Memory...</returns>
  447. private int GetTotalVirtual ()
  448. {
  449. MemoryStatus mem = new MemoryStatus();
  450. mem = GlobalMemoryStatus();
  451. return(mem.TotalVirtual);
  452. }
  453. /// <summary>
  454. /// Get total Physical memory from the system.
  455. /// </summary>
  456. /// <returns>Memory...</returns>
  457. private int GetTotalPhysical ()
  458. {
  459. MemoryStatus mem = new MemoryStatus();
  460. mem = GlobalMemoryStatus();
  461. return(mem.TotalPhysical);
  462. }
  463. /// <summary>
  464. /// Get total Page File memory from the system.
  465. /// </summary>
  466. /// <returns>Memory...</returns>
  467. private int GetTotalPageFile ()
  468. {
  469. MemoryStatus mem = new MemoryStatus();
  470. mem = GlobalMemoryStatus();
  471. return(mem.TotalPageFile);
  472. }
  473. /// <summary>
  474. /// Get the Page size from the system.
  475. /// </summary>
  476. /// <returns>Memory...</returns>
  477. private int GetPageSize ()
  478. {
  479. SystemInfo sys = new SystemInfo();
  480. sys = GetSystemInfo();
  481. return(sys.PageSize);
  482. }
  483. #endregion
  484. #region [ Public Methods ]
  485. /// <summary>
  486. /// It sets Allocate memory in Megas
  487. /// </summary>
  488. /// <param name="iMemory">Number of Megas</param>
  489. /// <returns>True(OK)/False(Fail)</returns>
  490. public bool SetMemoryInMegaBit (int iMemory)
  491. {
  492. iMemory = iMemory * 256;
  493. return (SetSystemMemoryDivision(iMemory));
  494. }
  495. /// <summary>
  496. /// It sets Allocate memory in KB, the memory has to be allocated in pages of 256KB
  497. /// </summary>
  498. /// <param name="iMemory">Number of KB</param>
  499. /// <returns>True(OK)/False(Fail)</returns>
  500. public bool SetMemoryInKB (int iMemory)
  501. return (SetSystemMemoryDivision(iMemory));
  502. }
  503. /// <summary>
  504. /// It gets storage, ram and page size pages.
  505. /// </summary>
  506. /// <param name="iStoragePages">Storage Pages, it has to be passed by reference to get the value back</param>
  507. /// <param name="iRamPages">Ram Pages, it has to be passed by reference to get the value back</param>
  508. /// <param name="iPageSizePages">Page Size Pages, it has to be passed by reference to get the value back</param>
  509. /// <returns></returns>
  510. public bool SystemMemoryDivision(ref int iStoragePages_, ref int iRamPages_, ref int iPageSizePages_)
  511. {
  512. return(GetSystemMemoryDivision(ref iStoragePages_, ref iRamPages_, ref iPageSizePages_));
  513. }
  514. public bool SystemMemoryDivision()
  515. return (GetSystemMemoryDivision(ref iStoragePages, ref iRamPages, ref iPageSize));
  516. }
  517. /// <summary>
  518. /// It gets the Processor Architecture.
  519. /// </summary>
  520. /// <returns>Processor Architecture</returns>
  521. public string GetProcessorArchitecture()
  522. {
  523. SystemInfo sys = new SystemInfo();
  524. ProcessorArchitecture PA = new ProcessorArchitecture();
  525. sys = GetSystemInfo();
  526. PA = sys.ProcessorArchitecture;
  527. return(PA.ToString());
  528. }
  529. /// <summary>
  530. /// It gets the Allocation Granularity with Virtual Memory is allocated.
  531. /// </summary>
  532. /// <returns>Processor Architecture</returns>
  533. public int GetAllocationGranularity()
  534. {
  535. SystemInfo sys = new SystemInfo();
  536. ProcessorArchitecture PA = new ProcessorArchitecture();
  537. sys = GetSystemInfo();
  538. return(sys.AllocationGranularity);
  539. }
  540. public bool ReAllocateDivision ()
  541. {
  542. long X = 0;
  543. long L = 0;
  544. int PageSize = 0;
  545. X = ((this.GetAvailableVirtual()) - (this.GetAvailablePhysical())) / 2;
  546. L = X / this.GetPageSize(); 
  547. PageSize = System.Int32.Parse(L.ToString());
  548. return (SetSystemMemoryDivision(PageSize));
  549. }
  550. #endregion 
  551. }
  552. }
原创粉丝点击