From 7d652f9c5ad285b4461fcacf0818bb9179f794e8 Mon Sep 17 00:00:00 2001 From: JeffBezanson Date: Thu, 25 Feb 2010 04:37:33 +0000 Subject: [PATCH] some LLT cleanup: making allocation functions customizable adding ios_vprintf simplifying config variables for mac --- llt/Makefile | 5 ++++- llt/config.h | 15 --------------- llt/dirpath.c | 2 +- llt/dtypes.h | 16 +++++++++++++++- llt/dump.c | 1 + llt/hashing.c | 1 + llt/htable.c | 6 +++--- llt/ieee754.h | 22 +++++++++------------- llt/ios.c | 47 +++++++++++++++++++++++++---------------------- llt/ios.h | 1 + llt/llt.h | 1 + llt/random.c | 1 - llt/socket.c | 2 +- llt/timefuncs.c | 2 +- llt/utf8.h | 2 +- 15 files changed, 64 insertions(+), 60 deletions(-) delete mode 100644 llt/config.h diff --git a/llt/Makefile b/llt/Makefile index a8efc38..bae07e9 100644 --- a/llt/Makefile +++ b/llt/Makefile @@ -10,7 +10,10 @@ TARGET = libllt.a TESTSRC = unittest.c TESTER = llttest -FLAGS = -Wall -Wno-strict-aliasing $(CFLAGS) +# OS flags: LINUX, WIN32, MACOSX +# architecture flags: __CPU__=xxx, BITS64, ARCH_X86, ARCH_X86_64 +CONFIG = -DLINUX -DARCH_X86 -D__CPU__=586 +FLAGS = -Wall -Wno-strict-aliasing $(CFLAGS) $(CONFIG) LIBS = DEBUGFLAGS = -g -DDEBUG $(FLAGS) diff --git a/llt/config.h b/llt/config.h deleted file mode 100644 index e10681c..0000000 --- a/llt/config.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef __CONFIG_H_ -#define __CONFIG_H_ - -#define LINUX -#undef WIN32 - -#undef BITS64 - -#define ARCH_X86 -#undef ARCH_X86_64 - -#define __CPU__ 586 - - -#endif diff --git a/llt/dirpath.c b/llt/dirpath.c index cfe6063..cba9201 100644 --- a/llt/dirpath.c +++ b/llt/dirpath.c @@ -97,7 +97,7 @@ char *get_exename(char *buf, size_t size) return buf; } -#elif defined(MACOSX) || defined(MACINTEL) +#elif defined(MACOSX) #include "/Developer/Headers/FlatCarbon/Processes.h" #include "/Developer/Headers/FlatCarbon/Files.h" char *get_exename(char *buf, size_t size) diff --git a/llt/dtypes.h b/llt/dtypes.h index 586fdd4..04d46dd 100644 --- a/llt/dtypes.h +++ b/llt/dtypes.h @@ -16,7 +16,18 @@ We assume the LP64 convention for 64-bit platforms. */ -#include "config.h" +#if 0 +// boehm GC allocator +#include +#define LLT_ALLOC(n) GC_MALLOC(n) +#define LLT_REALLOC(p,n) GC_REALLOC((p),(n)) +#define LLT_FREE(x) ((void)(x)) +#else +// standard allocator +#define LLT_ALLOC(n) malloc(n) +#define LLT_REALLOC(p,n) realloc((p),(n)) +#define LLT_FREE(x) free(x) +#endif typedef int bool_t; @@ -98,6 +109,9 @@ typedef u_ptrint_t uptrint_t; #define S64_MAX 9223372036854775807LL #define S64_MIN (-S64_MAX - 1LL) #define BIT63 0x8000000000000000LL +#define U32_MAX 4294967295L +#define S32_MAX 2147483647L +#define S32_MIN (-S32_MAX - 1L) #define BIT31 0x80000000 #define DBL_EPSILON 2.2204460492503131e-16 diff --git a/llt/dump.c b/llt/dump.c index d50bfb7..34b00c1 100644 --- a/llt/dump.c +++ b/llt/dump.c @@ -1,4 +1,5 @@ #include +#include #include "dtypes.h" #include "ios.h" #include "utils.h" diff --git a/llt/hashing.c b/llt/hashing.c index 28ea97a..555cab7 100644 --- a/llt/hashing.c +++ b/llt/hashing.c @@ -3,6 +3,7 @@ */ #include #include +#include #include #include "ieee754.h" #include "dtypes.h" diff --git a/llt/htable.c b/llt/htable.c index 8cb2068..2857ade 100644 --- a/llt/htable.c +++ b/llt/htable.c @@ -23,7 +23,7 @@ htable_t *htable_new(htable_t *h, size_t size) size *= 2; // 2 pointers per key/value pair size *= 2; // aim for 50% occupancy h->size = size; - h->table = (void**)malloc(size*sizeof(void*)); + h->table = (void**)LLT_ALLOC(size*sizeof(void*)); } if (h->table == NULL) return NULL; size_t i; @@ -35,7 +35,7 @@ htable_t *htable_new(htable_t *h, size_t size) void htable_free(htable_t *h) { if (h->table != &h->_space[0]) - free(h->table); + LLT_FREE(h->table); } // empty and reduce size @@ -44,7 +44,7 @@ void htable_reset(htable_t *h, size_t sz) sz = nextipow2(sz); if (h->size > sz*4 && h->size > HT_N_INLINE) { size_t newsz = sz*4; - void **newtab = (void**)realloc(h->table, newsz*sizeof(void*)); + void **newtab = (void**)LLT_REALLOC(h->table, newsz*sizeof(void*)); if (newtab == NULL) return; h->size = newsz; diff --git a/llt/ieee754.h b/llt/ieee754.h index e1c6704..17a3126 100644 --- a/llt/ieee754.h +++ b/llt/ieee754.h @@ -19,32 +19,28 @@ #ifndef _IEEE754_H #define _IEEE754_H 1 + #ifdef LINUX + #include - #include - __BEGIN_DECLS + #else + #define __LITTLE_ENDIAN 1234 #define __BIG_ENDIAN 4321 #define __PDP_ENDIAN 3412 -#endif -#ifdef MACOSX +#if defined(WIN32) || defined(ARCH_X86) || defined(ARCH_X86_64) +#define __BYTE_ORDER __LITTLE_ENDIAN +#define __FLOAT_WORD_ORDER __LITTLE_ENDIAN +#else #define __BYTE_ORDER __BIG_ENDIAN #define __FLOAT_WORD_ORDER __BIG_ENDIAN #endif -#ifdef MACINTEL -#define __BYTE_ORDER __LITTLE_ENDIAN -#define __FLOAT_WORD_ORDER __LITTLE_ENDIAN -#endif - -#ifdef WIN32 -#define __BYTE_ORDER __LITTLE_ENDIAN -#define __FLOAT_WORD_ORDER __LITTLE_ENDIAN -#endif +#endif //ifdef LINUX union ieee754_float { diff --git a/llt/ios.c b/llt/ios.c index 287b546..7f430e1 100644 --- a/llt/ios.c +++ b/llt/ios.c @@ -25,14 +25,13 @@ #include "utils.h" #include "utf8.h" #include "ios.h" -#include "socket.h" #include "timefuncs.h" #define MOST_OF(x) ((x) - ((x)>>4)) /* OS-level primitive wrappers */ -#if defined(MACOSX) || defined(MACINTEL) +#if defined(MACOSX) void *memrchr(const void *s, int c, size_t n) { const unsigned char *src = s + n; @@ -184,12 +183,12 @@ static char *_buf_realloc(ios_t *s, size_t sz) // if we own the buffer we're free to resize it // always allocate 1 bigger in case user wants to add a NUL // terminator after taking over the buffer - temp = realloc(s->buf, sz+1); + temp = LLT_REALLOC(s->buf, sz+1); if (temp == NULL) return NULL; } else { - temp = malloc(sz+1); + temp = LLT_ALLOC(sz+1); if (temp == NULL) return NULL; s->ownbuf = 1; @@ -545,7 +544,7 @@ void ios_close(ios_t *s) close(s->fd); s->fd = -1; if (s->buf!=NULL && s->ownbuf && s->buf!=&s->local[0]) - free(s->buf); + LLT_FREE(s->buf); s->buf = NULL; s->size = s->maxsize = s->bpos = 0; } @@ -571,7 +570,7 @@ char *ios_takebuf(ios_t *s, size_t *psize) ios_flush(s); if (s->buf == &s->local[0]) { - buf = malloc(s->size+1); + buf = LLT_ALLOC(s->size+1); if (buf == NULL) return NULL; if (s->size) @@ -605,7 +604,7 @@ int ios_setbuf(ios_t *s, char *buf, size_t size, int own) s->size = nvalid; if (s->buf!=NULL && s->ownbuf && s->buf!=&s->local[0]) - free(s->buf); + LLT_FREE(s->buf); s->buf = buf; s->maxsize = size; s->ownbuf = own; @@ -778,14 +777,14 @@ ios_t *ios_stderr = NULL; void ios_init_stdstreams() { - ios_stdin = malloc(sizeof(ios_t)); + ios_stdin = LLT_ALLOC(sizeof(ios_t)); ios_fd(ios_stdin, STDIN_FILENO, 0); - ios_stdout = malloc(sizeof(ios_t)); + ios_stdout = LLT_ALLOC(sizeof(ios_t)); ios_fd(ios_stdout, STDOUT_FILENO, 0); ios_stdout->bm = bm_line; - ios_stderr = malloc(sizeof(ios_t)); + ios_stderr = LLT_ALLOC(sizeof(ios_t)); ios_fd(ios_stderr, STDERR_FILENO, 0); ios_stderr->bm = bm_none; } @@ -914,24 +913,19 @@ void ios_purge(ios_t *s) } } -int ios_printf(ios_t *s, char *format, ...) +int ios_vprintf(ios_t *s, char *format, va_list args) { char *str=NULL; - va_list args; int c; - va_start(args, format); - if (s->state == bst_wr && s->bpos < s->maxsize && s->bm != bm_none) { size_t avail = s->maxsize - s->bpos; char *start = s->buf + s->bpos; c = vsnprintf(start, avail, format, args); if (c < 0) { - va_end(args); return c; } if (c < avail) { - va_end(args); s->bpos += (size_t)c; _write_update_pos(s); // TODO: only works right if newline is at end @@ -942,12 +936,21 @@ int ios_printf(ios_t *s, char *format, ...) } c = vasprintf(&str, format, args); - va_end(args); + if (c >= 0) { + ios_write(s, str, c); - if (c < 0) return c; - - ios_write(s, str, c); - - free(str); + LLT_FREE(str); + } + return c; +} + +int ios_printf(ios_t *s, char *format, ...) +{ + va_list args; + int c; + + va_start(args, format); + c = ios_vprintf(s, format, args); + va_end(args); return c; } diff --git a/llt/ios.h b/llt/ios.h index 6d661b8..35ceef3 100644 --- a/llt/ios.h +++ b/llt/ios.h @@ -104,6 +104,7 @@ int ios_putint(ios_t *s, int n); int ios_pututf8(ios_t *s, uint32_t wc); int ios_putstringz(ios_t *s, char *str, bool_t do_write_nulterm); int ios_printf(ios_t *s, char *format, ...); +int ios_vprintf(ios_t *s, char *format, va_list args); void hexdump(ios_t *dest, char *buffer, size_t len, size_t startoffs); diff --git a/llt/llt.h b/llt/llt.h index a1d8e2c..dbc83a7 100644 --- a/llt/llt.h +++ b/llt/llt.h @@ -12,6 +12,7 @@ #include "ptrhash.h" #include "bitvector.h" #include "dirpath.h" +#include "random.h" void llt_init(); diff --git a/llt/random.c b/llt/random.c index e29b5bb..9133345 100644 --- a/llt/random.c +++ b/llt/random.c @@ -9,7 +9,6 @@ #include "utils.h" #include "random.h" #include "timefuncs.h" -#include "ios.h" #include "mt19937ar.c" diff --git a/llt/socket.c b/llt/socket.c index 484b496..306a07f 100644 --- a/llt/socket.c +++ b/llt/socket.c @@ -7,7 +7,7 @@ #include "dtypes.h" -#if defined(MACOSX) || defined(MACINTEL) +#if defined(MACOSX) #include #include #include diff --git a/llt/timefuncs.c b/llt/timefuncs.c index fe2269b..ae33e20 100644 --- a/llt/timefuncs.c +++ b/llt/timefuncs.c @@ -106,7 +106,7 @@ void timestring(double seconds, char *buffer, size_t len) #endif } -#if defined(LINUX) || defined(MACOSX) || defined(MACINTEL) +#if defined(LINUX) || defined(MACOSX) extern char *strptime(const char *s, const char *format, struct tm *tm); double parsetime(char *str) { diff --git a/llt/utf8.h b/llt/utf8.h index 28b71e3..b15a725 100644 --- a/llt/utf8.h +++ b/llt/utf8.h @@ -1,7 +1,7 @@ #ifndef __UTF8_H_ #define __UTF8_H_ -#if !defined(MACOSX) && !defined(MACINTEL) +#if !defined(MACOSX) #if !defined(__DTYPES_H_) && !defined(_SYS_TYPES_H) typedef char int8_t; typedef short int16_t;