projects
/
u
/
mdw
/
catacomb
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
24e1e86
)
Fix bug. Improve diagnostic capabilities.
author
mdw
<mdw>
Sun, 21 Nov 1999 22:14:19 +0000
(22:14 +0000)
committer
mdw
<mdw>
Sun, 21 Nov 1999 22:14:19 +0000
(22:14 +0000)
mparena.c
patch
|
blob
|
blame
|
history
diff --git
a/mparena.c
b/mparena.c
index
4e02454
..
e40a6a6
100644
(file)
--- a/
mparena.c
+++ b/
mparena.c
@@
-1,6
+1,6
@@
/* -*-c-*-
*
/* -*-c-*-
*
- * $Id: mparena.c,v 1.
1 1999/11/17 18:02:16
mdw Exp $
+ * $Id: mparena.c,v 1.
2 1999/11/21 22:14:19
mdw Exp $
*
* Allocation and freeing of MP buffers
*
*
* Allocation and freeing of MP buffers
*
@@
-30,6
+30,9
@@
/*----- Revision history --------------------------------------------------*
*
* $Log: mparena.c,v $
/*----- Revision history --------------------------------------------------*
*
* $Log: mparena.c,v $
+ * Revision 1.2 1999/11/21 22:14:19 mdw
+ * Fix bug. Improve diagnostic capabilities.
+ *
* Revision 1.1 1999/11/17 18:02:16 mdw
* New multiprecision integer arithmetic suite.
*
* Revision 1.1 1999/11/17 18:02:16 mdw
* New multiprecision integer arithmetic suite.
*
@@
-46,6
+49,12
@@
#include "mparena.h"
#include "mparena.h"
+/*----- Tweakables --------------------------------------------------------*/
+
+/* #define MPARENA_TRIVIAL */
+
+/* #define MPARENA_DEBUG "mparena.out" */
+
/*----- Default allocator -------------------------------------------------*/
static void *defalloc(mparena *a, size_t sz) { return xmalloc(sz); }
/*----- Default allocator -------------------------------------------------*/
static void *defalloc(mparena *a, size_t sz) { return xmalloc(sz); }
@@
-55,6
+64,20
@@
mparena_ops mparena_defops = { defalloc, deffree };
/*----- Static variables --------------------------------------------------*/
/*----- Static variables --------------------------------------------------*/
+#ifdef MPARENA_DEBUG
+ static FILE *debugfp = 0;
+
+# define MPARENA_OPENFILE do { \
+ if (!debugfp) { \
+ if ((debugfp = fopen(MPARENA_DEBUG, "w")) == 0) { \
+ fprintf(stderr, "couldn't open debug output file\n"); \
+ exit(EXIT_FAILURE); \
+ } \
+ } \
+ } while (0)
+
+#endif
+
static mparena arena = { 0, &mparena_defops };
#define MPARENA_RESOLVE(a) do { \
static mparena arena = { 0, &mparena_defops };
#define MPARENA_RESOLVE(a) do { \
@@
-73,19
+96,23
@@
static mparena arena = { 0, &mparena_defops };
* Use: Recursively dumps out the allocation tree.
*/
* Use: Recursively dumps out the allocation tree.
*/
+#ifdef MPARENA_DEBUG
+
static void tdump(mparena_node *n)
{
if (!n)
static void tdump(mparena_node *n)
{
if (!n)
- putc
har('*'
);
+ putc
('*', debugfp
);
else {
else {
- putc
har('('
);
+ putc
('(', debugfp
);
tdump(n->left);
tdump(n->left);
-
printf(
", %u, ", n->v[0]);
+
fprintf(debugfp,
", %u, ", n->v[0]);
tdump(n->right);
tdump(n->right);
- putc
har(')'
);
+ putc
(')', debugfp
);
}
}
}
}
+#endif
+
/* --- @mparena_create@ --- *
*
* Arguments: @mparena *a@ = pointer to arena block
/* --- @mparena_create@ --- *
*
* Arguments: @mparena *a@ = pointer to arena block
@@
-160,6
+187,16
@@
void mparena_destroy(mparena *a)
* digits.
*/
* digits.
*/
+#ifdef MPARENA_TRIVIAL
+
+mpw *mpalloc(mparena *a, size_t sz)
+{
+ MPARENA_RESOLVE(a);
+ return (a->ops->alloc(a, MPWS(sz)));
+}
+
+#else
+
mpw *mpalloc(mparena *a, size_t sz)
{
mparena_node **nn, *n;
mpw *mpalloc(mparena *a, size_t sz)
{
mparena_node **nn, *n;
@@
-168,9
+205,12
@@
mpw *mpalloc(mparena *a, size_t sz)
MPARENA_RESOLVE(a);
nn = &a->root;
MPARENA_RESOLVE(a);
nn = &a->root;
-#ifdef notdef
- printf("*** alloc %u\n", sz);
- tdump(a->root); putchar('\n');
+#ifdef MPARENA_DEBUG
+ MPARENA_OPENFILE;
+ fprintf(debugfp, "alloc %u\n before: ", sz);
+ tdump(a->root); putc('\n', debugfp);
+ if (sz == 0)
+ asm("nop");
#endif
/* --- First, find a block which is big enough --- */
#endif
/* --- First, find a block which is big enough --- */
@@
-178,6
+218,9
@@
mpw *mpalloc(mparena *a, size_t sz)
again:
n = *nn;
if (!n) {
again:
n = *nn;
if (!n) {
+#ifdef MPARENA_DEBUG
+ fputs(" failed\n", debugfp);
+#endif
v = a->ops->alloc(a, MPWS(sz + 1));
v[0] = sz;
return (v + 1);
v = a->ops->alloc(a, MPWS(sz + 1));
v[0] = sz;
return (v + 1);
@@
-196,7
+239,7
@@
again:
/* --- If the block we've got is still too large, start digging --- */
/* --- If the block we've got is still too large, start digging --- */
- if (n->v[0] >
=
sz * 2) {
+ if (n->v[0] > sz * 2) {
nn = &n->left;
goto again;
}
nn = &n->left;
goto again;
}
@@
-219,12
+262,19
@@
again:
p->left = left;
}
p->left = left;
}
+#ifdef MPARENA_DEBUG
+ fputs(" after: ", debugfp);
+ tdump(a->root); putc('\n', debugfp);
+#endif
+
/* --- Get rid of this node now --- */
DESTROY(n);
return (v + 1);
}
/* --- Get rid of this node now --- */
DESTROY(n);
return (v + 1);
}
+#endif
+
/* --- @mpfree@ --- *
*
* Arguments: @mparena *a@ = pointer to arena block
/* --- @mpfree@ --- *
*
* Arguments: @mparena *a@ = pointer to arena block
@@
-236,14
+286,30
@@
again:
* returned to the arena from which it was allocated.
*/
* returned to the arena from which it was allocated.
*/
+#ifdef MPARENA_TRIVIAL
+
+void mpfree(mparena *a, mpw *v)
+{
+ MPARENA_RESOLVE(a);
+ a->ops->free(a, v);
+}
+
+#else
+
void mpfree(mparena *a, mpw *v)
{
mparena_node **nn, *n;
size_t sz = *--v;
MPARENA_RESOLVE(a);
void mpfree(mparena *a, mpw *v)
{
mparena_node **nn, *n;
size_t sz = *--v;
MPARENA_RESOLVE(a);
- nn = &a->root;
+#ifdef MPARENA_DEBUG
+ MPARENA_OPENFILE;
+ fprintf(debugfp, "free %u\n before: ", sz);
+ tdump(a->root); putc('\n', debugfp);
+#endif
+
+ nn = &a->root;
while (*nn) {
n = *nn;
if (n->v[0] > sz)
while (*nn) {
n = *nn;
if (n->v[0] > sz)
@@
-257,10
+323,12
@@
void mpfree(mparena *a, mpw *v)
n->v = v;
*nn = n;
n->v = v;
*nn = n;
-#ifdef
notdef
-
printf("*** free %u\n", sz
);
- tdump(a->root); putc
har('\n'
);
+#ifdef
MPARENA_DEBUG
+
fputs(" after: ", debugfp
);
+ tdump(a->root); putc
('\n', debugfp
);
#endif
}
#endif
}
+#endif
+
/*----- That's all, folks -------------------------------------------------*/
/*----- That's all, folks -------------------------------------------------*/