Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tadd committed Oct 19, 2024
1 parent bc558d6 commit 686dcb6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC=gcc
CFLAGS=-Wall -Wextra -O3 $(XCFLAGS)
CFLAGS=-Wall -Wextra -O3 -fPIC $(XCFLAGS) -DPOSIXLY_CORRUPT
#CFLAGS=-Wall -Wextra -ggdb3 -O $(XCFLAGS)
ANALYZE=-fanalyzer
LIB=libscary.so
Expand All @@ -16,7 +16,7 @@ testlib.o scary.o: scary.h
$(CC) $(CFLAGS) -S -c $<

%.so:
$(CC) $(CFLAGS) -shared -fPIC $^ -o $@
$(CC) $(CFLAGS) -shared $^ -o $@

test: testlib
env LD_LIBRARY_PATH=. ./$<
Expand Down
53 changes: 52 additions & 1 deletion scary.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifdef POSIXLY_CORRUPT
#include <unistd.h>
#include <sys/types.h>
#endif

#include "scary.h"

Expand All @@ -23,16 +28,24 @@ static inline void *xmalloc(size_t size)
return p;
}

#ifndef POSIXLY_CORRUPT
static inline void *xrealloc(void *p0, size_t size)
{
void *p;
NONNULL(p = realloc(p0, size));
return p;
}
#endif

typedef struct {
#ifdef POSIXLY_CORRUPT
FILE *stream;
size_t capacity, elem_size;
void *space;
#else
size_t capacity, length, elem_size;
uint8_t space[];
#endif
} Scary;

enum {
Expand All @@ -43,7 +56,8 @@ enum {

static inline void *opaque(Scary *a)
{
return a->space;
uint8_t *bp = (void *) a;
return bp + SCARY_OFFSET;
}

static inline Scary *get(const void *p)
Expand All @@ -54,12 +68,19 @@ static inline Scary *get(const void *p)

void *scary_new(size_t elem_size)
{
#ifdef POSIXLY_CORRUPT
Scary *ary = xmalloc(sizeof(Scary) + sizeof(void *));
ary->elem_size = elem_size;
ary->stream = open_memstream((char **) &ary->space, &ary->capacity);
return opaque(ary);
#else
size_t cap = elem_size * SCARY_INIT;
Scary *ary = xmalloc(sizeof(Scary) + cap);
ary->capacity = cap;
ary->length = 0;
ary->elem_size = elem_size;
return opaque(ary);
#endif
}

void scary_free(void *p)
Expand All @@ -71,26 +92,43 @@ static Scary *maybe_resize(void *p)
{
const void **pp = p;
Scary *ary = get(*pp);
#ifndef POSIXLY_CORRUPT
if (ary->capacity <= ary->length * ary->elem_size) {
ary->capacity *= SCARY_INC_RATIO;
ary = xrealloc(ary, sizeof(Scary) + ary->capacity);
*pp = opaque(ary);
}
#endif
return ary;
}

size_t scary_length(const void *p)
{
#ifdef POSIXLY_CORRUPT
const Scary *ary = get(p);
return ary->capacity / ary->elem_size;
#else
return get(p)->length;
#endif
}

#ifdef POSIXLY_CORRUPT
#define DEF_PUSH_VARIANT2(type, suffix) \
void scary_push_##suffix(type **p, type elem) \
{ \
Scary *ary = maybe_resize(p); \
fwrite(&elem, ary->elem_size, 1, ary->stream); \
fflush(ary->stream); \
}
#else
#define DEF_PUSH_VARIANT2(type, suffix) \
void scary_push_##suffix(type **p, type elem) \
{ \
Scary *ary = maybe_resize(p); \
type *sp = (type *) ary->space; \
sp[ary->length++] = elem; \
}
#endif
#define DEF_PUSH_VARIANT_T(type) DEF_PUSH_VARIANT2(type##_t, type)
#define DEF_PUSH_VARIANT1(type) DEF_PUSH_VARIANT2(type, type)
DEF_PUSH_VARIANT_T(int8)
Expand All @@ -106,8 +144,14 @@ DEF_PUSH_VARIANT1(char)
static void scary_push_ptr(void *p, const void *elem)
{
Scary *ary = maybe_resize(p);
#ifdef POSIXLY_CORRUPT
size_t n = fwrite(elem, ary->elem_size, 1, ary->stream);
fprintf(stderr, "wrote: %zu\n", n);
fflush(ary->stream);
#else
const void **sp = (const void **) ary->space;
sp[ary->length++] = elem;
#endif
}

#define DEF_PUSH_VARIANT_PTR(type, suffix) \
Expand All @@ -134,7 +178,14 @@ void scary_push_ccharp(const char ***p, const char *elem)

void scary_pop(void *p)
{
#ifdef POSIXLY_CORRUPT
Scary *ary = get(p);
fseek(ary->stream, -ary->elem_size, SEEK_CUR);
ftruncate(fileno(ary->stream), ary->capacity - ary->elem_size);
fflush(ary->stream);
#else
get(p)->length--; // do not shrink for speed
#endif
}

void *scary_dup(void *p)
Expand Down

0 comments on commit 686dcb6

Please sign in to comment.