home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / gnulib / normal / ger_realloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  1.8 KB  |  79 lines

  1. extern struct ExecBase * SysBase;
  2.  
  3. static __inline void*
  4. AllocVec (unsigned long byteSize,unsigned long requirements)
  5. {
  6.   register void *_res  __asm("d0");
  7.   register struct ExecBase *a6 __asm("a6") = SysBase;
  8.   register unsigned long d0 __asm("d0") = byteSize;
  9.   register unsigned long d1 __asm("d1") = requirements;
  10.   __asm __volatile ("jsr a6@(-0x2ac)"
  11.   : "=r" (_res)
  12.   : "r" (a6), "r" (d0), "r" (d1)
  13.   : "a0","a1","d0","d1", "memory");
  14.   return _res;
  15. }
  16.  
  17. static __inline void
  18. FreeVec (void *memoryBlock)
  19. {
  20.   register struct ExecBase *a6 __asm("a6") = SysBase;
  21.   register void *a1 __asm("a1") = memoryBlock;
  22.   __asm __volatile ("jsr a6@(-0x2b2)"
  23.   : /* no output */
  24.   : "r" (a6), "r" (a1)
  25.   : "a0","a1","d0","d1", "memory");
  26. }
  27.  
  28. static __inline void
  29. CopyMem (void *source,void *dest,unsigned long size)
  30. {
  31.   register struct ExecBase *a6 __asm("a6") = SysBase;
  32.   register void *a0 __asm("a0") = source;
  33.   register void *a1 __asm("a1") = dest;
  34.   register unsigned long d0 __asm("d0") = size;
  35.   __asm __volatile ("jsr a6@(-0x270)"
  36.   : /* no output */
  37.   : "r" (a6), "r" (a0), "r" (a1), "r" (d0)
  38.   : "a0","a1","d0","d1", "memory");
  39. }
  40.  
  41. void *malloc(unsigned long Anz);
  42. void free(unsigned long *p);
  43.  
  44.  
  45. void *realloc(unsigned long *ptr, unsigned long size)
  46. {
  47.     /* Free memory */
  48.     if(size==0)
  49.     {
  50.         if(ptr)    free(ptr);
  51.         return 0;
  52.     }
  53.     else
  54.     if(ptr)
  55.     {
  56.         /* now we are in trouble. We depend on how the length of the memory-Block
  57.             is stored by AllocVec: in the longword before ptr the length of the
  58.             memory-Block (with the len-long)*/
  59.  
  60.         char *new_ptr=0;
  61.         unsigned long old_len=*(ptr-1)-4;
  62.  
  63.         if((new_ptr=malloc(size)))
  64.         {
  65.             if(size > old_len) size=old_len;    /* don't copy more than the old size */
  66.             CopyMem(ptr, new_ptr, size);
  67.             free(ptr);
  68.  
  69.             return new_ptr;
  70.         }
  71.         else
  72.             return 0;
  73.     }
  74.     else
  75.     {
  76.         /* behave like normal malloc */
  77.         return malloc(size);
  78.     }
  79. }