delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/10/09/15:53:14

Message-ID: <361E6945.5FCA5FAC@tecnotron.com.br>
Date: Fri, 09 Oct 1998 16:51:33 -0300
From: Charles Ricardo Staub <charles AT tecnotron DOT com DOT br>
X-Mailer: Mozilla 4.05 [en] (Win95; I)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Memory allocation
Reply-To: djgpp AT delorie DOT com

/* Please, excuse-me but I need answers to be sent
to heckler AT mat DOT ufrgs DOT br    My computer is now
broken, so this is a borrowed one ;)  */

Hi there!  the following program allocate lots of memory
and free it.  Then, try to allocate again, but with a diferent
block size.  It dosn't work ! -> first part runs OK, but the
second gets a "virtual memory exceed" error !

 May be I'm a complete stupid and this code is absolutaly
wrong, but also may some of you guys have a tip about it.  So ?

 Oh, please test it (if you want, of course!) using CWSDPR0 !

---- included test.cc ----
#include <stdio.h>
#include <dpmi.h>
#include <crt0.h>

int _crt0_startup_flags = _CRT0_FLAG_LOCK_MEMORY;

void main (void)
{
   // discover the amount of free memory:
   _go32_dpmi_meminfo info;
   _go32_dpmi_get_free_memory_information(&info);
   unsigned long int FreeMem = info.available_memory;
   printf("avaliable memory = %d\n",FreeMem);

   // Let's reserve 10MB for safety:
   FreeMem -= 10*1024*1024;
   printf("using %d bytes\n",FreeMem);

   printf("starting alocation...\n\n");
   int Fraction;
   char *ptr[100];

   // Now, allocate some pieces:
   Fraction = 10;
   for (int i=0; i<Fraction; i++) {
      ptr[i] = new char[FreeMem/Fraction];
      fprintf(stderr,"%d ",i);
   }
   printf("end of first alocation\n");

   // dealocate:
   for (int i=0; i<Fraction; i++) {
      delete ptr[i];
      fprintf(stderr,"%d ",i);
   }
   printf("end of first dealocation\n");

   // and now realocate, with diferent size:
   Fraction = 8;
   for (int i=0; i<Fraction; i++) {
      ptr[i] = new char[FreeMem/Fraction];
      fprintf(stderr,"%d ",i);
   }
   printf("end of second alocation\n");

   // dealocate:
   for (int i=0; i<Fraction; i++) {
      delete ptr[i];
      fprintf(stderr,"%d ",i);
   }
   printf("end of second dealocation\n");
}
---- end test.cc ----


More: well, if you readed utill here, you deserve to know
more about what I'm trying to do.

I need to allocate lots of memory, in blocks with diferent sizes,
and at diferent times.  under some situations, the operations of
"new" and "delete" cause the DPMI server to block the interrupts
for a long time (more than 5ms), and it create problems for a
hardware I drive on the program.

Bight idea :) -> why don't just allocate the largest amount
of memory I can in the initialization, then free it ?  As the
FAQ says, this memory should be allocated to the local heap of
the problem, and from this point on, no call to DMPI will be
made for memory requests ...

Cloud: it seems for me that I need to reuse the previously
allocated blocks in the same block-size I did first, otherwise
I will receive a "virtual memory exceed in new" ...

Ideas ?

Any idea of good on-line resources for memory allocation
under djgpp/dpmi ?

ps.: I really don't want to "reinvent the weel", i.e., made
my own memory managing.


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019