Yikes! I've only just noticed that this copy of tree234.c was still

using unwrappered malloc/free, leaving plenty of openings for out-
of-memory segfaults. Switch to using Puzzles's memory management,
which I should have done right at the start but can only assume I
forgot about.

[originally from svn r6388]
This commit is contained in:
Simon Tatham
2005-10-12 17:57:40 +00:00
parent 7e57366a57
commit 2bef4dfb50

View File

@ -31,10 +31,7 @@
#include "tree234.h" #include "tree234.h"
#define smalloc malloc #include "puzzles.h" /* for smalloc/sfree */
#define sfree free
#define mknew(typ) ( (typ *) smalloc (sizeof (typ)) )
#ifdef TEST #ifdef TEST
#define LOG(x) (printf x) #define LOG(x) (printf x)
@ -60,7 +57,7 @@ struct node234_Tag {
* Create a 2-3-4 tree. * Create a 2-3-4 tree.
*/ */
tree234 *newtree234(cmpfn234 cmp) { tree234 *newtree234(cmpfn234 cmp) {
tree234 *ret = mknew(tree234); tree234 *ret = snew(tree234);
LOG(("created tree %p\n", ret)); LOG(("created tree %p\n", ret));
ret->root = NULL; ret->root = NULL;
ret->cmp = cmp; ret->cmp = cmp;
@ -187,7 +184,7 @@ static int add234_insert(node234 *left, void *e, node234 *right,
LOG((" done\n")); LOG((" done\n"));
break; break;
} else { } else {
node234 *m = mknew(node234); node234 *m = snew(node234);
m->parent = n->parent; m->parent = n->parent;
LOG((" splitting a 4-node; created new node %p\n", m)); LOG((" splitting a 4-node; created new node %p\n", m));
/* /*
@ -283,7 +280,7 @@ static int add234_insert(node234 *left, void *e, node234 *right,
return 0; /* root unchanged */ return 0; /* root unchanged */
} else { } else {
LOG((" root is overloaded, split into two\n")); LOG((" root is overloaded, split into two\n"));
(*root) = mknew(node234); (*root) = snew(node234);
(*root)->kids[0] = left; (*root)->counts[0] = lcount; (*root)->kids[0] = left; (*root)->counts[0] = lcount;
(*root)->elems[0] = e; (*root)->elems[0] = e;
(*root)->kids[1] = right; (*root)->counts[1] = rcount; (*root)->kids[1] = right; (*root)->counts[1] = rcount;
@ -314,7 +311,7 @@ static void *add234_internal(tree234 *t, void *e, int index) {
LOG(("adding element \"%s\" to tree %p\n", e, t)); LOG(("adding element \"%s\" to tree %p\n", e, t));
if (t->root == NULL) { if (t->root == NULL) {
t->root = mknew(node234); t->root = snew(node234);
t->root->elems[1] = t->root->elems[2] = NULL; t->root->elems[1] = t->root->elems[2] = NULL;
t->root->kids[0] = t->root->kids[1] = NULL; t->root->kids[0] = t->root->kids[1] = NULL;
t->root->kids[2] = t->root->kids[3] = NULL; t->root->kids[2] = t->root->kids[3] = NULL;
@ -1040,7 +1037,7 @@ static node234 *join234_internal(node234 *left, void *sep,
* nodes. * nodes.
*/ */
node234 *newroot; node234 *newroot;
newroot = mknew(node234); newroot = snew(node234);
newroot->kids[0] = left; newroot->counts[0] = countnode234(left); newroot->kids[0] = left; newroot->counts[0] = countnode234(left);
newroot->elems[0] = sep; newroot->elems[0] = sep;
newroot->kids[1] = right; newroot->counts[1] = countnode234(right); newroot->kids[1] = right; newroot->counts[1] = countnode234(right);
@ -1216,7 +1213,7 @@ static node234 *split234_internal(tree234 *t, int index) {
* new node pointers in halves[0] and halves[1], and go up * new node pointers in halves[0] and halves[1], and go up
* a level. * a level.
*/ */
sib = mknew(node234); sib = snew(node234);
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
if (i+ki < 3 && n->elems[i+ki]) { if (i+ki < 3 && n->elems[i+ki]) {
sib->elems[i] = n->elems[i+ki]; sib->elems[i] = n->elems[i+ki];
@ -1416,7 +1413,7 @@ tree234 *split234(tree234 *t, void *e, cmpfn234 cmp, int rel) {
static node234 *copynode234(node234 *n, copyfn234 copyfn, void *copyfnstate) { static node234 *copynode234(node234 *n, copyfn234 copyfn, void *copyfnstate) {
int i; int i;
node234 *n2 = mknew(node234); node234 *n2 = snew(node234);
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
if (n->elems[i] && copyfn) if (n->elems[i] && copyfn)