Exemplo n.º 1
0
void *ReallocMemory (void *old, long size)
{
#if 0
	void *ptr;

	if (size < 1)
		size = 1;
	ptr = realloc(old,size);
	if (NULL == ptr)
		ProgError ("Out of memory! (cannot reallocate %lu bytes)", size);
	return ptr;
#else
	void *ret;
	// DEBUG
	// ULONG asksize = size;

	/* limit fragmentation on large blocks */
	if (size >= (ULONG) SIZE_THRESHOLD)
		size = (size + (ULONG) SIZE_OF_BLOCK) & ~((ULONG) SIZE_OF_BLOCK);

	// TRACE ("Realloc: ask size = " << dec << asksize << ", real size = " << size << " bytes");

#ifdef __WIN32__
	ret = realloc (old, size);
#else
	ret = farrealloc (old, size);
#endif

	if (ret == NULL)
		ProgError ("Out of memory! (cannot reallocate %lu bytes)", size);

	return ret;
#endif
}
Exemplo n.º 2
0
/*
** Reallocate memory
*/
void  *Realloc (void  *old, long size)
{  void  *ret;

   if(size<1)
   {  Warning("Attempt to allocate %ld bytes",size);
      size=1;
   }
#if DT_OS == 'd' && DT_CC == 'b'
   ret = farrealloc( old, size);
#else
   if ((size_t) size != size)
      ProgError ("Tried to realloc %ld b but couldn't."
        " Use another compiler.", size);
   ret = realloc( old, (size_t)size);
#endif
   if (ret==NULL)
      ProgError( "Out of memory (Needed %ld bytes)", size);
   return ret;
}
Exemplo n.º 3
0
void main() {  		     /*  Se debe correr en model = large */
	struct Y far *q;
	struct Z far *p;

	clrscr();
	randomize();
	q = LocFar;
	if (q == NULL) {
		PR("No existe memoria dinamica lejana..");
		getch();
		exit(1);
	}
	printf ("Valor de q = %p\n",q);
	q->d = exp(1);
	gcvt (q->d,6,q->a);
	p = (struct Z far *) farrealloc (q, LONZ);
	printf ("Valor de p = %p\n",p);
	p->f = asin (1) * 2.0;
	p->i = rand();
	PR ("Valores de p-> (p contenido)");
	printf ("%lf %s %f %d\n",p->d,p->a,p->f,p->i);
	getch();
}
Exemplo n.º 4
0
int                  main(int argc, char **argv)
{
   int                  CheckDuplicateDescriptions;
   int                  SortBy;
   int                  SkipTilde;
   int                  SkipMovedto;
   char                *arg;
   int                  c;
   int                  i;
   int                  j;
   FILE                *fp;
   struct ffblk         ffb;
   int                  done;
   int                  Len;
   char               **Lines;
   int                  maxLines;
   int                  nLines;
   int                  pathlen;
   char                 Line[200];
   char                 Dirname[MAX_PATH];
   char                 Filename[MAX_PATH];
   char                 OutFilename[MAX_PATH];
   char                *s;
   char                *Description;
   char                *FormattedLine;
   unsigned long        farcoreleftStart;
   unsigned long        farcoreleftEnd;
   long                 FileSize;
   struct stat		statbuf;
   int 			verbose;
   int 			ragged;
   int 			terminalsized;

   printf("%s\n", ProgVer);
   printf("Replacement for James Jessiman's makelist\n");
   printf("Call with -h to see a list of options.\n\n");

   strcpy(Dirname, "PARTS"); /* Default input directory path */
   strcpy(OutFilename, "parts.lst"); /* Default output filename */

   verbose = 0;
   ragged = 1; /* Steve wants it ragged by default */
   terminalsized = 1; /* Steve wants it to fit in 80 chars by default */

   CheckDuplicateDescriptions = 0;
   SortBy = 0;
   SkipTilde = 0;
   SkipMovedto = 1;
   while (--argc > 0)
   {
      arg = *++argv;
      if (arg[0] == '-')
      {
         switch (arg[1])
         {
            case '?':
            case 'h':
               PrintUsage();
               exit(1);
               break;
            case '8':
               namelen = 12;
               break;
            case 'c':
               CheckDuplicateDescriptions = 1;
               break;
            case 'n':
            case 'd':
               SortBy = arg[1];
               break;
            case 'm':
               SkipMovedto = 0;
               break;
            case '~':
               SkipTilde = 1;
               break;
            case 'i':
	       if (--argc > 0)
		 strcpy(Dirname, *++argv);
	       else
	       {
		 PrintUsage();
		 printf("*** input directory expected as next argument after -i.\n");
		 exit(1);
	       }
               break;
            case 'o':
	       if (--argc > 0)
		 strcpy(OutFilename, *++argv);
	       else
	       {
		 PrintUsage();
		 printf("*** output filename expected as next argument after -o.\n");
		 exit(1);
	       }
               break;
            case 'q':
               quiet = 1;
            case 'f':
               forceit = 1;
               break;
            case 'r':
               ragged = ragged ^ 1;
               break;
            case 't':
               terminalsized = terminalsized ^ 1;
               break;
            case 'v':
               verbose = 1;
               break;
            default:
               PrintUsage();
               printf("*** Unknown option '%s'.\n", arg);
               exit(1);
               break;
         }
      }
      else
      {
         PrintUsage();
         exit(1);
      }
   }

   /* Do a stat to see if Dirname exists and is a directory. */
   if (stat(Dirname, &statbuf) < 0)
   {
     printf("*** Could not stat input directory \"%s\".\n", Dirname);
     exit(1);
   }

   if ((statbuf.st_mode & S_IFDIR) == 0) 
   {
     printf("*** Input directory \"%s\" is not a directory.\n", Dirname);
     exit(1);
   }

   if (CheckDuplicateDescriptions)
      SortBy = 'd';
   if (!SortBy)
   {
      if (forceit)
        SortBy = 'd';
      if (!quiet)
         printf("Sorting by [D]escription.\n");
   }

   if (!SortBy)
   {
      printf("Sort by [N]umber or [D]escription: ");
      c = getch();
      printf("%c\n", c);

      if (c == 'N' || c == 'n')
         SortBy = 'n';
      else if (c == 'D' || c == 'd')
         SortBy = 'd';
      else
      {
         printf("Nothing done.\n");
         exit(0);
      }
   }

   farcoreleftStart = farcoreleft();

   nLines = 0;
   maxLines = numlines;
   Lines = farmalloc(maxLines * sizeof(char *));
   if (!Lines)
   {
      printf("Out of memory after %d parts\n", nLines);
      printf("Memory available at beginning: %ld kBytes\n",
             (farcoreleftStart + 1023) / 1024);
      exit(1);
   }
   strcpy(Filename, Dirname);
   strcat(Filename, "\\");
   pathlen = strlen(Filename);
   strcat(Filename, "*.*");
   for (done = findfirst(Filename, &ffb, 0); !done; done = findnext(&ffb))
   {
      if (verbose)
      {
         printf("Processing file: \"%s\"\n", ffb.ff_name);
      }
      strcpy(Filename + pathlen, ffb.ff_name);
      fp = fopen(Filename, "rt");
      if (!fp)
      {
         if (!quiet) printf("Cannot open \"%s\"", ffb.ff_name);
         PressAnyKey();
         continue;
      }
      fgets(Line, sizeof(Line), fp);
      fclose(fp);

      s = Line + strlen(Line) - 1;
      while (s >= Line && (*s == '\n' || *s == '\r' || *s == '\t' || *s == ' '))
         *s-- = '\0';                     /* clear newline and trailing tabs
                                             and spaces                      */
      s = Line;
      while (*s == '\t' || *s == ' ')
         *s++;
      if (*s++ != '0')
      {
         if (!quiet)          printf("Line type 0 expected in \"%s\", skipping...", ffb.ff_name);
         PressAnyKey();
         continue;
      }
      while (*s == '\t' || *s == ' ')
         *s++;
      Description = s;
      if (SkipTilde && Description[0] == '~')
         continue;
      if (SkipMovedto && strncmp(Description, "~Moved to", 9) == 0)
         continue;
      Len = strlen(Description);
      if (Len == 0)
      {
         if (!quiet)          printf("Empty description in \"%s\"", ffb.ff_name);
         PressAnyKey();
      }
      if (Len > 64)
      {
         /* Original makelist truncates to 64 characters. */
         if (!quiet) 
         {
            printf("Description in \"%s\" will be truncated to 64 characters:\n",
                   ffb.ff_name);
            printf("Before: \"%s\"\n", Description);
            printf("After:  \"%-64.64s\"\n", Description);
         }
         PressAnyKey();
      }
      if (namelen == 12)
         FormattedLine = farmalloc(79);
      else
         FormattedLine = farmalloc(128);
      if (!FormattedLine)
      {
         printf("Out of memory after %d parts\n", nLines);
         printf("Memory available at beginning: %ld kBytes\n",
                (farcoreleftStart + 1023) / 1024);
         exit(1);
      }


      if (namelen > 12) 
         strcpy(shortfilename, ffb.ff_name);
      else
      {
         GetShortPathName(Filename, shortfilepath, MAX_PATH);
         s = basename(shortfilepath);
         strcpy(shortfilename, s);    
         if (s != NULL)
            free(s);
         if (strcmp(ffb.ff_name, shortfilename))
         {
            if (!quiet) 
               printf("Filename \"%s\" will be shortened to %s\n", 
                      ffb.ff_name, shortfilename);
            PressAnyKey();
         }
      }

      Len = strlen(shortfilename); 
      if (Len > namelen)
      {
         if (!quiet)          
            printf("Filename \"%s\" will be truncated to %d characters.\n",
                   shortfilename, namelen);
         PressAnyKey();
      }
      shortfilename[namelen] = 0;
      if (namelen == 12)
         sprintf(FormattedLine, "%-12s  %-64.64s", shortfilename, Description);
      else if (ragged && terminalsized)
      {
         if (Len > 14) /* Squeeze every last char out of the 80. */
            sprintf(FormattedLine, "%s %-64.64s", shortfilename, Description);
         else
            sprintf(FormattedLine, "%-14s  %-64.64s", shortfilename, Description);
      }
      else if (ragged)
      {
         if (Len > 12)
            sprintf(FormattedLine, "%s %-64.64s", shortfilename, Description);
         else
            sprintf(FormattedLine, "%-12s  %-64.64s", shortfilename, Description);
      }
      else
         sprintf(FormattedLine, "%-25s  %-64.64s", shortfilename, Description);
    
      if (terminalsized)
      {
         if (namelen == 12)
            FormattedLine[78] = 0;
         else
            FormattedLine[80] = 0;
      }

      if (verbose)
      {
         printf("%d:\t%s\n", nLines, FormattedLine);
      }
      if (nLines >= maxLines)
      {
         /* Let's have another 1000 pointers */
         maxLines += numlines;
         Lines = farrealloc(Lines, maxLines * sizeof(char *));
         if (!Lines)
         {
            printf("Out of memory after %d parts\n", nLines);
            printf("Memory available at beginning: %ld kBytes\n",
                   (farcoreleftStart + 1023) / 1024);
            exit(1);
         }
      }
      Lines[nLines++] = FormattedLine;
      if (nLines % 100 == 0)
         printf("%d parts so far...\r", nLines);
   }
   printf("%d parts found in %s.\n", nLines, Dirname);
   if (nLines == 0)
   {
      printf("No parts found, nothing done.\n");
      exit(0);
   }

   printf("Sorting...\n");
   qsort(Lines, nLines, sizeof(Lines[0]),
         (SortBy == 'n') ? CmpNumber : CmpDescription);

   if (CheckDuplicateDescriptions)
   {
      printf("Checking for duplicate descriptions. \"%s\" unchanged.\n", OutFilename);
      for (i = 0; i < nLines; i += j)
      {
         for (j = 1; i + j < nLines; j++)
         {
            if (stricmp(Lines[i] + (namelen+2), Lines[i + j] + (namelen+2)) != 0)
               break;                     /* OK to break, lines are sorted   */
            if (j == 1)                   /* First duplicate                 */
               printf("%s\n", Lines[i]);
            printf("%s\n", Lines[i + j]);
         }
         if (j > 1)                       /* Duplicates found                */
            PressAnyKey();
      }
   }
   else
   {
      fp = fopen(OutFilename, "wt");
      if (!fp)
      {
         printf("Cannot open \"%s\" for writing.\n", OutFilename);
         exit(1);
      }
      for (i = 0; i < nLines; i++)
         fprintf(fp, "%s\n", Lines[i]);
      FileSize = ftell(fp);
      fclose(fp);
      printf("\"%s\" successfully written, %ld kBytes\n", OutFilename,
             (FileSize + 1023) / 1024);
   }

   if (numlines > 1000) /* if not Borland DOS compiler then skip the mem msg. */
   {
       return (0);
   }

   farcoreleftEnd = farcoreleft();

   printf("Maximum memory usage: %ld kBytes of %ld kBytes available\n",
          (farcoreleftStart - farcoreleftEnd + 1023) / 1024,
          (farcoreleftStart + 1023) / 1024);

   return (0);
}
Exemplo n.º 5
0
void far * EXPENTRY sq_farrepalloc(void far *ptr, size_t size)
{
  return ((void far *)farrealloc(ptr, size));
}