1 /*
2  * falloc.c
3  * data for Nyquist memory allocation.
4  */
5 
6 #include <stdio.h>
7 #include <assert.h>
8 #include "xlisp.h"
9 #include "sound.h"
10 #include "falloc.h"
11 
12 /* special free lists */
13 CQUE *sample_block_free = NULL;    /* really a sample_block_type */
14 
15 /* special counts */
16 int sample_block_used = 0;
17 int sample_block_low_water = 0;
18 int sample_block_total = 0;
19 int snd_list_used = 0;
20 int sound_used = 0;
21 
22 /* generic free lists */
23 CQUE *generic_free[MAXLISTS];
24 
25 
falloc_init(void)26 void falloc_init(void)
27 {
28     int i;
29     for (i = 0; i < MAXLISTS; i++) generic_free[i] = NULL;
30 }
31 
32 
33 /* memory pool */
34 char *poolp = NULL;
35 char *poolend = NULL;
36 
37 /* sample block memory pool */
38 char *spoolp = NULL;
39 char *spoolend = NULL;
40 
41 int npools = 0;
42 
43 #if defined(TRACK_POOLS) && TRACK_POOLS
44 #define POOL_HEAD_SIZE (round_size(sizeof(CQUE)))
45 CQUE *pools = NULL;
46 #endif
47 
sound_already_free_test(sound_type s)48 void sound_already_free_test(sound_type s)
49 {
50     sound_type sp;
51     for (sp = (sound_type) sound_free; sp; sp = (sound_type) ((CQUE *) sp)->qnext) {
52         if (s == sp) {
53             stdputstr("SOUND ALREADY FREE!!!");
54             fflush(stdout);
55             sp = 0; sp->list = 0;   /* trap to debugger */
56         }
57     }
58 }
59 
60 
61 /* new_pool -- allocate a new pool from which mem is allocated */
62 /**/
new_pool(void)63 void new_pool(void)
64 {
65     poolp = (char *) malloc(MAXPOOLSIZE);
66 
67     if (poolp == NULL) {
68         fprintf(STDERR, "Nyquist: out of memory!\n");
69         EXIT(1);
70     }
71 
72     poolend = poolp + MAXPOOLSIZE;
73     npools++;
74     /* stick to double word boundaries */
75     poolp = (char *) round_size(((intptr_t) poolp));
76 }
77 
78 /* new_spool -- allocate a new spool from which sample blocks are allocated */
79 /**/
new_spool(void)80 void new_spool(void)
81 {
82 #if defined(TRACK_POOLS) && TRACK_POOLS
83     spoolp = (char *) malloc(MAXSPOOLSIZE + POOL_HEAD_SIZE);
84 #else
85     spoolp = (char *) malloc(MAXSPOOLSIZE);
86 #endif
87 
88     if (spoolp == NULL) {
89         fprintf(STDERR, "Nyquist: out of memory!\n");
90         EXIT(1);
91     }
92 
93 #if defined(TRACK_POOLS) && TRACK_POOLS
94     Qenter(pools, spoolp);
95     spoolp += POOL_HEAD_SIZE;
96 #endif
97 
98     spoolend = spoolp + MAXSPOOLSIZE;
99     npools++;
100     /* stick to double word boundaries */
101     spoolp = (char *) round_size(((intptr_t) spoolp));
102 }
103 
104 
105 /* find_sample_block -- get sample block when freelist is empty */
106 /* Try these strategies in order:
107    1) try free list
108    2) use pool to get sample_blocks_low_water + BLOCKS_PER_GC blocks or until
109       pool runs out
110    3) GC and try free list again, set sample_blocks_low_water to
111       sample_blocks_used
112    4) try pool again
113    5) allocate new pool and use it
114  */
find_sample_block(sample_block_type * sp)115 void find_sample_block(sample_block_type *sp)
116 {
117     if (!Qempty(sample_block_free)) {
118         Qget(sample_block_free, sample_block_type, *sp);
119     } else if (sample_block_total < sample_block_low_water + BLOCKS_PER_GC &&
120                sample_block_total < max_sample_blocks &&
121                check_spool(round_size(sizeof(sample_block_node)))) {
122         if (DEBUG_MEM) spoolp += DEBUG_MEM_INFO_SIZE;
123         *sp = (sample_block_type) spoolp;
124         spoolp += round_size(sizeof(sample_block_node));
125         sample_block_total++;
126     } else {
127         gc();
128         sample_block_low_water = sample_block_used;
129         if (!Qempty(sample_block_free)) {
130             Qget(sample_block_free, sample_block_type, *sp);
131         } else if (sample_block_used >= max_sample_blocks) {
132             /* we are not allowed to allocate more */
133             stdputstr("The maximum number of sample blocks has been\n");
134             stdputstr("reached, so audio computation must be terminated.\n");
135             stdputstr("Probably, your program should not be retaining\n");
136             stdputstr("so many samples in memory. You can get and set\n");
137             stdputstr("the maximum using SND-SET-MAX-AUDIO-MEM.\n");
138             xlfail("audio memory exhausted");
139         } else if (check_spool(round_size(sizeof(sample_block_node)))) {
140             if (DEBUG_MEM) spoolp += DEBUG_MEM_INFO_SIZE;
141             *sp = (sample_block_type) spoolp;
142             spoolp += round_size(sizeof(sample_block_node));
143             sample_block_total++;
144         } else if (sample_block_used < max_sample_blocks) {
145             new_spool();
146             if (DEBUG_MEM) spoolp += DEBUG_MEM_INFO_SIZE;
147             *sp = (sample_block_type) spoolp;
148             spoolp += round_size(sizeof(sample_block_node));
149             sample_block_total++;
150         }
151     }
152     (*sp)->refcnt = 1;   \
153     sample_block_used++; \
154 }
155 
156 
157 
158 /* get_from_pool -- return size bytes from pool memory */
159 /**/
get_from_pool(size_t siz)160 char *get_from_pool(size_t siz)
161 {
162     if (!check_pool(siz)) {
163         new_pool();
164     }
165     poolp += siz;
166     if (DEBUG_MEM) poolp += DEBUG_MEM_INFO_SIZE; /* allow for debug info */
167     return poolp - siz;
168 }
169 
170 
171 #if defined(TRACK_POOLS) && TRACK_POOLS
172 
173 /* falloc_gc -- return empty pools to the system */
174 /*
175  * Algorithm: for each pool, move all free sample blocks
176  * (on the sample_block_free list) to tlist. If tlist
177  * has ALL of the blocks in the pool (determined by
178  * byte counts), the pool is returned to the heap.
179  */
falloc_gc()180 void falloc_gc()
181 {
182    CQUE *lp = NULL;
183    CQUE *cp;
184    CQUE *np;
185    CQUE *tlist = NULL;
186 
187    /* Scan all allocated pools */
188    for (cp = pools; cp; lp = cp, cp = np) {
189       char *str = ((char *)cp) + POOL_HEAD_SIZE;
190       char *end = str + MAXSPOOLSIZE;
191       intptr_t tsiz = end - str;
192       long csiz = 0;
193       CQUE *tsave = NULL;
194       CQUE *ln = NULL;
195       CQUE *cn;
196       CQUE *nn;
197 
198       /* Save pointer to next pool */
199       np = cp->qnext;
200 
201       /* Remember head of temp free list */
202       tsave = tlist;
203 
204       /* Scan all nodes on the free list */
205       for (cn = sample_block_free; cn; ln = cn, cn = nn) {
206 
207          /* Get next node */
208          nn = cn->qnext;
209 
210          /* Count it if the node belongs to this pool */
211          if (cn >= (CQUE *) str && cn <= (CQUE *) end) {
212             csiz += round_size(sizeof(sample_block_node));
213 
214             Qenter(tlist, cn);
215 
216             /* Unlink the node */
217             if (cn == sample_block_free) {
218                sample_block_free = nn;
219                cn = NULL;
220             }
221             else {
222                ln->qnext = nn;
223                cn = ln;
224             }
225          }
226       }
227 
228       /* The pool had inuse nodes */
229       if (csiz != tsiz) {
230          continue;
231       }
232 
233       /* Remove the nodes from the temp free list */
234       tlist = tsave;
235 
236       /* Maintain stats */
237       sample_block_total -= (int)
238               (tsiz / round_size(sizeof(sample_block_node)));
239       npools--;
240 
241       /* If this is the active pool, then reset current pointers */
242       if (spoolp >= str && spoolp <= end) {
243          spoolp = NULL;
244          spoolend = NULL;
245       }
246 
247       /* Release the pool to the system */
248       free(cp);
249 
250       /* Unlink this pool from the list */
251       if (cp == pools) {
252          pools = np;
253          cp = NULL;
254       }
255       else {
256          /* lp cannot be null here: On 1st iteration, lp == NULL, but
257           * cp == pools, so code above is executed. Before the for-loop
258           * iterates, pools == np (assigned above), and cp == NULL. The
259           * for-loop update (lp=cp,cp=np) produces lp == NULL, cp == pools.
260           * Since cp == pools, this else branch will not be taken.
261           * The other path to this code is via the "continue" above. In that
262           * case, the update (lp=cp,cp=np) makes lp a valid pointer or else
263           * the loop exits.
264           * The assert(lp) is here to possibly make static analyzers happy.
265           */
266          assert(lp);
267          lp->qnext = np;
268          cp = lp;
269       }
270    }
271 
272    /* Resave list of free nodes */
273    sample_block_free = tlist;
274 }
275 
276 #endif
277 
278 
279