43
43
// See the Judy*(3C) manual entry for details.
45
// This code is written recursively, at least at first, because that's much
46
// simpler. Hope it's fast enough.
45
// This code is written recursively, at least at first, because thats much
46
// simpler. Hope its fast enough.
49
FUNCTION Word_t Judy1FreeArray(
49
FUNCTION Word_t Judy1FreeArray
51
FUNCTION Word_t JudyLFreeArray(
51
FUNCTION Word_t JudyLFreeArray
53
54
PPvoid_t PPArray, // array to free.
54
PJError_t PJError) // optional, for returning error info.
55
PJError_t PJError // optional, for returning error info.
56
58
jpm_t jpm; // local to accumulate free statistics.
57
Word_t JAPtype; // JAP type part of *PPArray.
60
60
// CHECK FOR NULL POINTER (error by caller):
68
68
DBGCODE(JudyCheckPop(*PPArray);)
71
// PROCESS TOP LEVEL "JAP" BRANCHES AND LEAVES:
73
70
// Zero jpm.jpm_Pop0 (meaning the array will be empty in a moment) for accurate
74
71
// logging in TRACEMI2.
76
73
jpm.jpm_Pop0 = 0; // see above.
77
74
jpm.jpm_TotalMemWords = 0; // initialize memory freed.
79
// Get pointer and type from array pointer:
81
JAPtype = JAPTYPE(*PPArray);
88
// Note: It is an error to have a JAPtype for a JAPNULL with the rest of the
89
// pointer not being null.
93
if (P_JLW(*PPArray) == (Pjlw_t) NULL) return(0);
94
break; // invalid JAP, although possibly a valid non-Judy pointer.
78
if (P_JLW(*PPArray) == (Pjlw_t) NULL) return(0);
80
// PROCESS TOP LEVEL "JRP" BRANCHES AND LEAF:
82
if (JU_LEAFW_POP0(*PPArray) < cJU_LEAFW_MAXPOP1) // must be a LEAFW
84
Pjlw_t Pjlw = P_JLW(*PPArray); // first word of leaf.
86
j__udyFreeJLW(Pjlw, Pjlw[0] + 1, &jpm);
87
*PPArray = (Pvoid_t) NULL; // make an empty array.
88
return (-(jpm.jpm_TotalMemWords * cJU_BYTESPERWORD)); // see above.
97
92
// Rootstate leaves: just free the leaf:
99
94
// Common code for returning the amount of memory freed.
101
// Note: In a an ordinary JAPLEAF, pop0 = *PPArray[0].
96
// Note: In a an ordinary LEAFW, pop0 = *PPArray[0].
103
98
// Accumulate (negative) words freed, while freeing objects.
104
99
// Return the positive bytes freed.
108
Pjlw_t Pjlw = P_JLW(*PPArray); // first word of leaf.
110
__JudyFreeJLW(Pjlw, Pjlw[0] + 1, &jpm);
111
*PPArray = (Pvoid_t) NULL; // make an empty array.
112
return (-(jpm.jpm_TotalMemWords * cJU_BYTESPERWORD)); // see above.
115
#if (LOW_POP && JUDYL)
116
case cJL_JAPLEAF_POPU1:
118
Pjlw_t Pjlw= P_JLW(*PPArray); // first word of leaf.
120
__JudyFreeJLW(Pjlw, 1, &jpm);
121
*PPArray = (Pvoid_t) NULL; // make an empty array.
122
return (-(jpm.jpm_TotalMemWords * cJU_BYTESPERWORD)); // see above.
124
case cJL_JAPLEAF_POPU2:
126
Pjlw_t Pjlw = P_JLW(*PPArray); // first word of leaf.
128
__JudyFreeJLW(Pjlw, 2, &jpm);
129
*PPArray = (Pvoid_t) NULL; // make an empty array.
130
return (-(jpm.jpm_TotalMemWords * cJU_BYTESPERWORD)); // see above.
136
102
Pjpm_t Pjpm = P_JPM(*PPArray);
137
103
Word_t TotalMem = Pjpm->jpm_TotalMemWords;
139
__JudyFreeSM(&(Pjpm->jpm_JP), &jpm); // recurse through tree.
140
__JudyFreeJPM(Pjpm, &jpm);
105
j__udyFreeSM(&(Pjpm->jpm_JP), &jpm); // recurse through tree.
106
j__udyFreeJPM(Pjpm, &jpm);
142
108
// Verify the array was not corrupt. This means that amount of memory freed
143
109
// (which is negative) is equal to the initial amount:
173
129
// the total words freed (as a negative value). "SM" = State Machine.
175
131
// Note: Corruption is not detected at this level because during a FreeArray,
176
// if the code hasn't already core dumped, it's better to remain silent, even
132
// if the code hasnt already core dumped, its better to remain silent, even
177
133
// if some memory has not been freed, than to bother the caller about the
178
134
// corruption. TBD: Is this true? If not, must list all legitimate JPNULL
179
135
// and JPIMMED above first, and revert to returning bool_t (see 4.34).
181
FUNCTION void __JudyFreeSM(
137
FUNCTION void j__udyFreeSM(
182
138
Pjp_t Pjp, // top of Judy (top-state).
183
139
Pjpm_t Pjpm) // to return words freed.
187
switch (Pjp->jp_Type)
143
switch (JU_JPTYPE(Pjp))
245
201
for (subexp = 0; subexp < cJU_NUMSUBEXPB; ++subexp)
247
jpcount = __JudyCountBitsB(JU_JBB_BITMAP(Pjbb, subexp));
203
jpcount = j__udyCountBitsB(JU_JBB_BITMAP(Pjbb, subexp));
251
207
for (offset = 0; offset < jpcount; ++offset)
253
__JudyFreeSM(P_JP(JU_JBB_PJP(Pjbb, subexp)) + offset,
209
j__udyFreeSM(P_JP(JU_JBB_PJP(Pjbb, subexp)) + offset,
256
__JudyFreeJBBJP(JU_JBB_PJP(Pjbb, subexp), jpcount, Pjpm);
212
j__udyFreeJBBJP(JU_JBB_PJP(Pjbb, subexp), jpcount, Pjpm);
259
__JudyFreeJBB((Pjbb_t) (Pjp->jp_Addr), Pjpm);
215
j__udyFreeJBB((Pjbb_t) (Pjp->jp_Addr), Pjpm);
281
237
Pjbu_t Pjbu = P_JBU(Pjp->jp_Addr);
283
239
for (offset = 0; offset < cJU_BRANCHUNUMJPS; ++offset)
284
__JudyFreeSM((Pjbu->jbu_jp) + offset, Pjpm);
240
j__udyFreeSM((Pjbu->jbu_jp) + offset, Pjpm);
286
__JudyFreeJBU((Pjbu_t) (Pjp->jp_Addr), Pjpm);
242
j__udyFreeJBU((Pjbu_t) (Pjp->jp_Addr), Pjpm);
296
252
// Note: cJU_JPLEAF1 is a special case, see discussion in ../Judy1/Judy1.h
298
#if (JUDYL || (! JU_64BIT ))
254
#if (defined(JUDYL) || (! defined(JU_64BIT)))
299
255
case cJU_JPLEAF1:
300
Pop1 = JU_JPLEAF_POP0(Pjp->jp_DcdPop0) + 1;
301
__JudyFreeJLL1((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
256
Pop1 = JU_JPLEAF_POP0(Pjp) + 1;
257
j__udyFreeJLL1((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
305
261
case cJU_JPLEAF2:
306
Pop1 = JU_JPLEAF_POP0(Pjp->jp_DcdPop0) + 1;
307
__JudyFreeJLL2((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
262
Pop1 = JU_JPLEAF_POP0(Pjp) + 1;
263
j__udyFreeJLL2((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
310
266
case cJU_JPLEAF3:
311
Pop1 = JU_JPLEAF_POP0(Pjp->jp_DcdPop0) + 1;
312
__JudyFreeJLL3((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
267
Pop1 = JU_JPLEAF_POP0(Pjp) + 1;
268
j__udyFreeJLL3((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
316
272
case cJU_JPLEAF4:
317
Pop1 = JU_JPLEAF_POP0(Pjp->jp_DcdPop0) + 1;
318
__JudyFreeJLL4((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
273
Pop1 = JU_JPLEAF_POP0(Pjp) + 1;
274
j__udyFreeJLL4((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
321
277
case cJU_JPLEAF5:
322
Pop1 = JU_JPLEAF_POP0(Pjp->jp_DcdPop0) + 1;
323
__JudyFreeJLL5((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
278
Pop1 = JU_JPLEAF_POP0(Pjp) + 1;
279
j__udyFreeJLL5((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
326
282
case cJU_JPLEAF6:
327
Pop1 = JU_JPLEAF_POP0(Pjp->jp_DcdPop0) + 1;
328
__JudyFreeJLL6((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
283
Pop1 = JU_JPLEAF_POP0(Pjp) + 1;
284
j__udyFreeJLL6((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
331
287
case cJU_JPLEAF7:
332
Pop1 = JU_JPLEAF_POP0(Pjp->jp_DcdPop0) + 1;
333
__JudyFreeJLL7((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
288
Pop1 = JU_JPLEAF_POP0(Pjp) + 1;
289
j__udyFreeJLL7((Pjll_t) (Pjp->jp_Addr), Pop1, Pjpm);
335
291
#endif // JU_64BIT
349
305
for (subexp = 0; subexp < cJU_NUMSUBEXPL; ++subexp)
351
jpcount = __JudyCountBitsL(JU_JLB_BITMAP(Pjlb, subexp));
307
jpcount = j__udyCountBitsL(JU_JLB_BITMAP(Pjlb, subexp));
354
__JudyLFreeJV(JL_JLB_PVALUE(Pjlb, subexp), jpcount, Pjpm);
310
j__udyLFreeJV(JL_JLB_PVALUE(Pjlb, subexp), jpcount, Pjpm);
358
__JudyFreeJLB1((Pjlb_t) (Pjp->jp_Addr), Pjpm);
314
j__udyFreeJLB1((Pjlb_t) (Pjp->jp_Addr), Pjpm);
361
317
} // case cJU_JPLEAF_B1
375
331
case cJU_JPIMMED_1_06:
376
332
case cJU_JPIMMED_1_07:
378
Pop1 = Pjp->jp_Type - cJU_JPIMMED_1_02 + 2;
379
__JudyLFreeJV((Pjv_t) (Pjp->jp_Addr), Pop1, Pjpm);
334
Pop1 = JU_JPTYPE(Pjp) - cJU_JPIMMED_1_02 + 2;
335
j__udyLFreeJV((Pjv_t) (Pjp->jp_Addr), Pop1, Pjpm);
383
339
case cJU_JPIMMED_2_02:
384
340
case cJU_JPIMMED_2_03:
386
Pop1 = Pjp->jp_Type - cJU_JPIMMED_2_02 + 2;
387
__JudyLFreeJV((Pjv_t) (Pjp->jp_Addr), Pop1, Pjpm);
342
Pop1 = JU_JPTYPE(Pjp) - cJU_JPIMMED_2_02 + 2;
343
j__udyLFreeJV((Pjv_t) (Pjp->jp_Addr), Pop1, Pjpm);
390
346
case cJU_JPIMMED_3_02:
391
__JudyLFreeJV((Pjv_t) (Pjp->jp_Addr), 2, Pjpm);
347
j__udyLFreeJV((Pjv_t) (Pjp->jp_Addr), 2, Pjpm);
394
350
#endif // JU_64BIT