Debug swcsa
[SXSI/TextCollection.git] / swcsa / utils / basics.c
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4
5 // Basics
6
7 // #include "basics.h" included later to avoid macro recursion for malloc
8 #include <sys/types.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #define ALLOC_WARN_LIMIT (40*1024*1024)
12
13                 // Memory management
14
15 void *Malloc_ (size_t n, size_t l, char * file)
16
17            { void *p;
18
19              if (n > ALLOC_WARN_LIMIT)
20                {
21                  fprintf(stderr, "\nWarning: allocating %lu bytes, file:%s, line: %lu\n",
22                          n, file, l);
23                };
24
25              p = (void*) malloc (n);
26              if (p == NULL && n > 0)
27                {
28                  fprintf (stderr,"Could not allocate %lu bytes, file: %s, line: %lu\n",n,file,l);
29                  exit(1);
30                 }
31              return p;
32            }
33
34 void Free_ (void *p, size_t l, char * file)
35
36            { if (p) free (p);
37              else {
38                fprintf (stderr,"Double free, file: %s, line: %lu\n",file,l);
39                exit(1);
40              }
41            }
42
43 void *Realloc_ (void *p, size_t n, size_t l, char * file)
44 {
45   if (n > ALLOC_WARN_LIMIT)
46     {
47       fprintf(stderr, "\nWarning: allocating %lu bytes, file:%s, line: %lu\n",
48               n, file, l);
49     };
50
51   p = (void*) realloc (p,n);
52   if (p == NULL)
53     {
54       fprintf (stderr,"Could not re-allocate %lu bytes, file: %s, line: %lu\n",n,file,l);
55       exit(1);
56     }
57   return p;
58 }
59
60 #include "basics.h"
61
62         // bits needed to represent a number between 0 and n
63
64 uint _bits (uint n)
65
66    { uint b = 0;
67      while (n)
68         { b++; n >>= 1; }
69      return b;
70    }
71
72         // returns e[p..p+len-1], assuming len <= W
73
74 uint bitread (uint *e, uint p, uint len)
75
76    { uint answ;
77      e += p/W; p %= W;
78      answ = *e >> p;
79      if (len == W)
80           { if (p) answ |= (*(e+1)) << (W-p);
81           }
82      else { if (p+len > W) answ |= (*(e+1)) << (W-p);
83             answ &= (1<<len)-1;
84           }
85      return answ;
86    }
87
88
89         // writes e[p..p+len-1] = s, len <= W
90
91 void bitwrite (register uint *e, register uint p,
92                register uint len, register uint s)
93
94    { e += p/W; p %= W;
95      if (len == W)
96           { *e |= (*e & ((1<<p)-1)) | (s << p);
97             if (!p) return;
98             e++;
99             *e = (*e & ~((1<<p)-1)) | (s >> (W-p));
100           }
101      else { if (p+len <= W)
102                { *e = (*e & ~(((1<<len)-1)<<p)) | (s << p);
103                  return;
104                }
105             *e = (*e & ((1<<p)-1)) | (s << p);
106             e++; len -= W-p;
107             *e = (*e & ~((1<<len)-1)) | (s >> (W-p));
108           }
109    }
110         // writes e[p..p+len-1] = 0
111
112 void bitzero2 (register uint *e, register uint p,
113                register uint len)
114
115    { e += p/W; p %= W;
116      if (p+len >= W)
117         { *e &= ~((1<<p)-1);
118           len -= p;
119           e++; p = 0;
120         }
121      while (len >= W)
122         { *e++ = 0;
123           len -= W;
124         }
125      if (len > 0)
126         *e &= ~(((1<<len)-1)<<p);
127    }
128 #ifdef __cplusplus
129 }
130 #endif