delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/21/00:50:20

From: fesenko AT pacific DOT net DOT sg (Victor)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: [Q] wrapping functions with linker
Date: Thu, 21 Aug 1997 16:55:54 GMT
Organization: Subscriber, Pacific Internet, Singapore
Lines: 156
Message-ID: <5tg75j$sj3$1@newton.pacific.net.sg>
References: <5stvfi$ffm$1 AT newton DOT pacific DOT net DOT sg> <33F43A43 DOT 6E6EF6B2 AT Mathematik DOT TU-Chemnitz DOT DE> <33f54e90 DOT 1752245 AT news DOT pacific DOT net DOT sg> <33F81374 DOT 7D6987A7 AT Mathematik DOT TU-Chemnitz DOT DE>
NNTP-Posting-Host: dyn104ppp205.pacific.net.sg
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Robert Hoehne <Robert DOT Hoehne AT Mathematik DOT TU-Chemnitz DOT DE> wrote:


>If all of this does not help you (or you don't understand),
>please post a short example of code with an as detailed as
>possible description what you want to get and I will try if 
>I can get you more specific help.

>Robert

I am trying to implement malloc() related functions using global array
__imd[].  I understand that this is not a correct malloc()
implementation, because it usually has to use sbrk() to allocate
memory. But this code is tested and workable. Only when I include it
to replace ALL calls to malloc(), including libc, then my program
doesn't even start properly and I'm getting this message without even
a traceback info.

General Protection Fault at eip=1af61; flags=3056
eax=00000025 ebx=0000000e ecx=fffe6ad1 edx=0001cc85 esi=0001cc2b
edi=00000123	ebp=00000123 esp=00003006 cs=a7 ds=af es=af fs=8f
gs=bf ss=33 error=0000

  I understand that my code could cause severe problems in some
startup routines, but I'd like to know if there is a way to fix them.

The reason I'm using such a weird malloc() replacement is that I'm
doing a program which could be running for a long time (probably days)
and when I check available DPMI memory, it's going down all the time.
I understand that after calling free() memory is added to a free
memory pool, but the DPMI memory is going low at an alarming rate and
the memory in the pool gets fragmented too (and I don't have any
control over the memory in the pool).

The code follows (I think it's quite easy to understand).
It works like this :
first all the static allocations using malloc() are done (that are not
going to be changed later). Then mem_ptr_fix() is called and the
pointer is fixed at some value. Then the malloc() is going to be used
only within the function scope, so that mem_clean() would be called
before function exits and the pointer would be restored to it's fixed
position. It looks like this:
void some_function(void)
{              int *ptr1, *ptr2, *ptr3;
  
  ptr1=malloc(2000);
  ptr2=malloc(20000);
  ptr3=malloc(35000);
  ......
  free(ptr1);
  free(ptr2);
  free(ptr3);

  mem_clean();
}

So the whole thing looks like manual memory control :-)

Any hints appreciated.
Victor Fesenko.




/************** START OF THE CODE **************/
#define MMALLOC_SIZE 5000000
#define WRITE_TO_LOGFILE 1
char __imd[MMALLOC_SIZE];

size_t ___im__ptr,___im__ptr_t=0;
FILE *fi;

#define mprintf(format,args...) {fi=fopen("malloc.log","a");
fprintf(fi,format,## args); fclose(fi);}


void  mem_clean();
void  mem_ptr_fix();
void* malloc(size_t size);
void  free(void *ptr);
void* realloc(void *ptr,size_t size);


void mem_clean()
{ ___im__ptr=___im__ptr_t;
  if(WRITE_TO_LOGFILE)mprintf(" mem_clean() called.\n");
}

void mem_ptr_fix()
{ ___im__ptr_t=___im__ptr; 
  if(WRITE_TO_LOGFILE)mprintf(" mem_ptr_fix() called, fixed at %li\n",
                                ___im__ptr);
}

void* malloc(size_t size)
{      void *p=(void*)(__imd+___im__ptr);
    if(WRITE_TO_LOGFILE)
      mprintf(" Program requested %8li bytes. Ptr =
%li\n",size,___im__ptr);
      ___im__ptr+=size;
      if(___im__ptr>MMALLOC_SIZE)
      { mprintf("-----> Program error. Ptr = %li\n",___im__ptr);
        p = NULL;
      }
 return p;
}

void free(void *ptr)
{ if(WRITE_TO_LOGFILE)
     mprintf(" **** Program called free(). Ptr = %li\n",
             (long int)(ptr-(void*)__imd));
}

void* realloc(void *ptr,size_t size)
{     void *p=(void*)(__imd+___im__ptr); char *pt, *pti;
  if(WRITE_TO_LOGFILE){
     if(!ptr)mprintf("  Program requested %8li bytes for realloc().\n
Old ptr - NULL  New ptr = %8li\n",size,___im__ptr)
     else mprintf("  Program requested %8li bytes for realloc().\n
Old ptr - %8li New ptr = %8li\n",
                        size,(long int)(ptr-(void*)__imd),___im__ptr);
                      }  
     if(___im__ptr+size>MMALLOC_SIZE){ mprintf("-----> Program error.
Ptr = %li\n",___im__ptr);
                                       return NULL;
                                     }
     if(ptr)for(pt=ptr,pti=__imd+___im__ptr; pt<(char*)(ptr+size); 
                pt++,pti++) *pti= *pt;
     ___im__ptr+=size;
     return p;
}

void* calloc(size_t n_elements,size_t size)
{       void *p=(void*)(__imd+___im__ptr);
        char *ptr, *bptr;
    size*=n_elements;
    if(WRITE_TO_LOGFILE)
      mprintf(" Program requested %8li bytes for calloc(). Ptr =
%li\n",size,___im__ptr);
      ___im__ptr+=size;
      if(___im__ptr>MMALLOC_SIZE)
      { mprintf("-----> Program error. Ptr = %li\n",___im__ptr);
        return NULL;
      }
      for(ptr=p,bptr=p+size; ptr<bptr; ptr++)*ptr = 0; /* !!!!! */
 return p;
}

void cfree(void *ptr)
{ if(WRITE_TO_LOGFILE)
     mprintf(" **** Program called cfree(). Ptr = %li\n",
             (long int)(ptr-(void*)__imd));
}



- Raw text -


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