… | |
… | |
40 | typedef DB_TXN DB_TXN_ornull; |
40 | typedef DB_TXN DB_TXN_ornull; |
41 | typedef DBC DBC_ornull; |
41 | typedef DBC DBC_ornull; |
42 | typedef DB DB_ornull; |
42 | typedef DB DB_ornull; |
43 | typedef DB_SEQUENCE DB_SEQUENCE_ornull; |
43 | typedef DB_SEQUENCE DB_SEQUENCE_ornull; |
44 | |
44 | |
|
|
45 | typedef DB_ENV DB_ENV_ornuked; |
|
|
46 | typedef DB_TXN DB_TXN_ornuked; |
|
|
47 | typedef DBC DBC_ornuked; |
|
|
48 | typedef DB DB_ornuked; |
|
|
49 | typedef DB_SEQUENCE DB_SEQUENCE_ornuked; |
|
|
50 | |
45 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
51 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
46 | typedef char *octetstring; |
52 | typedef char *bdb_filename; |
47 | |
53 | |
48 | static SV *prepare_cb; |
54 | static SV *prepare_cb; |
49 | |
55 | |
50 | #if DB_VERSION_MINOR >= 6 |
56 | #if DB_VERSION_MINOR >= 6 |
51 | # define c_close close |
57 | # define c_close close |
… | |
… | |
54 | # define c_dup dup |
60 | # define c_dup dup |
55 | # define c_get get |
61 | # define c_get get |
56 | # define c_pget pget |
62 | # define c_pget pget |
57 | # define c_put put |
63 | # define c_put put |
58 | #endif |
64 | #endif |
|
|
65 | |
|
|
66 | static char * |
|
|
67 | get_bdb_filename (SV *sv) |
|
|
68 | { |
|
|
69 | return !SvOK (sv) |
|
|
70 | ? 0 |
|
|
71 | : |
|
|
72 | #if WIN32 |
|
|
73 | SvPVutf8_nolen (sv) |
|
|
74 | #else |
|
|
75 | SvPVbyte_nolen (sv) |
|
|
76 | #endif |
|
|
77 | ; |
|
|
78 | } |
59 | |
79 | |
60 | static void |
80 | static void |
61 | debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg) |
81 | debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg) |
62 | { |
82 | { |
63 | printf ("err[%s]\n", msg); |
83 | printf ("err[%s]\n", msg); |
… | |
… | |
199 | static mutex_t reqlock = X_MUTEX_INIT; |
219 | static mutex_t reqlock = X_MUTEX_INIT; |
200 | static cond_t reqwait = X_COND_INIT; |
220 | static cond_t reqwait = X_COND_INIT; |
201 | |
221 | |
202 | #if WORDACCESS_UNSAFE |
222 | #if WORDACCESS_UNSAFE |
203 | |
223 | |
204 | static unsigned int get_nready () |
224 | static unsigned int get_nready (void) |
205 | { |
225 | { |
206 | unsigned int retval; |
226 | unsigned int retval; |
207 | |
227 | |
208 | X_LOCK (reqlock); |
228 | X_LOCK (reqlock); |
209 | retval = nready; |
229 | retval = nready; |
210 | X_UNLOCK (reqlock); |
230 | X_UNLOCK (reqlock); |
211 | |
231 | |
212 | return retval; |
232 | return retval; |
213 | } |
233 | } |
214 | |
234 | |
215 | static unsigned int get_npending () |
235 | static unsigned int get_npending (void) |
216 | { |
236 | { |
217 | unsigned int retval; |
237 | unsigned int retval; |
218 | |
238 | |
219 | X_LOCK (reslock); |
239 | X_LOCK (reslock); |
220 | retval = npending; |
240 | retval = npending; |
221 | X_UNLOCK (reslock); |
241 | X_UNLOCK (reslock); |
222 | |
242 | |
223 | return retval; |
243 | return retval; |
224 | } |
244 | } |
225 | |
245 | |
226 | static unsigned int get_nthreads () |
246 | static unsigned int get_nthreads (void) |
227 | { |
247 | { |
228 | unsigned int retval; |
248 | unsigned int retval; |
229 | |
249 | |
230 | X_LOCK (wrklock); |
250 | X_LOCK (wrklock); |
231 | retval = started; |
251 | retval = started; |
… | |
… | |
294 | } |
314 | } |
295 | |
315 | |
296 | abort (); |
316 | abort (); |
297 | } |
317 | } |
298 | |
318 | |
299 | static int poll_cb (); |
319 | static int poll_cb (void); |
300 | static void req_free (bdb_req req); |
320 | static void req_free (bdb_req req); |
301 | static void req_cancel (bdb_req req); |
321 | static void req_cancel (bdb_req req); |
302 | |
322 | |
303 | static int req_invoke (bdb_req req) |
323 | static int req_invoke (bdb_req req) |
304 | { |
324 | { |
… | |
… | |
385 | #else |
405 | #else |
386 | # define TO_SOCKET(x) (x) |
406 | # define TO_SOCKET(x) (x) |
387 | #endif |
407 | #endif |
388 | |
408 | |
389 | static void |
409 | static void |
390 | create_respipe () |
410 | create_respipe (void) |
391 | { |
411 | { |
392 | #ifdef _WIN32 |
412 | #ifdef _WIN32 |
393 | int arg; /* argg */ |
413 | int arg; /* argg */ |
394 | #endif |
414 | #endif |
395 | int old_readfd = respipe [0]; |
415 | int old_readfd = respipe [0]; |
… | |
… | |
449 | free (wrk); |
469 | free (wrk); |
450 | |
470 | |
451 | X_UNLOCK (wrklock); |
471 | X_UNLOCK (wrklock); |
452 | } |
472 | } |
453 | |
473 | |
454 | static void maybe_start_thread () |
474 | static void maybe_start_thread (void) |
455 | { |
475 | { |
456 | if (get_nthreads () >= wanted) |
476 | if (get_nthreads () >= wanted) |
457 | return; |
477 | return; |
458 | |
478 | |
459 | /* todo: maybe use idle here, but might be less exact */ |
479 | /* todo: maybe use idle here, but might be less exact */ |
… | |
… | |
544 | |
564 | |
545 | while (started > wanted) |
565 | while (started > wanted) |
546 | end_thread (); |
566 | end_thread (); |
547 | } |
567 | } |
548 | |
568 | |
549 | static void poll_wait () |
569 | static void poll_wait (void) |
550 | { |
570 | { |
551 | fd_set rfd; |
571 | fd_set rfd; |
552 | |
572 | |
553 | while (nreqs) |
573 | while (nreqs) |
554 | { |
574 | { |
… | |
… | |
567 | |
587 | |
568 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
588 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
569 | } |
589 | } |
570 | } |
590 | } |
571 | |
591 | |
572 | static int poll_cb () |
592 | static int poll_cb (void) |
573 | { |
593 | { |
574 | dSP; |
594 | dSP; |
575 | int count = 0; |
595 | int count = 0; |
576 | int maxreqs = max_poll_reqs; |
596 | int maxreqs = max_poll_reqs; |
577 | int do_croak = 0; |
597 | int do_croak = 0; |
… | |
… | |
922 | req->pri = req_pri |
942 | req->pri = req_pri |
923 | |
943 | |
924 | #define REQ_SEND \ |
944 | #define REQ_SEND \ |
925 | req_send (req) |
945 | req_send (req) |
926 | |
946 | |
927 | #define SvPTR(var, arg, type, class, nullok) \ |
947 | #define SvPTR(var, arg, type, class, nullok) \ |
928 | if (!SvOK (arg)) \ |
948 | if (!SvOK (arg)) \ |
929 | { \ |
949 | { \ |
930 | if (!nullok) \ |
950 | if (nullok != 1) \ |
931 | croak (# var " must be a " # class " object, not undef"); \ |
951 | croak (# var " must be a " # class " object, not undef"); \ |
932 | \ |
952 | \ |
933 | (var) = 0; \ |
953 | (var) = 0; \ |
934 | } \ |
954 | } \ |
935 | else if (sv_derived_from ((arg), # class)) \ |
955 | else if (sv_derived_from ((arg), # class)) \ |
936 | { \ |
956 | { \ |
937 | IV tmp = SvIV ((SV*) SvRV (arg)); \ |
957 | IV tmp = SvIV ((SV*) SvRV (arg)); \ |
938 | (var) = INT2PTR (type, tmp); \ |
958 | (var) = INT2PTR (type, tmp); \ |
939 | if (!var) \ |
959 | if (!var && nullok != 2) \ |
940 | croak (# var " is not a valid " # class " object anymore"); \ |
960 | croak (# var " is not a valid " # class " object anymore"); \ |
941 | } \ |
961 | } \ |
942 | else \ |
962 | else \ |
943 | croak (# var " is not of type " # class); \ |
963 | croak (# var " is not of type " # class); \ |
944 | \ |
964 | \ |
… | |
… | |
946 | static void |
966 | static void |
947 | ptr_nuke (SV *sv) |
967 | ptr_nuke (SV *sv) |
948 | { |
968 | { |
949 | assert (SvROK (sv)); |
969 | assert (SvROK (sv)); |
950 | sv_setiv (SvRV (sv), 0); |
970 | sv_setiv (SvRV (sv), 0); |
|
|
971 | } |
|
|
972 | |
|
|
973 | static int |
|
|
974 | errno_get (pTHX_ SV *sv, MAGIC *mg) |
|
|
975 | { |
|
|
976 | if (*mg->mg_ptr == '!') // should always be the case |
|
|
977 | if (-30999 <= errno && errno <= -30800) |
|
|
978 | { |
|
|
979 | sv_setnv (sv, (NV)errno); |
|
|
980 | sv_setpv (sv, db_strerror (errno)); |
|
|
981 | SvNOK_on (sv); /* what a wonderful hack! */ |
|
|
982 | // ^^^ copied from perl sources |
|
|
983 | return 0; |
|
|
984 | } |
|
|
985 | |
|
|
986 | return PL_vtbl_sv.svt_get (aTHX_ sv, mg); |
|
|
987 | } |
|
|
988 | |
|
|
989 | static MGVTBL vtbl_errno; |
|
|
990 | |
|
|
991 | // this wonderful hack :( patches perl's $! variable to support our errno values |
|
|
992 | static void |
|
|
993 | patch_errno (void) |
|
|
994 | { |
|
|
995 | SV *sv; |
|
|
996 | MAGIC *mg; |
|
|
997 | |
|
|
998 | if (!(sv = get_sv ("!", 1))) |
|
|
999 | return; |
|
|
1000 | |
|
|
1001 | if (!(mg = mg_find (sv, PERL_MAGIC_sv))) |
|
|
1002 | return; |
|
|
1003 | |
|
|
1004 | if (mg->mg_virtual != &PL_vtbl_sv) |
|
|
1005 | return; |
|
|
1006 | |
|
|
1007 | vtbl_errno = PL_vtbl_sv; |
|
|
1008 | vtbl_errno.svt_get = errno_get; |
|
|
1009 | mg->mg_virtual = &vtbl_errno; |
951 | } |
1010 | } |
952 | |
1011 | |
953 | MODULE = BDB PACKAGE = BDB |
1012 | MODULE = BDB PACKAGE = BDB |
954 | |
1013 | |
955 | PROTOTYPES: ENABLE |
1014 | PROTOTYPES: ENABLE |
… | |
… | |
1153 | X_MUTEX_CHECK (reslock); |
1212 | X_MUTEX_CHECK (reslock); |
1154 | X_MUTEX_CHECK (reqlock); |
1213 | X_MUTEX_CHECK (reqlock); |
1155 | |
1214 | |
1156 | X_COND_CHECK (reqwait); |
1215 | X_COND_CHECK (reqwait); |
1157 | #endif |
1216 | #endif |
|
|
1217 | patch_errno (); |
1158 | } |
1218 | } |
1159 | |
1219 | |
1160 | void |
1220 | void |
1161 | max_poll_reqs (int nreqs) |
1221 | max_poll_reqs (int nreqs) |
1162 | PROTOTYPE: $ |
1222 | PROTOTYPE: $ |
… | |
… | |
1320 | } |
1380 | } |
1321 | OUTPUT: |
1381 | OUTPUT: |
1322 | RETVAL |
1382 | RETVAL |
1323 | |
1383 | |
1324 | void |
1384 | void |
1325 | db_env_open (DB_ENV *env, octetstring db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) |
1385 | db_env_open (DB_ENV *env, bdb_filename db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) |
1326 | CODE: |
1386 | CODE: |
1327 | { |
1387 | { |
1328 | dREQ (REQ_ENV_OPEN); |
1388 | dREQ (REQ_ENV_OPEN); |
1329 | |
|
|
1330 | env->set_thread_count (env, wanted + 2); |
|
|
1331 | |
1389 | |
1332 | req->env = env; |
1390 | req->env = env; |
1333 | req->uint1 = open_flags | DB_THREAD; |
1391 | req->uint1 = open_flags | DB_THREAD; |
1334 | req->int1 = mode; |
1392 | req->int1 = mode; |
1335 | req->buf1 = strdup_ornull (db_home); |
1393 | req->buf1 = strdup_ornull (db_home); |
… | |
… | |
1403 | } |
1461 | } |
1404 | OUTPUT: |
1462 | OUTPUT: |
1405 | RETVAL |
1463 | RETVAL |
1406 | |
1464 | |
1407 | void |
1465 | void |
1408 | db_open (DB *db, DB_TXN_ornull *txnid, octetstring file, octetstring database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef) |
1466 | db_open (DB *db, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, int type, U32 flags, int mode, SV *callback = &PL_sv_undef) |
1409 | CODE: |
1467 | CODE: |
1410 | { |
1468 | { |
1411 | dREQ (REQ_DB_OPEN); |
1469 | dREQ (REQ_DB_OPEN); |
1412 | req->db = db; |
1470 | req->db = db; |
1413 | req->txn = txnid; |
1471 | req->txn = txnid; |
… | |
… | |
1453 | req->uint1 = flags; |
1511 | req->uint1 = flags; |
1454 | REQ_SEND; |
1512 | REQ_SEND; |
1455 | } |
1513 | } |
1456 | |
1514 | |
1457 | void |
1515 | void |
1458 | db_upgrade (DB *db, octetstring file, U32 flags = 0, SV *callback = &PL_sv_undef) |
1516 | db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = &PL_sv_undef) |
1459 | CODE: |
1517 | CODE: |
1460 | { |
1518 | { |
1461 | dREQ (REQ_DB_SYNC); |
1519 | dREQ (REQ_DB_SYNC); |
1462 | req->db = db; |
1520 | req->db = db; |
1463 | req->buf1 = strdup (file); |
1521 | req->buf1 = strdup (file); |
… | |
… | |
1713 | |
1771 | |
1714 | |
1772 | |
1715 | MODULE = BDB PACKAGE = BDB::Env |
1773 | MODULE = BDB PACKAGE = BDB::Env |
1716 | |
1774 | |
1717 | void |
1775 | void |
1718 | DESTROY (DB_ENV_ornull *env) |
1776 | DESTROY (DB_ENV_ornuked *env) |
1719 | CODE: |
1777 | CODE: |
1720 | if (env) |
1778 | if (env) |
1721 | env->close (env, 0); |
1779 | env->close (env, 0); |
1722 | |
1780 | |
1723 | int set_data_dir (DB_ENV *env, const char *dir) |
1781 | int set_data_dir (DB_ENV *env, const char *dir) |
… | |
… | |
1870 | RETVAL |
1928 | RETVAL |
1871 | |
1929 | |
1872 | MODULE = BDB PACKAGE = BDB::Db |
1930 | MODULE = BDB PACKAGE = BDB::Db |
1873 | |
1931 | |
1874 | void |
1932 | void |
1875 | DESTROY (DB_ornull *db) |
1933 | DESTROY (DB_ornuked *db) |
1876 | CODE: |
1934 | CODE: |
1877 | if (db) |
1935 | if (db) |
1878 | { |
1936 | { |
1879 | SV *env = (SV *)db->app_private; |
1937 | SV *env = (SV *)db->app_private; |
1880 | db->close (db, 0); |
1938 | db->close (db, 0); |
… | |
… | |
1975 | |
2033 | |
1976 | |
2034 | |
1977 | MODULE = BDB PACKAGE = BDB::Txn |
2035 | MODULE = BDB PACKAGE = BDB::Txn |
1978 | |
2036 | |
1979 | void |
2037 | void |
1980 | DESTROY (DB_TXN_ornull *txn) |
2038 | DESTROY (DB_TXN_ornuked *txn) |
1981 | CODE: |
2039 | CODE: |
1982 | if (txn) |
2040 | if (txn) |
1983 | txn->abort (txn); |
2041 | txn->abort (txn); |
1984 | |
2042 | |
1985 | int set_timeout (DB_TXN *txn, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT) |
2043 | int set_timeout (DB_TXN *txn, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT) |
… | |
… | |
1996 | |
2054 | |
1997 | |
2055 | |
1998 | MODULE = BDB PACKAGE = BDB::Cursor |
2056 | MODULE = BDB PACKAGE = BDB::Cursor |
1999 | |
2057 | |
2000 | void |
2058 | void |
2001 | DESTROY (DBC_ornull *dbc) |
2059 | DESTROY (DBC_ornuked *dbc) |
2002 | CODE: |
2060 | CODE: |
2003 | if (dbc) |
2061 | if (dbc) |
2004 | dbc->c_close (dbc); |
2062 | dbc->c_close (dbc); |
2005 | |
2063 | |
2006 | #if DB_VERSION_MINOR >= 6 |
2064 | #if DB_VERSION_MINOR >= 6 |
… | |
… | |
2012 | #endif |
2070 | #endif |
2013 | |
2071 | |
2014 | MODULE = BDB PACKAGE = BDB::Sequence |
2072 | MODULE = BDB PACKAGE = BDB::Sequence |
2015 | |
2073 | |
2016 | void |
2074 | void |
2017 | DESTROY (DB_SEQUENCE_ornull *seq) |
2075 | DESTROY (DB_SEQUENCE_ornuked *seq) |
2018 | CODE: |
2076 | CODE: |
2019 | if (seq) |
2077 | if (seq) |
2020 | seq->close (seq, 0); |
2078 | seq->close (seq, 0); |
2021 | |
2079 | |
2022 | int initial_value (DB_SEQUENCE *seq, db_seq_t value) |
2080 | int initial_value (DB_SEQUENCE *seq, db_seq_t value) |
… | |
… | |
2041 | CODE: |
2099 | CODE: |
2042 | RETVAL = seq->set_range (seq, min, max); |
2100 | RETVAL = seq->set_range (seq, min, max); |
2043 | OUTPUT: |
2101 | OUTPUT: |
2044 | RETVAL |
2102 | RETVAL |
2045 | |
2103 | |
|
|
2104 | |