Merge pull request #4 from thequux/master
All sorts of makefile hacks, plus a new fastpath for the bitreader
This commit is contained in:
commit
530eed8df3
8 changed files with 172 additions and 46 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -2,3 +2,4 @@
|
||||||
*~
|
*~
|
||||||
*.a
|
*.a
|
||||||
src/test_suite
|
src/test_suite
|
||||||
|
lib/hush
|
||||||
|
|
|
||||||
22
Makefile
Normal file
22
Makefile
Normal 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 $@))
|
||||||
31
common.mk
31
common.mk
|
|
@ -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)
|
LDFLAGS := $(shell pkg-config --libs glib-2.0)
|
||||||
CC := gcc
|
CC := gcc
|
||||||
|
# Set V=1 for verbose mode...
|
||||||
|
V := 0
|
||||||
CFLAGS += -DINCLUDE_TESTS
|
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
|
.DEFAULT_GOAL:=all
|
||||||
|
|
||||||
%.a:
|
%.a: $(call ifsilent,| $(HUSH))
|
||||||
-rm -f $@
|
-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:
|
clean:
|
||||||
-rm -f $(OUTPUTS)
|
-rm -f $(OUTPUTS)
|
||||||
|
|
||||||
|
$(TOPLEVEL)/lib/hush: $(TOPLEVEL)/lib/hush.c
|
||||||
|
make -C $(TOPLEVEL)/lib hush
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
include ../common.mk
|
|
||||||
|
|
||||||
OUTPUTS := \
|
OUTPUTS := \
|
||||||
allocator.o
|
allocator.o
|
||||||
|
|
||||||
|
TOPLEVEL := ../
|
||||||
|
|
||||||
|
include ../common.mk
|
||||||
|
|
||||||
all: allocator.o
|
all: allocator.o
|
||||||
|
|
||||||
|
hush: hush.c
|
||||||
|
$(CC) $(CFLAGS) -o $@ $<
|
||||||
|
|
|
||||||
64
lib/hush.c
Normal file
64
lib/hush.c
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,14 +1,18 @@
|
||||||
-include ../common.mk
|
|
||||||
|
|
||||||
OUTPUTS := bitreader.o \
|
OUTPUTS := bitreader.o \
|
||||||
hammer.o \
|
hammer.o \
|
||||||
libhammer.a \
|
libhammer.a \
|
||||||
test_suite
|
test_suite
|
||||||
|
|
||||||
|
TOPLEVEL := ../
|
||||||
|
|
||||||
|
include ../common.mk
|
||||||
|
|
||||||
|
|
||||||
all: libhammer.a test_suite
|
all: libhammer.a test_suite
|
||||||
|
|
||||||
test_suite: test_suite.o libhammer.a
|
test_suite: test_suite.o libhammer.a
|
||||||
$(CC) -o $@ $^ $(LDFLAGS)
|
$(call hush, "Linking $@") $(CC) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
libhammer.a: bitreader.o hammer.o
|
libhammer.a: bitreader.o hammer.o
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,41 +12,54 @@ long long read_bits(input_stream_t* state, int count, char signed_p) {
|
||||||
long long out = 0;
|
long long out = 0;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
long long msb = (!!signed_p) << (count - 1); // 0 if unsigned, else 1 << (nbits - 1)
|
long long msb = (!!signed_p) << (count - 1); // 0 if unsigned, else 1 << (nbits - 1)
|
||||||
while (count) {
|
if ((state->bit_offset & 0x7) == 0 && (count & 0x7) == 0) {
|
||||||
int segment, segment_len;
|
// fast path
|
||||||
// Read a segment...
|
|
||||||
if (state->endianness & BIT_BIG_ENDIAN) {
|
|
||||||
if (count >= state->bit_offset) {
|
|
||||||
segment_len = state->bit_offset;
|
|
||||||
state->bit_offset = 8;
|
|
||||||
segment = state->input[state->index] & ((1 << segment_len) - 1);
|
|
||||||
state->index++;
|
|
||||||
} else {
|
|
||||||
segment_len = count;
|
|
||||||
state->bit_offset -= count;
|
|
||||||
segment = (state->input[state->index] >> state->bit_offset) & ((1 << segment_len) - 1);
|
|
||||||
}
|
|
||||||
} else { // BIT_LITTLE_ENDIAN
|
|
||||||
if (count + state->bit_offset >= 8) {
|
|
||||||
segment_len = 8 - state->bit_offset;
|
|
||||||
segment = (state->input[state->index] >> state->bit_offset);
|
|
||||||
state->index++;
|
|
||||||
state->bit_offset = 0;
|
|
||||||
} else {
|
|
||||||
segment_len = count;
|
|
||||||
segment = (state->input[state->index] >> state->bit_offset) & ((1 << segment_len) - 1);
|
|
||||||
state->bit_offset += segment_len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// have a valid segment; time to assemble the byte
|
|
||||||
if (state->endianness & BYTE_BIG_ENDIAN) {
|
if (state->endianness & BYTE_BIG_ENDIAN) {
|
||||||
out = out << segment_len | segment;
|
while (count > 0)
|
||||||
} else { // BYTE_LITTLE_ENDIAN
|
out = (out << 8) | state->input[state->index++];
|
||||||
out |= segment << offset;
|
} else {
|
||||||
offset += segment_len;
|
while (count > 0) {
|
||||||
|
count -= 8;
|
||||||
|
out |= state->input[state->index++] << count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (count) {
|
||||||
|
int segment, segment_len;
|
||||||
|
// Read a segment...
|
||||||
|
if (state->endianness & BIT_BIG_ENDIAN) {
|
||||||
|
if (count >= state->bit_offset) {
|
||||||
|
segment_len = state->bit_offset;
|
||||||
|
state->bit_offset = 8;
|
||||||
|
segment = state->input[state->index] & ((1 << segment_len) - 1);
|
||||||
|
state->index++;
|
||||||
|
} else {
|
||||||
|
segment_len = count;
|
||||||
|
state->bit_offset -= count;
|
||||||
|
segment = (state->input[state->index] >> state->bit_offset) & ((1 << segment_len) - 1);
|
||||||
|
}
|
||||||
|
} else { // BIT_LITTLE_ENDIAN
|
||||||
|
if (count + state->bit_offset >= 8) {
|
||||||
|
segment_len = 8 - state->bit_offset;
|
||||||
|
segment = (state->input[state->index] >> state->bit_offset);
|
||||||
|
state->index++;
|
||||||
|
state->bit_offset = 0;
|
||||||
|
} else {
|
||||||
|
segment_len = count;
|
||||||
|
segment = (state->input[state->index] >> state->bit_offset) & ((1 << segment_len) - 1);
|
||||||
|
state->bit_offset += segment_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// have a valid segment; time to assemble the byte
|
||||||
|
if (state->endianness & BYTE_BIG_ENDIAN) {
|
||||||
|
out = out << segment_len | segment;
|
||||||
|
} else { // BYTE_LITTLE_ENDIAN
|
||||||
|
out |= segment << offset;
|
||||||
|
offset += segment_len;
|
||||||
|
}
|
||||||
|
count -= segment_len;
|
||||||
}
|
}
|
||||||
count -= segment_len;
|
|
||||||
}
|
}
|
||||||
return (out ^ msb) - msb; // perform sign extension
|
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_) \
|
#define MK_INPUT_STREAM(buf,len,endianness_) \
|
||||||
{ \
|
{ \
|
||||||
.input = buf, \
|
.input = (uint8_t*)buf, \
|
||||||
.length = len, \
|
.length = len, \
|
||||||
.index = 0, \
|
.index = 0, \
|
||||||
.bit_offset = (((endianness_) & BIT_BIG_ENDIAN) ? 8 : 0), \
|
.bit_offset = (((endianness_) & BIT_BIG_ENDIAN) ? 8 : 0), \
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
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);
|
uint8_t r = (uint8_t)read_bits(&state->input_stream, 8, false);
|
||||||
if (c == r) {
|
if (c == r) {
|
||||||
parsed_token_t *tok = g_new(parsed_token_t, 1);
|
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) {
|
const parser_t* ch(const uint8_t c) {
|
||||||
parser_t *ret = g_new(parser_t, 1);
|
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;
|
return (const parser_t*)ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue