From f1037cdd9a92277aee240b1fc431c5c081cfadf2 Mon Sep 17 00:00:00 2001 From: Gregory Burd Date: Sat, 24 Mar 2007 22:59:14 +0100 Subject: [PATCH] fixed portability to mac os/x 10.4.9 There was no need to maintain random number generation state within the DBSQL structure, that is removed and now static within the two functions requiring random number generation. The placement of the function hash table in the memory allocated for DBSQL was wrong, that is now fixed. --- dist/Makefile.in | 7 +++---- dist/configure.ac | 5 +---- src/api.c | 8 +++----- src/cg_build.c | 8 +++++++- src/clib/random.c | 4 ++-- src/dbsql.in | 1 - src/inc/db_int.h | 1 + src/inc/dbsql_int.in | 7 ++++--- src/sql_fns.c | 4 ++-- src/vdbe.c | 14 ++++++++++---- 10 files changed, 33 insertions(+), 26 deletions(-) diff --git a/dist/Makefile.in b/dist/Makefile.in index ad3edcc..fc82e44 100644 --- a/dist/Makefile.in +++ b/dist/Makefile.in @@ -136,15 +136,14 @@ C_FILES=\ # Object and utility lists. ################################################## -C_OBJS= @ADDITIONAL_OBJS@ @REPLACEMENT_OBJS@ \ - cg_attach@o@ cg_insert@o@ sql_tokenize@o@ cg_auth@o@ \ +C_OBJS= cg_attach@o@ cg_insert@o@ sql_tokenize@o@ cg_auth@o@ \ cg_copy@o@ api_table@o@ cg_date@o@ api@o@ xvprintf@o@ \ cg_pragma@o@ cg_where@o@ cg_trigger@o@ cg_build@o@ \ sql_fns@o@ random@o@ cg_update@o@ cg_delete@o@ hash@o@ \ cg_expr@o@ opcodes@o@ sql_parser@o@ cg_vacuum@o@ \ vdbe@o@ vdbe_method@o@ sm@o@ snprintf@o@ dbsql_err@o@ cg_select@o@ \ - os_jtime@o@ memcmp@o@ dbsql_atof@o@ safety@o@ \ - strcasecmp@o@ strdup@o@ dbsql_alloc@o@ str@o@ dbsql_atoi@o@ + os_jtime@o@ memcmp@o@ dbsql_atof@o@ safety@o@ dbsql_atoi@o@ \ + strcasecmp@o@ strdup@o@ dbsql_alloc@o@ str@o@ LEMON_OBJS=\ lemon@o@ diff --git a/dist/configure.ac b/dist/configure.ac index 62b33ea..eb01c16 100644 --- a/dist/configure.ac +++ b/dist/configure.ac @@ -490,10 +490,7 @@ esac # We need to add the additional object files into the Makefile with the correct # suffix. We can't use $LTLIBOBJS itself, because that variable has $U encoded -# in it for automake, and that's not what we want. See SR #7227 for additional -# information. -# -# XXX: I'm not sure this is correct. +# in it for automake, and that's not what we want. REPLACEMENT_OBJS=`echo "$LIB@&t@OBJS" | sed "s,\.[[^.]]* ,$o ,g;s,\.[[^.]]*$,$o,"` diff --git a/src/api.c b/src/api.c index 172c1bc..2c3e0a6 100644 --- a/src/api.c +++ b/src/api.c @@ -789,7 +789,7 @@ __default_busy_callback(dbp, arg, not_used, count) #if defined(__LP64) || defined(__LP64__) u_int64_t timeout = (u_int64_t)arg; #else - u_int32_t timeout = (u_int32_)t)arg; + u_int32_t timeout = (u_int32_t)arg; #endif #if DBSQL_MIN_SLEEP_MS==1 static const char delays[] = @@ -1438,13 +1438,11 @@ dbsql_create(dbpp, dbenv, flags) DBSQL_GLOBAL(encoding) = "iso8859"; #endif - if (__dbsql_calloc(NULL, 1, sizeof(*dbp) + sizeof(hash_t), &dbp) + if (__dbsql_calloc(NULL, 1, sizeof(DBSQL) + sizeof(hash_t), &dbp) == ENOMEM) return DBSQL_NOMEM; - dbp->aFunc = dbp + sizeof(hash_t); - - srand48_r(1, &dbp->rand); /* seed our random number generator TODO */ + dbp->aFunc = dbp + sizeof(DBSQL); if (LF_ISSET(DBSQL_THREAD)) F_SET(dbp, DBSQL_Threaded); diff --git a/src/cg_build.c b/src/cg_build.c index 6b897ea..3cd4175 100644 --- a/src/cg_build.c +++ b/src/cg_build.c @@ -1053,9 +1053,15 @@ void __change_schema_signature(dbp, v) DBSQL *dbp; vdbe_t *v; { + static struct drand48_data rand; + static int first_time = 1; + if (first_time) { + first_time = 0; + srand48_r(&rand); + } if (dbp->next_sig == dbp->aDb[0].schema_sig) { u_int8_t n; - rand8_r(&dbp->rand, &n); + rand8_r(&rand, &n); dbp->next_sig = dbp->aDb[0].schema_sig + n + 1; dbp->flags |= DBSQL_InternChanges; __vdbe_add_op(v, OP_Integer, dbp->next_sig, 0); diff --git a/src/clib/random.c b/src/clib/random.c index 0e7dedb..9afeda8 100644 --- a/src/clib/random.c +++ b/src/clib/random.c @@ -129,7 +129,7 @@ __rng_seed(buf) #ifdef CONFIG_TEST return; #else - __os_pid(&pid); + __os_id(NULL, &pid, NULL); __os_jtime(&jt); for (i = 0; i < SEED_SZ; i++) { if (i % 2) @@ -152,7 +152,7 @@ __rng_seed(buf) */ #ifndef HAVE_SRAND48_R int -srand48_r(seedval, buffer) +srand48_r(buffer) struct drand48_data *buffer; { int i; diff --git a/src/dbsql.in b/src/dbsql.in index 7ea9f5e..137b784 100644 --- a/src/dbsql.in +++ b/src/dbsql.in @@ -326,7 +326,6 @@ struct __dbsql { FILE *dbsql_errfile; /* The error file */ char *dbsql_errpfx; /* The error prefix string */ void (*dbsql_paniccall)(DBSQL *, int); /* The panic callback */ - struct drand48_data rand; /* Random generator state */ int panic; /* If non-zero, shut down the application */ dbsql_db_t *aDb; /* One for each open SQL database + 2 */ int nDb; /* Number of open dbsql_db_t's open */ diff --git a/src/inc/db_int.h b/src/inc/db_int.h index 45af6f4..d169291 100644 --- a/src/inc/db_int.h +++ b/src/inc/db_int.h @@ -46,6 +46,7 @@ extern int __os_urealloc__DB_UNIQUE_NAME__ __P((DB_ENV *, size_t, void *)); extern int __os_umalloc__DB_UNIQUE_NAME__ __P((DB_ENV *, size_t, void *)); extern int __os_exists__DB_UNIQUE_NAME__ __P((const char *, int *)); extern int __db_omode__DB_UNIQUE_NAME__ __P((const char *)); +extern int __os_id__DB_UNIQUE_NAME__ __P((DB_ENV *, pid_t *, db_threadid_t*)); #if defined(__cplusplus) } diff --git a/src/inc/dbsql_int.in b/src/inc/dbsql_int.in index 8e82866..aedb435 100644 --- a/src/inc/dbsql_int.in +++ b/src/inc/dbsql_int.in @@ -37,17 +37,18 @@ #include -#include "dbsql.h" #include "sql_parser.h" - #include "inc/queue.h" #include "inc/hash.h" #include "inc/str.h" +#include "inc/random.h" #include "inc/vdbe.h" #include "inc/debug.h" #include "inc/xvprintf.h" #include "inc/globals.h" +#include "dbsql.h" + /* * Forward references to structures. */ @@ -184,7 +185,7 @@ typedef struct sm_cursor { struct __dbsql_db { char *zName; /* Name of this database */ sm_t *pBt; /* The storage manager for this database */ - int schema_sig; /* Database schema version number for this file */ + u_int32_t schema_sig;/* Database schema version number for this file */ hash_t tblHash; /* All tables indexed by name */ hash_t idxHash; /* All (named) indices indexed by name */ hash_t trigHash; /* All triggers indexed by name */ diff --git a/src/sql_fns.c b/src/sql_fns.c index 15bfff9..49fd504 100644 --- a/src/sql_fns.c +++ b/src/sql_fns.c @@ -351,12 +351,12 @@ __random_func(context, argc, argv) int argc; const char **argv; { - int n; + u_int32_t n; static struct drand48_data rand; static int first_time = 1; if (first_time) { first_time = 0; - srand48_r(1, &rand); /* TODO seed our random number generator */ + srand48_r(&rand); } rand32_r(&rand, &n); dbsql_set_result_int(context, n); diff --git a/src/vdbe.c b/src/vdbe.c index 8f6fe78..3a96d73 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -2445,7 +2445,7 @@ case OP_SetSchemaSignature: { ** everyone (and their data). */ case OP_VerifySchemaSignature: { - int sig; + u_int32_t sig; DBSQL_ASSERT(pOp->p1 >= 0 && pOp->p1 < db->nDb); rc = __sm_get_schema_sig(db->aDb[pOp->p1].pBt, &sig); if (rc == DBSQL_SUCCESS && sig != pOp->p2) { @@ -2943,8 +2943,14 @@ case OP_NotExists: { ** onto the stack. */ case OP_NewRecno: { + static struct drand48_data rand; + static int first_time = 1; + if (first_time) { + first_time = 0; + srand48_r(&rand); + } int i = pOp->p1; - int v = 0; + u_int32_t v = 0; cursor_t *pC; DBSQL_ASSERT(i >= 0 && i < p->nCursor); if ((pC = &p->aCsr[i])->pCursor == 0) { @@ -3017,12 +3023,12 @@ case OP_NewRecno: { cnt = 0; do { if (v == 0 || cnt > 2) { - rand32_r(&db->rand, &v); + rand32_r(&rand, &v); if (cnt < 5) v &= 0xffffff; } else { u_int8_t rb; - rand8_r(&db->rand, &rb); + rand8_r(&rand, &rb); v += rb + 1; } if (v == 0)