Example #1
0
/* ___mkd_reparse() a line, returning it in malloc()ed memory
 */
int
mkd_line(char *bfr, int size, char **res, DWORD flags)
{
    MMIOT f;
    int len;
    
    mkd_parse_line(bfr, size, &f, flags);

    if ( (len = S(f.out)) ) {
	/* kludge alert;  we know that T(f.out) is malloced memory,
	 * so we can just steal it away.   This is awful -- there
	 * should be an opaque method that transparently moves 
	 * the pointer out of the embedded Cstring.
	 */
	EXPAND(f.out) = 0;
	*res = T(f.out);
	T(f.out) = 0;
	S(f.out) = ALLOCATED(f.out) = 0;
    }
    else {
	 *res = 0;
	 len = EOF;
     }
    ___mkd_freemmiot(&f, 0);
    return len;
}
Example #2
0
/* printf() into a cstring
 */
int
Csprintf(Cstring *iot, char *fmt, ...)
{
    va_list ptr;
    int siz=100;

    do {
	RESERVE(*iot, siz);
	va_start(ptr, fmt);
	siz = vsnprintf(T(*iot)+S(*iot), ALLOCATED(*iot)-S(*iot), fmt, ptr);
	va_end(ptr);
    } while ( siz > (ALLOCATED(*iot)-S(*iot)) );

    S(*iot) += siz;
    return siz;
}
Example #3
0
// str copy
void
word_set_str_copy(ForthWord *word, const char *str)
{
    if (str == NULL) return;
    size_t len = strlen(str);

    // TODO use realloc()
    word->strval.str = malloc(len + 1);
    if (! ALLOCATED(word->strval.str)) {
        FREE(word->strval.str);    // for safety
        return;
    }

    strcpy(word->strval.str, str);

    word->strval.capacity = len + 1;
    word->strval.len = len;
}
Example #4
0
int main(int argc, char *argv[])
{
  int i, maxMem=0;
  void *start, *end;
  char *progname;

  if (argc > 0)
    progname = argv[0];
  else
    progname = "";
    
  MESSAGE("-- This test checks malloc(), free() and realloc()\n");
  srand((unsigned int)time(NULL));

#ifdef CUSTOM_MALLOC
  MESSAGE("Using custom malloc\n");
#else
  MESSAGE("Using system malloc\n");
#endif

#ifdef MMAP
  start = endHeap();
#else
  start = (void *)sbrk(0);
#endif

  for(i=0;i<MAXPOSTS;i++)
    {
      memPosts[i].size = rand()%(MAXSIZE/2);
      startMeasure();
      memPosts[i].ptr = (double*) malloc(memPosts[i].size*sizeof(double));
      stopMeasure();
      if ( memPosts[i].size == 0 &&  memPosts[i].ptr!= NULL )
/*	MESSAGE("* ERROR: malloc doesn't return NULL pointer on zero size\n");*/
		  ;
      else if( memPosts[i].size && memPosts[i].ptr == NULL ) {
	MESSAGE("* ERROR: malloc returned NULL on non-zero size request\n");
	  }
      else if( memPosts[i].ptr != NULL )
	memPosts[i].ptr[0]  = (double)3.14;
  }
  
  calcMemUsage(&maxMem);
  
  for(i=0;i<MAXITERS;i++)
    {
      int index;
      index = rand()%MAXPOSTS;
 
      if(ALLOCATED(index))
	{ 
	if(rand()%5 < 3)
	  {
	    if(memPosts[index].ptr[0] != (double)3.14)
	      MESSAGE("* ERROR: Corrupt memory handling\n");
	    memPosts[index].size = rand()%MAXSIZE;
	    startMeasure();
	    memPosts[index].ptr = 
	      (double*) realloc(memPosts[index].ptr,
				memPosts[index].size*sizeof(double));
				stopMeasure(); 
	    if(memPosts[index].size && memPosts[index].ptr[0] != (double)3.14)
	      MESSAGE("* ERROR: Corrupt memory handling\n");
	    if(memPosts[index].size) memPosts[index].ptr[0] = (double)3.14;
	  }
        else
	  {
	    if(memPosts[index].ptr[0] != (double)3.14)
	      MESSAGE("* ERROR: Corrupt memory handling\n");
	      startMeasure();
            free(memPosts[index].ptr);
            stopMeasure();
            memPosts[index].size = 0;
	  }
	}
      else 
	{
	  memPosts[index].size = rand()%MAXSIZE;
	  startMeasure();
	  memPosts[index].ptr = (double*) malloc(memPosts[index].size*sizeof(double)); 
	  stopMeasure();
	  if(memPosts[index].size) memPosts[index].ptr[0] = (double)3.14;
	}
      calcMemUsage(&maxMem);
    }
    
#ifdef MMAP
  end = endHeap();
#else
  end = (void *) sbrk(0);
#endif

  fprintf(stderr,
	  "%s: Max memory allocated %d\n%s: Memory consumed %ld\n%s: Time in free/malloc/realloc: %f\n",
	  progname,
          maxMem,
	  progname,
	  (unsigned long)(end-start),
	  progname, getMeasuredTime());
  return 0;
}
Example #5
0
int main(int argc, char *argv[]) {
	int i, maxMem=0;
	void *start, *end;
	struct timeval starttv;
	struct timeval endtv;

	if (argc > 1) {
		printf("Using seed: %s", argv[1]);
		srand(atoi(argv[1]));
	}
	else {
		srand((unsigned int)time(NULL));	
	}

	start = (void *)sbrk(0);
	
	gettimeofday(&starttv, NULL); 
	
	/*Alloc MAXPOSTS blocks of memory*/
	for(i=0;i<MAXPOSTS;i++) {
		memPosts[i].size = rand()%(MAXSIZE/2);
#ifndef NO
		memPosts[i].ptr = (double*) malloc(memPosts[i].size*sizeof(double));
#endif
	}
	
	gettimeofday(&endtv, NULL); 
	
	end = (void *) sbrk(0);
	
	printf("Time for malloc: %f\n", ((float)(endtv.tv_sec-starttv.tv_sec) + (float)(endtv.tv_usec-starttv.tv_usec)/1000000));
	calcMemUsage(&maxMem);
	printf("Maxmem after malloc: %d\n", maxMem);
	printf("Memory consumed after malloc %ld\n\n", (unsigned long)(end-start));

	gettimeofday(&starttv, NULL);
	
	/*Realloc MAXITERS blocks of memory*/
	for(i=0;i<MAXITERS;i++) {
		int index;
		index = rand()%MAXPOSTS;

		if(ALLOCATED(index)) { 
			if(rand()%5 < 3) {
				memPosts[index].size = rand()%MAXSIZE;
#ifndef NO
				memPosts[index].ptr =  (double*) realloc(memPosts[index].ptr, memPosts[index].size*sizeof(double)); 
#endif
			}
		}
	}

	gettimeofday(&endtv, NULL); 
	
	end = (void *) sbrk(0);
	
	printf("Time for realloc: %f\n", ((float)(endtv.tv_sec-starttv.tv_sec) + (float)(endtv.tv_usec-starttv.tv_usec)/1000000));
	calcMemUsage(&maxMem);
	printf("Maxmem after realloc: %d\n", maxMem);
	printf("Memory consumed after realloc %ld\n\n", (unsigned long)(end-start));

	gettimeofday(&starttv, NULL);
	
	/*Free all blocks*/
	for(i=0;i<MAXPOSTS;i++) {
#ifndef NO
		memPosts[i].size = 0;
		free(memPosts[i].ptr);
#endif
	}

	gettimeofday(&endtv, NULL); 
	printf("Time for free: %f\n", ((float)(endtv.tv_sec-starttv.tv_sec) + (float)(endtv.tv_usec-starttv.tv_usec)/1000000));

	return 0;
}
Example #6
0
int main(int argc, char *argv[])
{
  int i, maxMem=0;
  char *start, *end;
  char *progname;

  if (argc > 0)
    progname = argv[0];
  else
    progname = "";
  MESSAGE("-- This test checks malloc(), free() and realloc()\n");
  srand((unsigned int)time(NULL));
  start = (char *)sbrk(0);

  for(i=0;i<MAXPOSTS;i++)
    {
      memPosts[i].size = rand()%(MAXSIZE/2);
      memPosts[i].ptr = (double*) malloc(memPosts[i].size*sizeof(double));
      if ( memPosts[i].size == 0 &&  memPosts[i].ptr!= NULL )
/*	MESSAGE("* ERROR: malloc doesn't return NULL pointer on zero size\n");*/
		  ;
      else if( memPosts[i].size && memPosts[i].ptr == NULL ) {
	MESSAGE("* ERROR: malloc returned NULL on non-zero size request\n");
	  }
      else if( memPosts[i].ptr != NULL )
	memPosts[i].ptr[0]  = 3.14;
  }
  
  calcMemUsage(&maxMem);
  
  for(i=0;i<MAXITERS;i++)
    {
      int index;
      index = rand()%MAXPOSTS;
 
      if(ALLOCATED(index))
	{ 
	if(rand()%5 < 3)
	  {
	    if(memPosts[index].ptr[0] != 3.14)
	      MESSAGE("* ERROR: Corrupt memory handling\n");
	    memPosts[index].size = rand()%MAXSIZE;
	    memPosts[index].ptr = 
	      (double*) realloc(memPosts[index].ptr,
				memPosts[index].size*sizeof(double)); 
	    if(memPosts[index].size && memPosts[index].ptr[0] != 3.14)
	      MESSAGE("* ERROR: Corrupt memory handling\n");
	    if(memPosts[index].size) memPosts[index].ptr[0] = 3.14;
	  }
        else
	  {
	    if(memPosts[index].ptr[0] != 3.14)
	      MESSAGE("* ERROR: Corrupt memory handling\n");
            free(memPosts[index].ptr);
            memPosts[index].size = 0;
	  }
	}
      else 
	{
	  memPosts[index].size = rand()%MAXSIZE;
	  memPosts[index].ptr = (double*) malloc(memPosts[index].size*sizeof(double)); 
	  if(memPosts[index].size) memPosts[index].ptr[0] = 3.14;
	}
      calcMemUsage(&maxMem);
    }
  end = (char*) sbrk(0);
  fprintf(stderr,
	  "%s: Max memory allocated %d\n%s: Memory consumed %ld\n",
	  progname,
          maxMem,
	  progname,
	  (unsigned long)(end-start));
  return 0;
}