summaryrefslogtreecommitdiff
path: root/src/common/alloc.c
blob: 7a048b1bc1bf6668d61c8d336575565b4d6e4683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * alloc.c -- allocation routines for the Icon compiler
 */

#include "../h/gsupport.h"

#ifdef TypTrc
   int typealloc = 0;		/* type allocation switch */
   long typespace = 0;		/* type allocation amount */
#endif					/* TypTrc */

/*
 * salloc - allocate and initialize string
 */

char *salloc(s)
char *s;
   {
   register char *s1;

   s1 = (char *)malloc(strlen(s) + 1);
   if (s1 == NULL) {
      fprintf(stderr, "salloc(%d): out of memory\n", (int)strlen(s) + 1);
      exit(EXIT_FAILURE);
      }
   return strcpy(s1, s);
   }

/*
 * alloc - allocate n bytes
 */

pointer alloc(n)
unsigned int n;
   {
   register pointer a;

#ifdef AllocTrace
   static int sum = 0;
#endif					/* AllocTrace */

#ifdef TypTrc
   if (typealloc)
      typespace += (long)n;
#endif					/* TypTrc */

#ifdef AllocTrace
   sum = sum + n;
   if (sum > 5000) {
      fprintf(stderr, ".");
      fflush(stderr);
      sum = 0;
      };
#endif					/* AllocTrace */

   if (n == 0)				/* Work-around for 0 allocation */
      n = 1;

   a = calloc(n, sizeof(char));
   if (a == NULL) {
      fprintf(stderr, "alloc(%d): out of memory\n", (int)n);
      exit(EXIT_FAILURE);
      }
   return a;
   }