home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / vbcc / machines / amigappc / libsrc / stdlib / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-24  |  1.0 KB  |  51 lines

  1. /*
  2. ** vbcc-Amiga-PowerPC version of newmalloc.c
  3. ** utilizes the Amiga pooled memory functions
  4. **
  5. ** v0.1 04.10.97 phx
  6. */
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <powerup/gcclib/powerup_protos.h>
  11.  
  12.  
  13. size_t _nalloc=16384;
  14. void *_first_mlist=0;
  15.  
  16. void _freemem(void)
  17. /*  Gibt allen Speicher frei    */
  18. {
  19.     if(_first_mlist) PPCDeletePool(_first_mlist);
  20. }
  21. void *malloc(size_t nbytes)
  22. {
  23.     size_t *p;
  24.     if(!nbytes) return(0);
  25.     if(!_first_mlist) _first_mlist=PPCCreatePool(0,_nalloc,_nalloc);
  26.     if(!_first_mlist) return(0);
  27.     p=PPCAllocPooled(_first_mlist,nbytes+sizeof(size_t));
  28.     if(!p) return(0);
  29.     *p=nbytes;
  30.     return(p+1);
  31. }
  32. void free(void *ap)
  33. {
  34.     size_t *p=ap;
  35.     if(!p||!_first_mlist) return;
  36.     p--;
  37.     PPCFreePooled(_first_mlist,p,*p+sizeof(size_t));
  38. }
  39.  
  40. void *realloc(void *old,size_t nsize)
  41. {
  42.     size_t osize;void *new;
  43.     if(!old) return(malloc(nsize));
  44.     osize=*((size_t *)old-1);
  45.     if(new=malloc(nsize)){
  46.         memcpy(new,old,osize>nsize ? nsize:osize);
  47.         free(old);
  48.     }
  49.     return(new);
  50. }
  51.