some LLT cleanup:

making allocation functions customizable
  adding ios_vprintf
  simplifying config variables for mac
This commit is contained in:
JeffBezanson 2010-02-25 04:37:33 +00:00
parent 2e99f52b29
commit 7d652f9c5a
15 changed files with 64 additions and 60 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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 @@ 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

View File

@ -1,4 +1,5 @@
#include <stdlib.h>
#include <stdarg.h>
#include "dtypes.h"
#include "ios.h"
#include "utils.h"

View File

@ -3,6 +3,7 @@
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include "ieee754.h"
#include "dtypes.h"

View File

@ -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;

View File

@ -19,32 +19,28 @@
#ifndef _IEEE754_H
#define _IEEE754_H 1
#ifdef LINUX
#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
{

View File

@ -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) return c;
if (c >= 0) {
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;
}

View File

@ -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);

View File

@ -12,6 +12,7 @@
#include "ptrhash.h"
#include "bitvector.h"
#include "dirpath.h"
#include "random.h"
void llt_init();

View File

@ -9,7 +9,6 @@
#include "utils.h"
#include "random.h"
#include "timefuncs.h"
#include "ios.h"
#include "mt19937ar.c"

View File

@ -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>

View File

@ -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)
{

View File

@ -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;