ref: 7d652f9c5ad285b4461fcacf0818bb9179f794e8
parent: 2e99f52b29df7b7e7c6ea59f3ffc3f305e2478c3
author: JeffBezanson <[email protected]>
date: Wed Feb 24 23:37:33 EST 2010
some LLT cleanup: making allocation functions customizable adding ios_vprintf simplifying config variables for mac
--- a/llt/Makefile
+++ b/llt/Makefile
@@ -10,7 +10,10 @@
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)
--- a/llt/config.h
+++ /dev/null
@@ -1,15 +1,0 @@
-#ifndef __CONFIG_H_
-#define __CONFIG_H_
-
-#define LINUX
-#undef WIN32
-
-#undef BITS64
-
-#define ARCH_X86
-#undef ARCH_X86_64
-
-#define __CPU__ 586
-
-
-#endif
--- a/llt/dirpath.c
+++ b/llt/dirpath.c
@@ -97,7 +97,7 @@
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)
--- 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 <gc.h>
+#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 @@
#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
--- a/llt/dump.c
+++ b/llt/dump.c
@@ -1,4 +1,5 @@
#include <stdlib.h>
+#include <stdarg.h>
#include "dtypes.h"
#include "ios.h"
#include "utils.h"
--- a/llt/hashing.c
+++ b/llt/hashing.c
@@ -3,6 +3,7 @@
*/
#include <stdlib.h>
#include <stdio.h>
+#include <stdarg.h>
#include <math.h>
#include "ieee754.h"
#include "dtypes.h"
--- a/llt/htable.c
+++ b/llt/htable.c
@@ -23,7 +23,7 @@
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 @@
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 @@
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;
--- a/llt/ieee754.h
+++ b/llt/ieee754.h
@@ -19,32 +19,28 @@
#ifndef _IEEE754_H
#define _IEEE754_H 1
+
#ifdef LINUX
-#include <features.h>
+#include <features.h>
#include <endian.h>
-
__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
{
--- a/llt/ios.c
+++ b/llt/ios.c
@@ -25,7 +25,6 @@
#include "utils.h"
#include "utf8.h"
#include "ios.h"
-#include "socket.h"
#include "timefuncs.h"
#define MOST_OF(x) ((x) - ((x)>>4))
@@ -32,7 +31,7 @@
/* 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 @@
// 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 @@
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 @@
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 @@
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 @@
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 @@
}
}
-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 @@
}
c = vasprintf(&str, format, args);
- va_end(args);
+ if (c >= 0) {
+ ios_write(s, str, c);
- if (c < 0) return c;
+ LLT_FREE(str);
+ }
+ return c;
+}
- ios_write(s, str, c);
+int ios_printf(ios_t *s, char *format, ...)
+{
+ va_list args;
+ int c;
- free(str);
+ va_start(args, format);
+ c = ios_vprintf(s, format, args);
+ va_end(args);
return c;
}
--- a/llt/ios.h
+++ b/llt/ios.h
@@ -104,6 +104,7 @@
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);
--- 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();
--- 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"
--- a/llt/socket.c
+++ b/llt/socket.c
@@ -7,7 +7,7 @@
#include "dtypes.h"
-#if defined(MACOSX) || defined(MACINTEL)
+#if defined(MACOSX)
#include <sys/time.h>
#include <sys/select.h>
#include <sys/types.h>
--- a/llt/timefuncs.c
+++ b/llt/timefuncs.c
@@ -106,7 +106,7 @@
#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)
{
--- 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;