1 #include <stdio.h>
2 #include "xlisp.h"
3 #include "sound.h"
4 #include "falloc.h"
5 #include "debug.h"
6 
7 /* The DEBUG_MEM related routines are:
8  *    dbg_mem_allocated: called when memory is allocated
9  *    dbg_mem_freed: called when memory is freed
10  *    dbg_mem_released: called when memory is released
11  */
12 
13 /* CHANGE LOG
14  * --------------------------------------------------------------------
15  * 28Apr03  dm  changes for portability and fix compiler warnings
16  */
17 
18 
19 #if DEBUG_MEM
20 typedef struct {
21     long seq_num;
22     char *who;
23 } dbg_mem_node, *dbg_mem_type;
24 
25 static long dbg_mem_last_seq_num = 0;
26 long dbg_mem_seq_num = 0;
27 long dbg_mem_trace = 0x410988;
28 
dbg_mem_pause(void)29 void dbg_mem_pause(void)
30 {
31     stdputstr("RETURN to continue: ");
32     getchar();
33 }
34 
35 
dbg_mem_allocated(void * p,char * who)36 void dbg_mem_allocated(void *p, char *who)
37 {
38     dbg_mem_type info = (dbg_mem_type) p;
39     if (p == (void *) dbg_mem_trace) {
40         nyquist_printf("dbg_mem_allocated(%p, %s)\n", p, who);
41     }
42     info--; /* info is stored (hidden) BEFORE the data */
43     dbg_mem_last_seq_num++;
44     if (dbg_mem_last_seq_num == dbg_mem_seq_num) {
45         nyquist_printf("dbg_mem_allocated: "
46                        "%s just allocated %p as number %d\n",
47                        who, p, (int)dbg_mem_last_seq_num);
48         dbg_mem_pause();
49     }
50     info->seq_num = dbg_mem_last_seq_num;
51     info->who = who;
52 }
53 
54 
dbg_mem_freed(void * p,char * who)55 void dbg_mem_freed(void *p, char *who)
56 {
57     dbg_mem_type info = (dbg_mem_type) p;
58     if (p == (void *) dbg_mem_trace) {
59         nyquist_printf("dbg_mem_freed(%p, %s)\n", p, who);
60     }
61     info--; /* info is stored (hidden) BEFORE the data */
62     if (!info->who) {
63         nyquist_printf("MEMORY %p FREED TWICE!, "
64                        "second time by: %s, seq_num %d\n",
65                        p, who, (int)info->seq_num);
66         fflush(stdout);
67         dbg_mem_pause();
68     }
69     if (info->seq_num == dbg_mem_seq_num) {
70         nyquist_printf("dbg_mem_freed: %s freeing %p, number %d\n",
71                        who, p, (int)dbg_mem_seq_num);
72         dbg_mem_pause();
73     }
74     info->who = NULL;
75 }
76 
dbg_mem_released(void * p,char * who)77 void dbg_mem_released(void *p, char *who)
78 {
79     dbg_mem_type info = (dbg_mem_type) p;
80     if (p == (void *) dbg_mem_trace) {
81         nyquist_printf("dbg_mem_released(%p, %s)\n", p, who);
82     }
83     info--; /* info is stored (hidden) BEFORE the data */
84     if (!info->who) {
85         nyquist_printf("MEMORY %p RELEASED BUT NOT ALLOCATED!, "
86                        "released by: %s, seq_num %d\n",
87                        p, who, (int)info->seq_num);
88         fflush(stdout);
89         dbg_mem_pause();
90     }
91     if (info->seq_num == dbg_mem_seq_num) {
92        nyquist_printf("dbg_mem_released: %s releasing %p, number %d\n",
93                       who, p, (int)dbg_mem_seq_num);
94         dbg_mem_pause();
95     }
96 }
97 
98 
dbg_mem_check(void * p,char * who)99 void dbg_mem_check(void *p, char *who)
100 {
101     dbg_mem_type info = (dbg_mem_type) p;
102     if (!info) {
103         nyquist_printf("DBG_MEM_CHECK (from %s): NULL POINTER!", who);
104          fflush(stdout);
105         dbg_mem_pause();
106     }
107     info--; /* info is stored (hidden) BEFORE the data */
108     if (!info->who) {
109        nyquist_printf("DBG_MEM_CHECK (from %s): %p IS FREE!, seq_num %d\n",
110                       who, p, (int)info->seq_num);
111        fflush(stdout);
112        dbg_mem_pause();
113     }
114 }
115 
116 
dbg_mem_print(char * msg,void * p)117 void dbg_mem_print(char *msg, void *p)
118 {
119     dbg_mem_type info = (dbg_mem_type) p;
120     stdputstr(msg);
121     if (!info) {
122         stdputstr(" NULL POINTER");
123     } else {
124         info--; /* info is stored (hidden) BEFORE the data */
125         if (!info->who) {
126             nyquist_printf(" %p IS FREE!, ", p);
127         } else {
128             nyquist_printf(" %p allocated by %s, ", p, info->who);
129         }
130         nyquist_printf("seq_num %d\n", (int)info->seq_num);
131     }
132 }
133 #endif
134 
135 
print_sound_type(sound_type s)136 void print_sound_type(sound_type s)
137 {
138     snd_list_type list;
139     int blockcount;
140 
141     nyquist_printf("sound_type: 0x%p\n", s);
142     nyquist_printf("\tt0: %f\n", s->t0);
143     nyquist_printf("\tsr: %f\n", s->sr);
144     nyquist_printf("\tcurrent: %d\n", (int)s->current);
145     nyquist_printf("\tlogical_stop_cnt: %d\n", (int)s->logical_stop_cnt);
146     nyquist_printf("\tlist: 0x%p\n", s->list);
147     nyquist_printf("\tscale: %f\n", s->scale);
148 
149     list = s->list;
150     blockcount = 0;
151     nyquist_printf("\t(0x%p:0x%p)->", list, list->block);
152     while (list->block) {
153         list = list->u.next;
154         if (blockcount < 50) {
155             nyquist_printf("(0x%p block 0x%p)->", list, list->block);
156         }
157         else {
158             stdputstr(" ... ");
159             break;
160         }
161         blockcount++;
162     }
163     stdputstr("\n");
164 }
165 
print_snd_list_type(snd_list_type list)166 void print_snd_list_type(snd_list_type list)
167 {
168     nyquist_printf("%p: [%p[%d], %p] refcnt %d ls %d", list, list->block,
169             list->block_len, list->u.next,
170             list->refcnt, list->logically_stopped);
171 }
172 
173 
174 
print_sample_block_type(char * label,sample_block_type sampblock,int len)175 void print_sample_block_type(char *label,
176                              sample_block_type sampblock,
177                              int len)
178 {
179     int j;
180     sample_block_values_type samp;
181 
182     samp = sampblock->samples;
183     nyquist_printf("%s: [%p(ref %d): len %d]: =========>>",
184                    label, sampblock, (int)sampblock->refcnt, len);
185     for (j = 0; j < len; j++) {
186         nyquist_printf("%6g ", *samp++);
187     }
188     stdputstr("\n");
189 }
190 
191 
192 /*******/
193 snd_susp_type susp_to_watch = NULL;
194 
watch_susp(snd_susp_type s)195 void watch_susp(snd_susp_type s)
196 {
197     if (!susp_to_watch) {
198         susp_to_watch = s;
199         nyquist_printf("watching susp %p\n", s);
200     }
201 }
202 
203 sound_type sound_to_watch = NULL;
204 
watch_sound(sound_type s)205 void watch_sound(sound_type s)
206 {
207     if (!sound_to_watch) {
208         sound_to_watch = s;
209         nyquist_printf("watching sound %p\n", s);
210     }
211 }
212 
213 
214 snd_list_type snd_list_to_watch = NULL;
215 
watch_snd_list(snd_list_type s)216 void watch_snd_list(snd_list_type s)
217 {
218     snd_list_to_watch = s;
219     nyquist_printf("watching snd_list %p\n", s);
220 }
221 
222 
snd_list_debug(snd_list_type snd_list,char * s)223 void snd_list_debug(snd_list_type snd_list, char *s)
224 {
225     if (snd_list == snd_list_to_watch) {
226         nyquist_printf("%s%s\n", s,
227                " appended to snd_list_to_watch.");
228         watch_snd_list(snd_list->u.next);
229     }
230 }
231 
232 
snd_list_report(snd_list_type snd_list,char * s)233 void snd_list_report(snd_list_type snd_list, char *s)
234 {
235     if (snd_list == snd_list_to_watch) {
236         nyquist_printf("%s: fetching block for watched snd_list.\n",
237                s);
238     }
239 }
240 
241 
242 #ifdef IGNORE
test_it()243 void test_it()
244 {
245     if (susp_to_watch && susp_to_watch->keep_fetch)
246         stdputstr("WE FOUND A SERIOUS PROBLEM\n");
247 }
248 #endif
249 
250