TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: All
from: jari laaksonen
date: 1995-01-15 21:13:10
subject: DosSetMem

I have seen only few "real world" examples of DosSetMem(). Toolkit samples
have some, and in over 3 megs of text files which I have saved from
programming echos there is only one mention of this function.

So, I have to ask you gurus, if the following example is the
"right" use of DosSetMem at all:

#define  INCL_NOPMAPI
#define  INCL_DOSMEMMGR   /* Memory Manager values */
#include 
#include 
#include 
#include 

typedef struct _data {
  char        text [170];
  long        offset;
  ULONG       fsize;
  USHORT      lboxID;
} DATA;

int main (int argc, char **argv)
{
  APIRET  rc;
  short   n = 0, i, l;
  ULONG   ulSize, ulPages;
  DATA   *pData;

  if (argc != 2)
    return 1;

  l = atoi (argv[1]);
  printf ("sizeof (DATA) = %d\n", sizeof (DATA));

  /* Allocate 2MB object  */
  rc = DosAllocMem ((PVOID *) &pData, 2097152, PAG_READ | PAG_WRITE);
  if (0 != rc)
  {
    printf ("DosAllocMem returns: %lu\n", rc);
    return 1;
  }

  /* Commit memory for 4 pages */
  ulPages = 4;
  rc = DosSetMem (pData, ulPages * 0x1000, PAG_COMMIT | PAG_READ | PAG_WRITE);
  if (0 != rc)
  {
    DosFreeMem (pData);
    printf ("DosSetMem returns: %lu\n", rc);
    return 1;
  }

  for (i = 0; i < l; i++)
  {
    ulSize = (n + 1) * sizeof (DATA);

    // just fill some fields with meaningless values
    strcpy (pData[n].text, "abcdefghijklmn");
    pData[n].fsize  = ulSize;
    pData[n].lboxID = n;

    n++;
    // commit more memory just before page fault is possible
    if ( ( (n + 1) * sizeof (DATA) ) > ulPages * 0x1000 )
    {
      VOID   *pNext;

      // calculate next address that is on page boundary
      pNext = (PBYTE)pData + (((n + 1) * sizeof(DATA)) & (ulPages * 0x1000));
      ulPages += 4;                // 4 more pages
      rc = DosSetMem (pNext,
        0x4000,                    // commit 4 pages of memory
        PAG_COMMIT | PAG_READ | PAG_WRITE
      );
      if (0 != rc)
      {
        printf ("in loop %d DosSetMem returns: %lu\n", i, rc);
        break;
      }
      printf ("i: %05d, pNext: %p, committed: 0x%08X, data: 0x%08X\n",
        i, pNext, ulPages * 0x1000, ulSize );
    }
  }

  for (i = 0; i < n; i++)
  {
    printf ("text: %s, fsize: %lu, id: %d, offs: %ld\n",
      pData[i].text, pData[i].fsize, pData[i].lboxID, pData[i].offset );
  }
  DosFreeMem (pData);

  return 0;
}

        // Albert  (Team OS/2)             email: albert{at}sci.fi
                                                  jla{at}to.icl.fi (work)

---
* Origin: Albert's Point/2 in Finland, Europe (2:221/360.20)
SEEN-BY: 12/2442 620/243 624/50 632/348 640/820 690/660 711/409 410 413 430
SEEN-BY: 711/807 808 809 934 942 949 712/353 515 713/888 800/1 7877/2809
@PATH: 221/360 0 220/841 201/2104 109/347 2 7 3615/50 229/2 12/2442 711/409
@PATH: 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.