improved memory managment

This commit is contained in:
gottox@rootkit.lan 2007-12-12 03:54:29 +01:00
parent 59037a1872
commit a6a434a400

View file

@ -9,8 +9,12 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#define BUFFERSIZE 1024 #define BUFFERSIZE 512
#define LENGTH(x) sizeof(x)/sizeof(x[0]) #define LENGTH(x) sizeof(x)/sizeof(x[0])
#define ADDC(b,i) if((i + 1) % BUFFERSIZE == 0) \
{ b = realloc(b,((i + 1)+ BUFFERSIZE) * sizeof(b)); if(!b) eprint("Malloc failed."); } \
b[i]
typedef unsigned int (*Parser)(const char *, const char *); typedef unsigned int (*Parser)(const char *, const char *);
struct Tag { struct Tag {
@ -19,6 +23,7 @@ struct Tag {
char *tag; char *tag;
}; };
void eprint(const char *format, ...); /* Prints error and exits */ void eprint(const char *format, ...); /* Prints error and exits */
void hprint(const char *begin, const char *end); /* escapes HTML and prints it to stdout*/ void hprint(const char *begin, const char *end); /* escapes HTML and prints it to stdout*/
unsigned int doamp(const char *begin, const char *end); /* Parser for & */ unsigned int doamp(const char *begin, const char *end); /* Parser for & */
@ -138,19 +143,19 @@ dolineprefix(const char *begin, const char *end) {
continue; continue;
if(strncmp(lineprefix[i].search,p+1,l)) if(strncmp(lineprefix[i].search,p+1,l))
continue; continue;
if(!(buffer = malloc(end - p+1))) if(!(buffer = malloc(BUFFERSIZE)))
eprint("Malloc failed."); eprint("Malloc failed.");
buffer[0] = '\0'; buffer[0] = '\0';
printf("\n<%s>",lineprefix[i].tag); printf("\n<%s>",lineprefix[i].tag);
for(j = 0; p != end; p++, j++) { for(j = 0; p != end; p++, j++) {
buffer[j] = *p; ADDC(buffer,j) = *p;
if(*p == '\n') { if(*p == '\n') {
if(strncmp(lineprefix[i].search,p+1,l) != 0) if(strncmp(lineprefix[i].search,p+1,l) != 0)
break; break;
p += l; p += l;
} }
} }
buffer[j] = '\0'; ADDC(buffer,j) = '\0';
if(lineprefix[i].process) if(lineprefix[i].process)
process(buffer,buffer+strlen(buffer)); process(buffer,buffer+strlen(buffer));
else else
@ -227,7 +232,7 @@ dolist(const char *begin, const char *end) {
for(p++; *p && p != end && *p == ' '; p++); for(p++; *p && p != end && *p == ' '; p++);
indent = p - q - 1; indent = p - q - 1;
if(!(buffer = malloc(end - q+1))) if(!(buffer = malloc(BUFFERSIZE)))
eprint("Malloc failed."); eprint("Malloc failed.");
puts(ul ? "<ul>" : "<ol>"); puts(ul ? "<ul>" : "<ol>");
@ -238,12 +243,12 @@ dolist(const char *begin, const char *end) {
if(*p == '\n') { if(*p == '\n') {
if(p[1] == '\n') { if(p[1] == '\n') {
run = 0; run = 0;
buffer[i++] = '\n'; ADDC(buffer,i++) = '\n';
p++; p++;
} }
if(p[1] == ' ') { if(p[1] == ' ') {
run = 1; run = 1;
buffer[i++] = '\n'; ADDC(buffer,i++) = '\n';
p += indent + 1; p += indent + 1;
} }
else if(p[1] >= '0' && p[1] <= '9' || strchr("+-*",p[1])) { else if(p[1] >= '0' && p[1] <= '9' || strchr("+-*",p[1])) {
@ -254,7 +259,7 @@ dolist(const char *begin, const char *end) {
else if(run == 0) else if(run == 0)
break; break;
} }
buffer[i] = *p; ADDC(buffer,i) = *p;
} }
buffer[i] = '\0'; buffer[i] = '\0';
while(buffer[--i] == '\n') buffer[i] = '\0'; while(buffer[--i] == '\n') buffer[i] = '\0';