home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3.4.17 [SPARC, PA-RISC] / nextstep33_risc.iso / NextLibrary / TeX / tex / src / texview / mymalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-20  |  2.1 KB  |  111 lines

  1. /*
  2.  *   mymalloc.c of dviamiga software package.
  3.  */
  4. #include "structures.h"
  5. /*
  6.  *   External variables we access
  7.  */
  8. extern TeXfontdesctype *TeXfonts[] ;
  9. extern fontdesctype *fontlist ;
  10. extern struct fontdesctype *curfnt ;
  11. /*
  12.  *   Functions we use.
  13.  */
  14. extern void *malloc() ;
  15. extern void error() ;
  16. extern void exit() ;
  17. extern void freetemp() ;
  18. extern void free() ;
  19. extern void fontinit() ;
  20. extern void freepages() ;
  21. /*
  22.  *   Our own memory clear routine.
  23.  */
  24. void clearmem(p, size)
  25. register long *p ;
  26. register int size ;
  27. {
  28.    while (size >= sizeof(long)) {
  29.       *p++ = 0 ;
  30.       size -= sizeof(long) ;
  31.    }
  32.    while (size > 0) {
  33.       size-- ;
  34.       ((char *)p)[size] = 0 ;
  35.    }
  36. }
  37. /*
  38.  *   We also need to be able to copy memory.
  39.  */
  40. void movmem(src, dest, size)
  41. register long *src, *dest, size ;
  42. {
  43.    while (size >= sizeof(long)) {
  44.       *dest++ = *src++ ;
  45.       size -= sizeof(long) ;
  46.    }
  47.    while (size > 0) {
  48.       size-- ;
  49.       ((char *)dest)[size] = ((char *)src)[size] ;
  50.    }
  51. }
  52. /*
  53.  *   We also need to be able to copy memory.
  54.  */
  55. void cmpmem(src, dest, size)
  56. register long *src, *dest, size ;
  57. {
  58.    while (size > sizeof(long)) {
  59.       *dest++ = ~*src++ ;
  60.       size -= sizeof(long) ;
  61.    }
  62.    while (size > 0) {
  63.       size-- ;
  64.       ((char *)dest)[size] = ~((char *)src)[size] ;
  65.    }
  66. }
  67. /*
  68.  *   This routine allocates memory of the specific type.  If the allocation
  69.  *   fails, we try to free memory by deleting some fonts.
  70.  */
  71. void *mymalloc(size, type)
  72. int size ;
  73. long type ;
  74. {
  75.    register void *p ;
  76.  
  77.    p = malloc(size) ;
  78.    if (p==NULL)
  79.       error("! could not allocate memory") ;
  80.    if (type & MEMF_CLEAR)
  81.       clearmem(p, size) ;
  82.    return(p) ;
  83. }
  84. /*
  85.  *   Some tempbuf routines.
  86.  */
  87. static void *tmpbuf = NULL ;
  88. static long csize = 0L ;
  89. void *tempbuf(size)
  90. long size ;
  91. {
  92.    if (size > csize) {
  93.       freetemp() ;
  94.       tmpbuf = mymalloc((int)size, MEMF_CLEAR) ;
  95.       csize = size ;
  96.    } else
  97.       clearmem(tmpbuf, size) ;
  98.    return(tmpbuf) ;
  99. }
  100. /*
  101.  *   Frees the tempbuf.
  102.  */
  103. void
  104. freetemp() {
  105.    if (tmpbuf != NULL) {
  106.       free(tmpbuf) ;
  107.       tmpbuf = NULL ;
  108.       csize = 0 ;
  109.    }
  110. }
  111.