forked from Orc/discount
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Add the new
amalloc
module, which (if enabled with
--enable-amalloc) replaced malloc and its ilk with versions that attempt to maintain a linked list of allocated blocks that can be printed to stderr with the `adump()` function. 2. Clean up a couple more places where I leak memory during the first level parse. 3. tweak cstring.h to try to chase down places where the C compiler might optimize EXPAND() into producing broken code.
- Loading branch information
Showing
15 changed files
with
171 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* debugging malloc()/realloc()/calloc()/free() that attempts | ||
* to keep track of just what's been allocated today. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define MAGIC 0x1f2e3d4c | ||
|
||
struct alist { int magic, size; struct alist *next, *last; }; | ||
|
||
static struct alist list = { 0, 0, 0, 0 }; | ||
|
||
void * | ||
acalloc(int size, int count) | ||
{ | ||
struct alist *ret = calloc(size + sizeof(struct alist), count); | ||
|
||
if ( ret ) { | ||
ret->magic = MAGIC; | ||
ret->size = size * count; | ||
if ( list.next ) { | ||
ret->next = list.next; | ||
ret->last = &list; | ||
ret->next->last = ret; | ||
list.next = ret; | ||
} | ||
else { | ||
ret->last = ret->next = &list; | ||
list.next = list.last = ret; | ||
} | ||
return ret+1; | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
void* | ||
amalloc(int size) | ||
{ | ||
return acalloc(size,1); | ||
} | ||
|
||
|
||
void * | ||
afree(void *ptr) | ||
{ | ||
struct alist *p2 = ((struct alist*)ptr)-1; | ||
|
||
if ( p2->magic == MAGIC ) { | ||
p2->last->next = p2->next; | ||
p2->next->last = p2->last; | ||
free(p2); | ||
} | ||
else | ||
free(ptr); | ||
} | ||
|
||
|
||
void * | ||
arealloc(void *ptr, int size) | ||
{ | ||
struct alist *p2 = ((struct alist*)ptr)-1; | ||
struct alist save; | ||
|
||
if ( p2->magic == MAGIC ) { | ||
save.next = p2->next; | ||
save.last = p2->last; | ||
p2 = realloc(p2, sizeof(p2) + size); | ||
|
||
if ( p2 ) { | ||
p2->size = size; | ||
p2->next->last = p2; | ||
p2->last->next = p2; | ||
return p2+1; | ||
} | ||
else { | ||
save.next->last = save.last; | ||
save.last->next = save.next; | ||
return 0; | ||
} | ||
} | ||
return realloc(ptr, size); | ||
} | ||
|
||
|
||
void | ||
adump() | ||
{ | ||
struct alist *p; | ||
|
||
|
||
for ( p = list.next; p && (p != &list); p = p->next ) { | ||
fprintf(stderr, "allocated: %d byte%s\n", p->size, (p->size==1) ? "" : "s"); | ||
fprintf(stderr, " [%.*s]\n", p->size, p+1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* debugging malloc()/realloc()/calloc()/free() that attempts | ||
* to keep track of just what's been allocated today. | ||
*/ | ||
#ifndef AMALLOC_D | ||
#define AMALLOC_D | ||
|
||
#include "config.h" | ||
|
||
#ifdef USE_AMALLOC | ||
|
||
extern void *amalloc(int); | ||
extern void *acalloc(int,int); | ||
extern void *arealloc(void*,int); | ||
extern void afree(void*); | ||
extern void adump(); | ||
|
||
#define malloc amalloc | ||
#define calloc acalloc | ||
#define realloc arealloc | ||
#define free afree | ||
|
||
#else | ||
|
||
#define adump() (void)1 | ||
|
||
#endif | ||
|
||
#endif/*AMALLOC_D*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
|
||
#include "mkdio.h" | ||
#include "cstring.h" | ||
#include "amalloc.h" | ||
|
||
char *pgm = "mkd2html"; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
|
||
#include "cstring.h" | ||
#include "markdown.h" | ||
#include "amalloc.h" | ||
|
||
typedef ANCHOR(Line) LineAnchor; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
#define _MKDIO_D | ||
|
||
#include <stdio.h> | ||
#include "amalloc.h" | ||
|
||
typedef void MMIOT; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
|
||
#include "mkdio.h" | ||
#include "cstring.h" | ||
#include "amalloc.h" | ||
|
||
char *pgm = "theme"; | ||
char *output = 0; | ||
|