home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / v / vim_src.zip / ALLOC.C < prev    next >
C/C++ Source or Header  |  1993-01-12  |  2KB  |  138 lines

  1. /* vi:ts=4:sw=4
  2.  *
  3.  * VIM - Vi IMitation
  4.  *
  5.  * Code Contributions By:    Bram Moolenaar            mool@oce.nl
  6.  *                            Tim Thompson            twitch!tjt
  7.  *                            Tony Andrews            onecom!wldrdg!tony 
  8.  *                            G. R. (Fred) Walter        watmath!watcgl!grwalter 
  9.  */
  10.  
  11. /*
  12.  * alloc.c
  13.  *
  14.  * This file contains various routines dealing with allocation and
  15.  * deallocation of data structures.
  16.  */
  17.  
  18. #include "vim.h"
  19. #include "globals.h"
  20. #include "proto.h"
  21.  
  22. #ifdef AMIGA
  23. # undef FALSE            /* these are redefined in exec/types.h */
  24. # undef TRUE
  25. # include <exec/types.h>
  26. # include <exec/memory.h>
  27. # undef FALSE
  28. # define FALSE 0        /* define FALSE and TRUE as ints instead of longs */
  29. # undef TRUE
  30. # define TRUE 1
  31. #endif /* AMIGA */
  32.  
  33. #ifdef MSDOS
  34. # include <alloc.h>
  35. #endif /* MSDOS */
  36.  
  37. #define PANIC_FACTOR_CHIP 8192L
  38.  
  39.     char *
  40. alloc(size)
  41.     unsigned        size;
  42. {
  43.     return (lalloc((u_long)size, TRUE));
  44. }
  45.  
  46.     char *
  47. lalloc(size, message)
  48.     u_long            size;
  49.     int                message;
  50. {
  51.     register char   *p;            /* pointer to new storage space */
  52.  
  53.     if ((p = malloc(size)) != NULL)
  54.     {
  55. #ifdef AMIGA
  56.         if (AvailMem((long)MEMF_CHIP) < PANIC_FACTOR_CHIP)
  57.         {                                 /* System is low... no go! */
  58.                 free(p);
  59.                 p = NULL;
  60.         }
  61. #endif
  62. #ifdef MSDOS
  63.         if (coreleft() < PANIC_FACTOR_CHIP)
  64.         {                                 /* System is low... no go! */
  65.                 free(p);
  66.                 p = NULL;
  67.         }
  68. #endif
  69.     }
  70.     if (message && p == NULL)
  71.         emsg(e_outofmem);
  72.     return (p);
  73. }
  74.  
  75. /*
  76.  * copy a string into newly allocated memory
  77.  */
  78.     char *
  79. strsave(string)
  80.     char           *string;
  81. {
  82.     char *p;
  83.  
  84.     p = alloc((unsigned) (strlen(string) + 1));
  85.     if (p != NULL)
  86.         strcpy(p, string);
  87.     return p;
  88. }
  89.  
  90.     char *
  91. strnsave(string, len)
  92.     char        *string;
  93.     int         len;
  94. {
  95.     char *p;
  96.  
  97.     p = alloc((unsigned) (len + 1));
  98.     if (p != NULL)
  99.     {
  100.         strncpy(p, string, (size_t)len);
  101.         p[len] = NUL;
  102.     }
  103.     return p;
  104. }
  105.  
  106. /*
  107.  * copy a number of spaces
  108.  */
  109.     void
  110. copy_spaces(ptr, count)
  111.     char    *ptr;
  112.     size_t    count;
  113. {
  114.     register size_t    j;
  115.  
  116.     while (count)        /* copy 15 spaces at a time */
  117.     {
  118.         j = count;
  119.         if (j > 15)
  120.             j = 15;
  121.         memmove(ptr, spaces, j);
  122.         ptr += j;
  123.         count -= j;
  124.     }
  125. }
  126.  
  127.     char *
  128. mkstr(c)
  129.     unsigned      c;
  130. {
  131.     static char      s[2];
  132.  
  133.     s[0] = c;
  134.     s[1] = NUL;
  135.  
  136.     return s;
  137. }
  138.