allow compilation without GC via `NO_GC`

master
Drake 2 years ago
parent d207a91c3f
commit abe5205aa0

@ -5,7 +5,7 @@ CC_FLAGS := ${CC_FLAGS} -Ilib
DESTDIR ?= /
ifndef NO_GC
CC_FLAGS := ${CC_FLAGS} -lgc
CC_FLAGS := ${CC_FLAGS} -lgc -DNO_GC=1
endif
.PHONY: all debug clean build-release install

Binary file not shown.

@ -1,9 +1,13 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
#else
#include <stdlib.h>
#endif
char* readFile(char* path) {
path++;
@ -23,7 +27,7 @@ char* readFile(char* path) {
int length = ftell(fp);
rewind(fp);
buf = (char*)GC_MALLOC(sizeof(char) * (length + 1));
buf = (char*)malloc(sizeof(char) * (length + 1));
fread(buf, sizeof(char), length, fp);
buf[length] = '\0';

@ -1,14 +1,18 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
#else
#include <stdlib.h>
#endif
#include "color.h"
void info(char* ustr, ...) {
char* tstr = GRN "INFO: " CRESET;
char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1);
char* str = malloc(strlen(tstr) + strlen(ustr) + 1);
sprintf(str, "%s%s", tstr, ustr);
//strcat(str, ustr);
@ -19,7 +23,7 @@ void info(char* ustr, ...) {
void warn(char* ustr, ...) {
char* tstr = YEL "WARN: " CRESET;
char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1);
char* str = malloc(strlen(tstr) + strlen(ustr) + 1);
sprintf(str, "%s%s", tstr, ustr);
va_list lst;
@ -29,7 +33,7 @@ void warn(char* ustr, ...) {
void error(char* ustr, ...) {
char* tstr = RED "ERR: " CRESET;
char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1);
char* str = malloc(strlen(tstr) + strlen(ustr) + 1);
sprintf(str, "%s%s", tstr, ustr);
va_list lst;
@ -39,7 +43,7 @@ void error(char* ustr, ...) {
void reqlog(char* ustr, ...) {
char* tstr = WHT "REQ: " CRESET;
char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1);
char* str = malloc(strlen(tstr) + strlen(ustr) + 1);
sprintf(str, "%s%s", tstr, ustr);
va_list lst;

@ -1,7 +1,12 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#ifdef NO_GC
#include <gc.h>
#else
#warning GC is disabled. There *will* be memory leaks all over the place!
#endif
#include "srv.c"
@ -18,7 +23,9 @@ void help(char* exe) {
}
int main(int argc, char** argv) {
#ifdef NO_GC
GC_INIT();
#endif
char* ADDR = DEFAULT_ADDR;
int PORT = DEFAULT_PORT;
@ -43,7 +50,7 @@ int main(int argc, char** argv) {
printf("%s\n", longopt);
if (!strcmp(longopt, "help")) {
help(argv[0]);
exit(0);
_exit(0);
} else if (!strcmp(longopt, "addr")) {
ADDR = optarg;
} else if (!strcmp(longopt, "port")) {
@ -63,7 +70,7 @@ int main(int argc, char** argv) {
break;
case 'h':
help(argv[0]);
exit(0);
_exit(0);
}
}

@ -6,9 +6,13 @@
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
#else
#include <stdlib.h>
#endif
#include "resHeader.h"
#include "reqHeader.h"
@ -18,8 +22,10 @@
void safeExit(int sig) {
info("Exiting...\n");
#ifdef NO_GC
GC_gcollect();
exit(0);
#endif
_exit(0);
}
int srv(char* ADDR, int PORT) {
@ -104,7 +110,7 @@ funy: {
reqlog("%d %s\n", rawHeader.status, request.path);
char tmpHeader[1024];
makeHeader(tmpHeader, &rawHeader);
char* resHeader = GC_MALLOC(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!";
char* resHeader = malloc(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!";
sprintf(resHeader, "%s%s", (char*)tmpHeader, file);
send(client, resHeader, strlen(resHeader), 0);
close(client);

Loading…
Cancel
Save