… | |
… | |
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 | if (!SvOK (sv)) |
|
|
70 | return 0; |
|
|
71 | |
|
|
72 | #if _WIN32 |
|
|
73 | /* win32 madness + win32 perl absolutely brokenness make for horrible hacks */ |
|
|
74 | { |
|
|
75 | STRLEN len; |
|
|
76 | char *src = SvPVbyte (sv, len); |
|
|
77 | SV *t1 = sv_newmortal (); |
|
|
78 | SV *t2 = sv_newmortal (); |
|
|
79 | |
|
|
80 | sv_upgrade (t1, SVt_PV); SvPOK_only (t1); SvGROW (t1, len * 16 + 1); |
|
|
81 | sv_upgrade (t2, SVt_PV); SvPOK_only (t2); SvGROW (t2, len * 16 + 1); |
|
|
82 | |
|
|
83 | len = MultiByteToWideChar (CP_ACP, 0, src, len, (WCHAR *)SvPVX (t1), SvLEN (t1) / sizeof (WCHAR)); |
|
|
84 | len = WideCharToMultiByte (CP_UTF8, 0, (WCHAR *)SvPVX (t1), len, SvPVX (t2), SvLEN (t2), 0, 0); |
|
|
85 | SvPOK_only (t2); |
|
|
86 | SvPVX (t2)[len] = 0; |
|
|
87 | SvCUR_set (t2, len); |
|
|
88 | |
|
|
89 | return SvPVX (t2); |
|
|
90 | } |
|
|
91 | #else |
|
|
92 | return SvPVbyte_nolen (sv); |
|
|
93 | #endif |
|
|
94 | } |
59 | |
95 | |
60 | static void |
96 | static void |
61 | debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg) |
97 | debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg) |
62 | { |
98 | { |
63 | printf ("err[%s]\n", msg); |
99 | printf ("err[%s]\n", msg); |
… | |
… | |
199 | static mutex_t reqlock = X_MUTEX_INIT; |
235 | static mutex_t reqlock = X_MUTEX_INIT; |
200 | static cond_t reqwait = X_COND_INIT; |
236 | static cond_t reqwait = X_COND_INIT; |
201 | |
237 | |
202 | #if WORDACCESS_UNSAFE |
238 | #if WORDACCESS_UNSAFE |
203 | |
239 | |
204 | static unsigned int get_nready () |
240 | static unsigned int get_nready (void) |
205 | { |
241 | { |
206 | unsigned int retval; |
242 | unsigned int retval; |
207 | |
243 | |
208 | X_LOCK (reqlock); |
244 | X_LOCK (reqlock); |
209 | retval = nready; |
245 | retval = nready; |
210 | X_UNLOCK (reqlock); |
246 | X_UNLOCK (reqlock); |
211 | |
247 | |
212 | return retval; |
248 | return retval; |
213 | } |
249 | } |
214 | |
250 | |
215 | static unsigned int get_npending () |
251 | static unsigned int get_npending (void) |
216 | { |
252 | { |
217 | unsigned int retval; |
253 | unsigned int retval; |
218 | |
254 | |
219 | X_LOCK (reslock); |
255 | X_LOCK (reslock); |
220 | retval = npending; |
256 | retval = npending; |
221 | X_UNLOCK (reslock); |
257 | X_UNLOCK (reslock); |
222 | |
258 | |
223 | return retval; |
259 | return retval; |
224 | } |
260 | } |
225 | |
261 | |
226 | static unsigned int get_nthreads () |
262 | static unsigned int get_nthreads (void) |
227 | { |
263 | { |
228 | unsigned int retval; |
264 | unsigned int retval; |
229 | |
265 | |
230 | X_LOCK (wrklock); |
266 | X_LOCK (wrklock); |
231 | retval = started; |
267 | retval = started; |
… | |
… | |
294 | } |
330 | } |
295 | |
331 | |
296 | abort (); |
332 | abort (); |
297 | } |
333 | } |
298 | |
334 | |
299 | static int poll_cb (); |
335 | static int poll_cb (void); |
300 | static void req_free (bdb_req req); |
336 | static void req_free (bdb_req req); |
301 | static void req_cancel (bdb_req req); |
337 | static void req_cancel (bdb_req req); |
302 | |
338 | |
303 | static int req_invoke (bdb_req req) |
339 | static int req_invoke (bdb_req req) |
304 | { |
340 | { |
… | |
… | |
385 | #else |
421 | #else |
386 | # define TO_SOCKET(x) (x) |
422 | # define TO_SOCKET(x) (x) |
387 | #endif |
423 | #endif |
388 | |
424 | |
389 | static void |
425 | static void |
390 | create_respipe () |
426 | create_respipe (void) |
391 | { |
427 | { |
392 | #ifdef _WIN32 |
428 | #ifdef _WIN32 |
393 | int arg; /* argg */ |
429 | int arg; /* argg */ |
394 | #endif |
430 | #endif |
395 | int old_readfd = respipe [0]; |
431 | int old_readfd = respipe [0]; |
… | |
… | |
449 | free (wrk); |
485 | free (wrk); |
450 | |
486 | |
451 | X_UNLOCK (wrklock); |
487 | X_UNLOCK (wrklock); |
452 | } |
488 | } |
453 | |
489 | |
454 | static void maybe_start_thread () |
490 | static void maybe_start_thread (void) |
455 | { |
491 | { |
456 | if (get_nthreads () >= wanted) |
492 | if (get_nthreads () >= wanted) |
457 | return; |
493 | return; |
458 | |
494 | |
459 | /* todo: maybe use idle here, but might be less exact */ |
495 | /* todo: maybe use idle here, but might be less exact */ |
… | |
… | |
544 | |
580 | |
545 | while (started > wanted) |
581 | while (started > wanted) |
546 | end_thread (); |
582 | end_thread (); |
547 | } |
583 | } |
548 | |
584 | |
549 | static void poll_wait () |
585 | static void poll_wait (void) |
550 | { |
586 | { |
551 | fd_set rfd; |
587 | fd_set rfd; |
552 | |
588 | |
553 | while (nreqs) |
589 | while (nreqs) |
554 | { |
590 | { |
… | |
… | |
567 | |
603 | |
568 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
604 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
569 | } |
605 | } |
570 | } |
606 | } |
571 | |
607 | |
572 | static int poll_cb () |
608 | static int poll_cb (void) |
573 | { |
609 | { |
574 | dSP; |
610 | dSP; |
575 | int count = 0; |
611 | int count = 0; |
576 | int maxreqs = max_poll_reqs; |
612 | int maxreqs = max_poll_reqs; |
577 | int do_croak = 0; |
613 | int do_croak = 0; |
… | |
… | |
922 | req->pri = req_pri |
958 | req->pri = req_pri |
923 | |
959 | |
924 | #define REQ_SEND \ |
960 | #define REQ_SEND \ |
925 | req_send (req) |
961 | req_send (req) |
926 | |
962 | |
927 | #define SvPTR(var, arg, type, class, nullok) \ |
963 | #define SvPTR(var, arg, type, class, nullok) \ |
928 | if (!SvOK (arg)) \ |
964 | if (!SvOK (arg)) \ |
929 | { \ |
965 | { \ |
930 | if (!nullok) \ |
966 | if (nullok != 1) \ |
931 | croak (# var " must be a " # class " object, not undef"); \ |
967 | croak (# var " must be a " # class " object, not undef"); \ |
932 | \ |
968 | \ |
933 | (var) = 0; \ |
969 | (var) = 0; \ |
934 | } \ |
970 | } \ |
935 | else if (sv_derived_from ((arg), # class)) \ |
971 | else if (sv_derived_from ((arg), # class)) \ |
936 | { \ |
972 | { \ |
937 | IV tmp = SvIV ((SV*) SvRV (arg)); \ |
973 | IV tmp = SvIV ((SV*) SvRV (arg)); \ |
938 | (var) = INT2PTR (type, tmp); \ |
974 | (var) = INT2PTR (type, tmp); \ |
939 | if (!var) \ |
975 | if (!var && nullok != 2) \ |
940 | croak (# var " is not a valid " # class " object anymore"); \ |
976 | croak (# var " is not a valid " # class " object anymore"); \ |
941 | } \ |
977 | } \ |
942 | else \ |
978 | else \ |
943 | croak (# var " is not of type " # class); \ |
979 | croak (# var " is not of type " # class); \ |
944 | \ |
980 | \ |
… | |
… | |
946 | static void |
982 | static void |
947 | ptr_nuke (SV *sv) |
983 | ptr_nuke (SV *sv) |
948 | { |
984 | { |
949 | assert (SvROK (sv)); |
985 | assert (SvROK (sv)); |
950 | sv_setiv (SvRV (sv), 0); |
986 | sv_setiv (SvRV (sv), 0); |
|
|
987 | } |
|
|
988 | |
|
|
989 | static int |
|
|
990 | errno_get (pTHX_ SV *sv, MAGIC *mg) |
|
|
991 | { |
|
|
992 | if (*mg->mg_ptr == '!') // should always be the case |
|
|
993 | if (-30999 <= errno && errno <= -30800) |
|
|
994 | { |
|
|
995 | sv_setnv (sv, (NV)errno); |
|
|
996 | sv_setpv (sv, db_strerror (errno)); |
|
|
997 | SvNOK_on (sv); /* what a wonderful hack! */ |
|
|
998 | // ^^^ copied from perl sources |
|
|
999 | return 0; |
|
|
1000 | } |
|
|
1001 | |
|
|
1002 | return PL_vtbl_sv.svt_get (aTHX_ sv, mg); |
|
|
1003 | } |
|
|
1004 | |
|
|
1005 | static MGVTBL vtbl_errno; |
|
|
1006 | |
|
|
1007 | // this wonderful hack :( patches perl's $! variable to support our errno values |
|
|
1008 | static void |
|
|
1009 | patch_errno (void) |
|
|
1010 | { |
|
|
1011 | SV *sv; |
|
|
1012 | MAGIC *mg; |
|
|
1013 | |
|
|
1014 | if (!(sv = get_sv ("!", 1))) |
|
|
1015 | return; |
|
|
1016 | |
|
|
1017 | if (!(mg = mg_find (sv, PERL_MAGIC_sv))) |
|
|
1018 | return; |
|
|
1019 | |
|
|
1020 | if (mg->mg_virtual != &PL_vtbl_sv) |
|
|
1021 | return; |
|
|
1022 | |
|
|
1023 | vtbl_errno = PL_vtbl_sv; |
|
|
1024 | vtbl_errno.svt_get = errno_get; |
|
|
1025 | mg->mg_virtual = &vtbl_errno; |
951 | } |
1026 | } |
952 | |
1027 | |
953 | MODULE = BDB PACKAGE = BDB |
1028 | MODULE = BDB PACKAGE = BDB |
954 | |
1029 | |
955 | PROTOTYPES: ENABLE |
1030 | PROTOTYPES: ENABLE |
… | |
… | |
1153 | X_MUTEX_CHECK (reslock); |
1228 | X_MUTEX_CHECK (reslock); |
1154 | X_MUTEX_CHECK (reqlock); |
1229 | X_MUTEX_CHECK (reqlock); |
1155 | |
1230 | |
1156 | X_COND_CHECK (reqwait); |
1231 | X_COND_CHECK (reqwait); |
1157 | #endif |
1232 | #endif |
|
|
1233 | patch_errno (); |
1158 | } |
1234 | } |
1159 | |
1235 | |
1160 | void |
1236 | void |
1161 | max_poll_reqs (int nreqs) |
1237 | max_poll_reqs (int nreqs) |
1162 | PROTOTYPE: $ |
1238 | PROTOTYPE: $ |
… | |
… | |
1320 | } |
1396 | } |
1321 | OUTPUT: |
1397 | OUTPUT: |
1322 | RETVAL |
1398 | RETVAL |
1323 | |
1399 | |
1324 | void |
1400 | void |
1325 | db_env_open (DB_ENV *env, octetstring db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) |
1401 | db_env_open (DB_ENV *env, bdb_filename db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) |
1326 | CODE: |
1402 | CODE: |
1327 | { |
1403 | { |
1328 | dREQ (REQ_ENV_OPEN); |
1404 | dREQ (REQ_ENV_OPEN); |
1329 | |
|
|
1330 | env->set_thread_count (env, wanted + 2); |
|
|
1331 | |
1405 | |
1332 | req->env = env; |
1406 | req->env = env; |
1333 | req->uint1 = open_flags | DB_THREAD; |
1407 | req->uint1 = open_flags | DB_THREAD; |
1334 | req->int1 = mode; |
1408 | req->int1 = mode; |
1335 | req->buf1 = strdup_ornull (db_home); |
1409 | req->buf1 = strdup_ornull (db_home); |
… | |
… | |
1403 | } |
1477 | } |
1404 | OUTPUT: |
1478 | OUTPUT: |
1405 | RETVAL |
1479 | RETVAL |
1406 | |
1480 | |
1407 | void |
1481 | 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) |
1482 | 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: |
1483 | CODE: |
1410 | { |
1484 | { |
1411 | dREQ (REQ_DB_OPEN); |
1485 | dREQ (REQ_DB_OPEN); |
1412 | req->db = db; |
1486 | req->db = db; |
1413 | req->txn = txnid; |
1487 | req->txn = txnid; |
… | |
… | |
1453 | req->uint1 = flags; |
1527 | req->uint1 = flags; |
1454 | REQ_SEND; |
1528 | REQ_SEND; |
1455 | } |
1529 | } |
1456 | |
1530 | |
1457 | void |
1531 | void |
1458 | db_upgrade (DB *db, octetstring file, U32 flags = 0, SV *callback = &PL_sv_undef) |
1532 | db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = &PL_sv_undef) |
1459 | CODE: |
1533 | CODE: |
1460 | { |
1534 | { |
1461 | dREQ (REQ_DB_SYNC); |
1535 | dREQ (REQ_DB_SYNC); |
1462 | req->db = db; |
1536 | req->db = db; |
1463 | req->buf1 = strdup (file); |
1537 | req->buf1 = strdup (file); |
… | |
… | |
1713 | |
1787 | |
1714 | |
1788 | |
1715 | MODULE = BDB PACKAGE = BDB::Env |
1789 | MODULE = BDB PACKAGE = BDB::Env |
1716 | |
1790 | |
1717 | void |
1791 | void |
1718 | DESTROY (DB_ENV_ornull *env) |
1792 | DESTROY (DB_ENV_ornuked *env) |
1719 | CODE: |
1793 | CODE: |
1720 | if (env) |
1794 | if (env) |
1721 | env->close (env, 0); |
1795 | env->close (env, 0); |
1722 | |
1796 | |
1723 | int set_data_dir (DB_ENV *env, const char *dir) |
1797 | int set_data_dir (DB_ENV *env, const char *dir) |
… | |
… | |
1870 | RETVAL |
1944 | RETVAL |
1871 | |
1945 | |
1872 | MODULE = BDB PACKAGE = BDB::Db |
1946 | MODULE = BDB PACKAGE = BDB::Db |
1873 | |
1947 | |
1874 | void |
1948 | void |
1875 | DESTROY (DB_ornull *db) |
1949 | DESTROY (DB_ornuked *db) |
1876 | CODE: |
1950 | CODE: |
1877 | if (db) |
1951 | if (db) |
1878 | { |
1952 | { |
1879 | SV *env = (SV *)db->app_private; |
1953 | SV *env = (SV *)db->app_private; |
1880 | db->close (db, 0); |
1954 | db->close (db, 0); |
… | |
… | |
1975 | |
2049 | |
1976 | |
2050 | |
1977 | MODULE = BDB PACKAGE = BDB::Txn |
2051 | MODULE = BDB PACKAGE = BDB::Txn |
1978 | |
2052 | |
1979 | void |
2053 | void |
1980 | DESTROY (DB_TXN_ornull *txn) |
2054 | DESTROY (DB_TXN_ornuked *txn) |
1981 | CODE: |
2055 | CODE: |
1982 | if (txn) |
2056 | if (txn) |
1983 | txn->abort (txn); |
2057 | txn->abort (txn); |
1984 | |
2058 | |
1985 | int set_timeout (DB_TXN *txn, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT) |
2059 | int set_timeout (DB_TXN *txn, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT) |
… | |
… | |
1996 | |
2070 | |
1997 | |
2071 | |
1998 | MODULE = BDB PACKAGE = BDB::Cursor |
2072 | MODULE = BDB PACKAGE = BDB::Cursor |
1999 | |
2073 | |
2000 | void |
2074 | void |
2001 | DESTROY (DBC_ornull *dbc) |
2075 | DESTROY (DBC_ornuked *dbc) |
2002 | CODE: |
2076 | CODE: |
2003 | if (dbc) |
2077 | if (dbc) |
2004 | dbc->c_close (dbc); |
2078 | dbc->c_close (dbc); |
2005 | |
2079 | |
2006 | #if DB_VERSION_MINOR >= 6 |
2080 | #if DB_VERSION_MINOR >= 6 |
… | |
… | |
2012 | #endif |
2086 | #endif |
2013 | |
2087 | |
2014 | MODULE = BDB PACKAGE = BDB::Sequence |
2088 | MODULE = BDB PACKAGE = BDB::Sequence |
2015 | |
2089 | |
2016 | void |
2090 | void |
2017 | DESTROY (DB_SEQUENCE_ornull *seq) |
2091 | DESTROY (DB_SEQUENCE_ornuked *seq) |
2018 | CODE: |
2092 | CODE: |
2019 | if (seq) |
2093 | if (seq) |
2020 | seq->close (seq, 0); |
2094 | seq->close (seq, 0); |
2021 | |
2095 | |
2022 | int initial_value (DB_SEQUENCE *seq, db_seq_t value) |
2096 | int initial_value (DB_SEQUENCE *seq, db_seq_t value) |
… | |
… | |
2041 | CODE: |
2115 | CODE: |
2042 | RETVAL = seq->set_range (seq, min, max); |
2116 | RETVAL = seq->set_range (seq, min, max); |
2043 | OUTPUT: |
2117 | OUTPUT: |
2044 | RETVAL |
2118 | RETVAL |
2045 | |
2119 | |
|
|
2120 | |