home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / os / mswindo / programm / misc / 4253 < prev    next >
Encoding:
Text File  |  1992-12-15  |  4.5 KB  |  107 lines

  1. Newsgroups: comp.os.ms-windows.programmer.misc
  2. From: chris@chrism.demon.co.uk (Chris Marriott)
  3. Path: sparky!uunet!pipex!demon!chrism.demon.co.uk!chris
  4. Subject: Re: Segmented Memory Woes and Portability 
  5. Distribution: world
  6. References: <wf=Fjj_00WB8QZoUYr@andrew.cmu.edu>
  7. Organization: None
  8. Reply-To: chris@chrism.demon.co.uk
  9. X-Mailer: Simple NEWS 1.90 (ka9q DIS 1.19)
  10. Lines: 92
  11. Date: Tue, 15 Dec 1992 14:44:19 +0000
  12. Message-ID: <724430659snz@chrism.demon.co.uk>
  13. Sender: usenet@demon.co.uk
  14.  
  15. In article <wf=Fjj_00WB8QZoUYr@andrew.cmu.edu> ak10+@andrew.cmu.edu writes:
  16.  
  17. >I naively assumed that all of the segmented memory issues would be
  18. >more or less transparent to me and that the compiler would take care
  19. >of pointer conversions, alignment problems, etc.  I figured if I
  20. >compiled my code with the Compact option, my pointer declarations
  21. >would be interpreted as far pointers and that I could address within a
  22. >1 Mb address space without changing a bit of my code.  This doesn't
  23. >_seem_ to work, though I just started and still don't know very much
  24. >about the 80x86.
  25.  
  26. Hmmm.  This should work OK.  It *could* be a structure packing problem.
  27.  
  28. >Alternatively, is it possible to simply call GlobalAlloc() or whatever
  29. >the function is to allocate memory from Windows global heap, convert the handle
  30. >to a far/huge pointer (the pointer declarations somehow declared this way
  31. >implicitly), lock the memory then when you're done with it, convert the
  32. >pointer back to a handle, unlock and free it?  This seems like sensible
  33. >behavior -- assuming that this doesn't force Windows to keep in memory
  34. >stuff that should it should be able to move and swap in/out.
  35. >
  36.  
  37. Yes - absolutely.  In fact, the standard Windows 3.1 header file
  38. "windowsx.h" contains the macros "GlobalAllocPtr" and "GlobalFreePtr" for
  39. this very purpose.  In Windows 3.1, you're quite safe to keep blocks
  40. of locked memory around - it doesn't adversely affect Windows' ability
  41. to move memory at all.
  42.  
  43. All you have to remember is to declare your pointers FAR if the item
  44. pointed to is <64k in size, and HUGE if >=64k.  Use FAR pointers unless
  45. you can't avoid it - there are severe computational penalties associated
  46. with the use of HUGE pointers.  One other thing - if you have a pointer to
  47. a HUGE array of items, and the array is >128k in size, each of the items
  48. in the array MUST be exactly a power-of-two number of bytes in size. ie,
  49. 16, 32, 64, etc bytes.  Unless you do this, you'll run into horrible
  50. segment wrap-around problems.
  51.  
  52. For example, you can simply say:
  53.  
  54. #include <windows.h>
  55. #include <windowsx.h>
  56.  
  57. double HUGE *x;             // x is a HUGE pointer to array of doubles
  58.  
  59. // Allocate an array of 50,000 zero-initialized doubles:
  60.  
  61. x = (double HUGE *) GlobalAllocPtr( 50000*sizeof(double),
  62.                                     GMEM_MOVEABLE | GMEM_ZEROINIT );
  63.  
  64. ....
  65.  
  66. // Free the memory again.
  67.  
  68. GlobalFreePtr(x);
  69.  
  70. >As a programmer, it doesn't seem like I should have to deal with the
  71. >quirks of the processor I'm running on...
  72. >
  73. >My understanding of the 386/486 is that it has better support for virtual
  74. >memory management and that its virtual 32-bit pointers look like contiguous,
  75. >unmoving addressable memory.  Is there a way to do this in Borland C++ 3.1?
  76.  
  77. What you probably need is "Win32s" - the 32-bit Windows Win32 subset for
  78. Enhanced-mode Windows 3.1.  This lets you run 32-bit applications, each of
  79. which has its own "flat 32-bit" 4GB address space. It's basically a subset
  80. of the Windows NT API that will run on Windows 3.1.  Unfortunately, you
  81. need the 32-bit software development tools supplied with Windows NT in order
  82. to develop Win32s applications, although a free (!) product called
  83. "QuickStart", given away by Phar Lap Software allows these tools to be
  84. run from DOS or Windows 3.1.
  85.  
  86. >Judging from some of the other posts about memory management, it seems that
  87. >a lot of people might benefit from discussing some of the stuff I've
  88. >brought up.  If this is FAQ material, I'm sorry for clogging network
  89. >bandwith with this message.
  90. >
  91.  
  92. I think you've raised some interesting points here, which are very worthy
  93. of discussion.
  94.  
  95. >Drew
  96. >
  97.  
  98. Chris.
  99. -- 
  100. --------------------------------------------------------------------------
  101. | Chris Marriott                           | chris@chrism.demon.co.uk    |
  102. | Warrington, UK                           | BIX: cmarriott              |
  103. | (Still awaiting inspiration              | CIX: cmarriott              |
  104. |  for a witty .sig .... )                 | CompuServe: 100113,1140     |
  105. --------------------------------------------------------------------------
  106.  
  107.