Ticket #514: filldir.snip2

File filldir.snip2, 4.9 KB (added by John Small, 11 years ago)

Code snippet of pci-tracing debug code

Line 
1typedef struct {
2 PCNRITEM pci;
3 PSZ pszFileName;
4 BOOL bAllocated;
5 }
6PCIHIST, *PPCIHIST;
7#define PciMaint(a, b) PciMaint2(a, b, pszSrcFile, __LINE__)
8void PciMaint2(PCNRITEM pci, BOOL bPciAllocation, PSZ pszSource, int iLine)
9{
10#define HIST_COUNT 1000
11
12 static HMTX hmtxPciTable = NULLHANDLE;
13 static PPCIHIST pHistory = NULL;
14 static USHORT usAllocations = 1;
15 ULONG ulNumStructs = usAllocations * HIST_COUNT;
16 UINT i, i2;
17 APIRET rc;
18 char szFile[CCHMAXPATH];
19
20 if (!hmtxPciTable) {
21 rc = DosCreateMutexSem(NULL, &hmtxPciTable, 0L, TRUE);
22 if (rc) {
23 fprintf(stderr, "Unable to create pcitable mutex: %d\n", rc);
24 fflush(stderr);
25 return;
26 }
27 pHistory = xmalloc(HIST_COUNT * usAllocations * sizeof(PCIHIST), pszSrcFile, __LINE__);
28 memset((void *)pHistory, 0, HIST_COUNT * sizeof(PCIHIST));
29 } else {
30 rc = DosRequestMutexSem(hmtxPciTable, SEM_INDEFINITE_WAIT);
31 if (rc) {
32 fprintf(stderr, "Error requesting mutex: %d\n", rc);
33 fflush(stderr);
34 return;
35 }
36 }
37 strcpy(szFile, ((pci->pszFileName && *pci->pszFileName) ? pci->pszFileName : "N/A"));
38 fprintf(stderr, "PciMaint entry. pci: %08x New: %c Line: %d src: %s file: %s\n",
39 pci, (bPciAllocation ? 'Y' : 'N'), iLine, pszSource, szFile);
40 if (!pci) {
41 DosReleaseMutexSem(hmtxPciTable);
42 fflush(stderr);
43 return;
44 }
45 for (i = 0; i < ulNumStructs; i++) {
46 if (!pHistory[i].pci || pHistory[i].pci == pci)
47 break;
48 }
49 if (i == ulNumStructs) {
50 fprintf(stderr, "PciMaint history reallocation: %d\n", usAllocations++);
51 ulNumStructs += HIST_COUNT;
52 pHistory = xrealloc(pHistory, usAllocations * HIST_COUNT * sizeof(PCIHIST), pszSrcFile, __LINE__);
53 usAllocations++;
54 for (i2 = i; i2 < i + HIST_COUNT; i2++) {
55 pHistory[i2].pci = NULL;
56 }
57 }
58 if (bPciAllocation) {
59 if (!pHistory[i].pci) {
60 fprintf(stderr, "PciMaint using new slot %d\n", i);
61 pHistory[i].pci = pci;
62 pHistory[i].bAllocated = TRUE;
63 if (pci->pszFileName && *pci->pszFileName) {
64 pHistory[i].pszFileName = xstrdup(pci->pszFileName, pszSrcFile, __LINE__);
65 } else {
66 pHistory[i].pszFileName = NULL;
67 }
68 } else {
69 if (pHistory[i].bAllocated) { // Error: repeat allocation w/o intervening dealloc
70 strcpy(szFile, (pHistory[i].pszFileName ? pHistory[i].pszFileName : "N/A"));
71 fprintf(stderr, "PciMaint error: Missed dealloc or repeat alloc for pci: %08x slot: %d prev file: %s\n",
72 pci, i, szFile);
73 pHistory[i].pci = pci;
74 pHistory[i].bAllocated = TRUE;
75 if (pHistory[i].pszFileName && (pHistory[i].pszFileName != NullStr)) free(pHistory[i].pszFileName);
76 if (pci->pszFileName && *pci->pszFileName) {
77 pHistory[i].pszFileName = xstrdup(pci->pszFileName, pszSrcFile, __LINE__);
78 } else {
79 pHistory[i].pszFileName = NULL;
80 }
81 } else { // OK, realloc deallocated pci
82 fprintf(stderr, "PciMaint re-using slot %d\n", i);
83 pHistory[i].bAllocated = TRUE;
84 if (pHistory[i].pszFileName && (pHistory[i].pszFileName != NullStr)) free(pHistory[i].pszFileName);
85 if (pci->pszFileName && *pci->pszFileName) {
86 pHistory[i].pszFileName = xstrdup(pci->pszFileName, pszSrcFile, __LINE__);
87 } else {
88 pHistory[i].pszFileName = NULL;
89 }
90 }
91 }
92 } else { // Deallocation
93 if (!pHistory[i].pci) { // Error: not previously allocated
94 fprintf(stderr, "PCiMaint error: Missed alloc or dealloc before alloc for pci: %08x slot %d\n", pci, i);
95 if (pci->pszFileName && *pci->pszFileName) {
96 pHistory[i].pszFileName = xstrdup(pci->pszFileName, pszSrcFile, __LINE__);
97 } else {
98 pHistory[i].pszFileName = NULL;
99 }
100 } else {
101 if (pHistory[i].bAllocated) {
102 fprintf(stderr, "PciMaint deallocating slot %d\n", i);
103 pHistory[i].bAllocated = FALSE;
104 if (pHistory[i].pszFileName && (pHistory[i].pszFileName != NullStr)) free(pHistory[i].pszFileName);
105 if (pci->pszFileName && *pci->pszFileName) {
106 pHistory[i].pszFileName = xstrdup(pci->pszFileName, pszSrcFile, __LINE__);
107 } else {
108 pHistory[i].pszFileName = NULL;
109 }
110 } else { // Error: 2x free
111 fprintf(stderr, "PCiMaint error: Missed alloc or repeat dealloc pci: %08x slot: %d prev. file: %s\n", pci, i);
112 if (pHistory[i].pszFileName && (pHistory[i].pszFileName != NullStr)) free(pHistory[i].pszFileName);
113 if (pci->pszFileName && *pci->pszFileName) {
114 pHistory[i].pszFileName = xstrdup(pci->pszFileName, pszSrcFile, __LINE__);
115 } else {
116 pHistory[i].pszFileName = NULL;
117 }
118 }
119 }
120 }
121 DosReleaseMutexSem(hmtxPciTable);
122 fflush(stderr);
123 return;
124}
125