Merge pull request #4 from thequux/master

All sorts of makefile hacks, plus a new fastpath for the bitreader
This commit is contained in:
Meredith L. Patterson 2012-04-30 19:40:39 -07:00
commit 530eed8df3
8 changed files with 172 additions and 46 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@
*~
*.a
src/test_suite
lib/hush

22
Makefile Normal file
View file

@ -0,0 +1,22 @@
# Most of this is so that you can do
# $ make all
# and kick off a recursive make
# Also, "make src/all" turns into "make -C src all"
SUBDIRS = src \
lib
.DEFAULT_GOAL := all
%:
+for dir in $(SUBDIRS); do $(MAKE) -C $${dir} $@; done
define SUBDIR_TEMPLATE
$(1)/%:
$$(MAKE) -C $(1) $$*
endef
$(foreach dir,$(SUBDIRS),$(eval $(call SUBDIR_TEMPLATE,$(dir))))
#.DEFAULT:
# $(if $(findstring ./,$(dir $@)),$(error No rule to make target `$@'),$(MAKE) -C $(dir $@) $(notdir $@))

View file

@ -1,19 +1,36 @@
CFLAGS := $(shell pkg-config --cflags glib-2.0) -std=gnu99
CFLAGS := $(shell pkg-config --cflags glib-2.0) -std=gnu99 -Wall -Wextra -Werror -Wno-unused-parameter
LDFLAGS := $(shell pkg-config --libs glib-2.0)
CC := gcc
# Set V=1 for verbose mode...
V := 0
CFLAGS += -DINCLUDE_TESTS
HUSH = $(TOPLEVEL)/lib/hush
.SUFFIX:
# Check to make sure variables are properly set
ifeq ($(TOPLEVEL),)
$(error $$TOPLEVEL is unset)
endif
ifsilent = $(if $(findstring 0, $(V)),$(1),)
hush = $(call ifsilent,$(HUSH) $(1))
#.SUFFIXES:
ifeq ($(V),0)
.SILENT:
endif
.DEFAULT_GOAL:=all
%.a:
%.a: $(call ifsilent,| $(HUSH))
-rm -f $@
ar crv $@ $^
$(call hush,"Archiving $@") ar cr $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
%.o: %.c $(call ifsilent,| $(HUSH))
$(call hush, "Compiling $<") $(CC) $(CFLAGS) -c -o $@ $<
clean:
-rm -f $(OUTPUTS)
$(TOPLEVEL)/lib/hush: $(TOPLEVEL)/lib/hush.c
make -C $(TOPLEVEL)/lib hush

View file

@ -1,7 +1,12 @@
include ../common.mk
OUTPUTS := \
allocator.o
TOPLEVEL := ../
include ../common.mk
all: allocator.o
hush: hush.c
$(CC) $(CFLAGS) -o $@ $<

64
lib/hush.c Normal file
View file

@ -0,0 +1,64 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
int main (int argc, char** argv) {
// Argv[1] is the message
// argv[2] is the name of the child
// the rest are passed to the child
if (argc < 3) return 1;
printf ("\x1b[1;32m*\x1b[0m %s...", argv[1]);
char cbuf[4096];
argc-=2;
argv+=1;
for (int i = 0; i < argc; i++)
argv[i] = argv[i+1];
argv[argc] = NULL;
int fd[2];
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fd) == -1) {
printf ("\n");
execvp(argv[0], argv);
}
int pid;
if ((pid=fork()) == -1) {
printf ("\n");
execvp(argv[0], argv);
} else if (pid == 0) { // child
printf ("Child");
close(fd[1]);
dup2(fd[0],0);
dup2(0,1);
dup2(0,2);
execvp(argv[0], argv);
} else { // parent
char* cols1 = getenv("COLUMNS");
int cols;
if (cols1) { cols = atoi(cols1); } else { cols = 80; }
close(fd[0]);
int delta = 1;
int ct = 0;
while (delta != 0) {
delta = read (fd[1], cbuf+ct, 4096-ct);
ct+= delta;
}
cbuf[ct] = 0;
int status;
wait(&status);
fflush (NULL);
if (status) {
fprintf (stderr, "\x1b[%dG\x1b[1;34m[\x1b[1;31m!!\x1b[1;34m]\x1b[0m\n", cols-4);
} else if (ct) {
fprintf (stderr, "\x1b[%dG\x1b[1;34m[\x1b[0;33mWW\x1b[1;34m]\x1b[0m\n", cols-4);
} else {
fprintf (stderr, "\x1b[%dG\x1b[1;34m[\x1b[1;32mOK\x1b[1;34m]\x1b[0m\n", cols-4);
}
fflush (NULL);
printf ("%s", cbuf);
fflush (NULL);
return WEXITSTATUS(status);
}
}

View file

@ -1,14 +1,18 @@
-include ../common.mk
OUTPUTS := bitreader.o \
hammer.o \
libhammer.a \
test_suite
TOPLEVEL := ../
include ../common.mk
all: libhammer.a test_suite
test_suite: test_suite.o libhammer.a
$(CC) -o $@ $^ $(LDFLAGS)
$(call hush, "Linking $@") $(CC) -o $@ $^ $(LDFLAGS)
libhammer.a: bitreader.o hammer.o

View file

@ -12,6 +12,18 @@ long long read_bits(input_stream_t* state, int count, char signed_p) {
long long out = 0;
int offset = 0;
long long msb = (!!signed_p) << (count - 1); // 0 if unsigned, else 1 << (nbits - 1)
if ((state->bit_offset & 0x7) == 0 && (count & 0x7) == 0) {
// fast path
if (state->endianness & BYTE_BIG_ENDIAN) {
while (count > 0)
out = (out << 8) | state->input[state->index++];
} else {
while (count > 0) {
count -= 8;
out |= state->input[state->index++] << count;
}
}
} else {
while (count) {
int segment, segment_len;
// Read a segment...
@ -48,6 +60,7 @@ long long read_bits(input_stream_t* state, int count, char signed_p) {
}
count -= segment_len;
}
}
return (out ^ msb) - msb; // perform sign extension
}
@ -55,7 +68,7 @@ long long read_bits(input_stream_t* state, int count, char signed_p) {
#define MK_INPUT_STREAM(buf,len,endianness_) \
{ \
.input = buf, \
.input = (uint8_t*)buf, \
.length = len, \
.index = 0, \
.bit_offset = (((endianness_) & BIT_BIG_ENDIAN) ? 8 : 0), \

View file

@ -122,7 +122,7 @@ const parser_t* token(const uint8_t *str, const size_t len) {
}
static parse_result_t* parse_ch(void* env, parse_state_t *state) {
uint8_t c = (uint8_t)env;
uint8_t c = (uint8_t)GPOINTER_TO_UINT(env);
uint8_t r = (uint8_t)read_bits(&state->input_stream, 8, false);
if (c == r) {
parsed_token_t *tok = g_new(parsed_token_t, 1);
@ -135,7 +135,7 @@ static parse_result_t* parse_ch(void* env, parse_state_t *state) {
const parser_t* ch(const uint8_t c) {
parser_t *ret = g_new(parser_t, 1);
ret->fn = parse_ch; ret->env = (void*)c;
ret->fn = parse_ch; ret->env = GUINT_TO_POINTER(c);
return (const parser_t*)ret;
}