Skip to content

Commit efd314e

Browse files
committed
-Fix memory handling of persistent dba connections.
-Update tests. # cdb and flatfile still FAIL for dba_popen since the known streams problem
1 parent 710e49a commit efd314e

11 files changed

+65
-51
lines changed

ext/dba/dba.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -215,25 +215,25 @@ static int le_pdb;
215215
static void dba_close(dba_info *info TSRMLS_DC)
216216
{
217217
if (info->hnd) info->hnd->close(info TSRMLS_CC);
218-
if (info->path) efree(info->path);
218+
if (info->path) pefree(info->path, info->flags&DBA_PERSISTENT);
219219
if (info->fp && info->fp!=info->lock.fp) php_stream_close(info->fp);
220220
if (info->lock.fd) {
221221
php_flock(info->lock.fd, LOCK_UN);
222222
/*close(info->lock.fd);*/
223223
info->lock.fd = 0;
224224
}
225225
if (info->lock.fp) php_stream_close(info->lock.fp);
226-
if (info->lock.name) efree(info->lock.name);
227-
efree(info);
226+
if (info->lock.name) pefree(info->lock.name, info->flags&DBA_PERSISTENT);
227+
pefree(info, info->flags&DBA_PERSISTENT);
228228
}
229229
/* }}} */
230230

231231
/* {{{ dba_close_rsrc
232232
*/
233233
static void dba_close_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
234234
{
235-
dba_info *info = (dba_info *)rsrc->ptr;
236-
235+
dba_info *info = (dba_info *)rsrc->ptr;
236+
237237
dba_close(info TSRMLS_CC);
238238
}
239239
/* }}} */
@@ -514,7 +514,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
514514
info->mode = modenr;
515515
info->argc = ac - 3;
516516
info->argv = args + 3;
517-
info->flags = (hptr->flags & ~DBA_LOCK_ALL) | (lock_flag & DBA_LOCK_ALL);
517+
info->flags = (hptr->flags & ~DBA_LOCK_ALL) | (lock_flag & DBA_LOCK_ALL) | (persistent ? DBA_PERSISTENT : 0);
518518
info->lock.mode = lock_mode;
519519

520520
/* if any open call is a locking call:

ext/dba/dba_cdb.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ DBA_OPEN_FUNC(cdb)
9898
return FAILURE;
9999
}
100100

101-
cdb = emalloc(sizeof(dba_cdb));
101+
cdb = pemalloc(sizeof(dba_cdb), info->flags&DBA_PERSISTENT);
102102
memset(cdb, 0, sizeof(dba_cdb));
103103

104104
#if DBA_CDB_BUILTIN
@@ -132,7 +132,7 @@ DBA_CLOSE_FUNC(cdb)
132132
cdb_free(&cdb->c);
133133
close(cdb->file);
134134
#endif
135-
efree(cdb);
135+
pefree(cdb, info->flags&DBA_PERSISTENT);
136136
}
137137

138138
#if DBA_CDB_BUILTIN

ext/dba/dba_db2.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ DBA_OPEN_FUNC(db2)
7676
return FAILURE;
7777
}
7878

79-
info->dbf = emalloc(sizeof(dba_db2_data));
79+
info->dbf = pemalloc(sizeof(dba_db2_data), info->flags&DBA_PERSISTENT);
8080
memset(info->dbf, 0, sizeof(dba_db2_data));
8181
((dba_db2_data *) info->dbf)->dbp = dbp;
8282
return SUCCESS;
@@ -89,7 +89,7 @@ DBA_CLOSE_FUNC(db2)
8989
if (dba->cursor)
9090
dba->cursor->c_close(dba->cursor);
9191
dba->dbp->close(dba->dbp, 0);
92-
efree(dba);
92+
pefree(dba, info->flags&DBA_PERSISTENT);
9393
}
9494

9595
DBA_FETCH_FUNC(db2)

ext/dba/dba_db3.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ DBA_OPEN_FUNC(db3)
8181
#endif
8282
dba_db3_data *data;
8383

84-
data = emalloc(sizeof(*data));
84+
data = pemalloc(sizeof(*data), info->flags&DBA_PERSISTENT);
8585
data->dbp = dbp;
8686
data->cursor = NULL;
8787
info->dbf = data;
@@ -100,7 +100,7 @@ DBA_CLOSE_FUNC(db3)
100100

101101
if (dba->cursor) dba->cursor->c_close(dba->cursor);
102102
dba->dbp->close(dba->dbp, 0);
103-
efree(dba);
103+
pefree(dba, info->flags&DBA_PERSISTENT);
104104
}
105105

106106
DBA_FETCH_FUNC(db3)

ext/dba/dba_db4.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ DBA_OPEN_FUNC(db4)
8181
#endif
8282
dba_db4_data *data;
8383

84-
data = emalloc(sizeof(*data));
84+
data = pemalloc(sizeof(*data), info->flags&DBA_PERSISTENT);
8585
data->dbp = dbp;
8686
data->cursor = NULL;
8787
info->dbf = data;
@@ -100,7 +100,7 @@ DBA_CLOSE_FUNC(db4)
100100

101101
if (dba->cursor) dba->cursor->c_close(dba->cursor);
102102
dba->dbp->close(dba->dbp, 0);
103-
efree(dba);
103+
pefree(dba, info->flags&DBA_PERSISTENT);
104104
}
105105

106106
DBA_FETCH_FUNC(db4)

ext/dba/dba_dbm.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ DBA_OPEN_FUNC(dbm)
7878
return FAILURE;
7979
}
8080

81-
info->dbf = emalloc(sizeof(dba_dbm_data));
81+
info->dbf = pemalloc(sizeof(dba_dbm_data), info->flags&DBA_PERSISTENT);
8282
memset(info->dbf, 0, sizeof(dba_dbm_data));
8383
return SUCCESS;
8484
}
8585

8686
DBA_CLOSE_FUNC(dbm)
8787
{
88-
efree(info->dbf);
88+
pefree(info->dbf, info->flags&DBA_PERSISTENT);
8989
dbmclose();
9090
}
9191

ext/dba/dba_flatfile.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
DBA_OPEN_FUNC(flatfile)
4343
{
44-
info->dbf = emalloc(sizeof(flatfile));
44+
info->dbf = pemalloc(sizeof(flatfile), info->flags&DBA_PERSISTENT);
4545
memset(info->dbf, 0, sizeof(flatfile));
4646

4747
((flatfile*)info->dbf)->fp = info->fp;
@@ -55,7 +55,7 @@ DBA_CLOSE_FUNC(flatfile)
5555

5656
if (dba->nextkey.dptr)
5757
efree(dba->nextkey.dptr);
58-
efree(dba);
58+
pefree(dba, info->flags&DBA_PERSISTENT);
5959
}
6060

6161
DBA_FETCH_FUNC(flatfile)

ext/dba/dba_gdbm.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DBA_OPEN_FUNC(gdbm)
5959
dbf = gdbm_open(info->path, 0, gmode, filemode, NULL);
6060

6161
if(dbf) {
62-
info->dbf = emalloc(sizeof(dba_gdbm_data));
62+
info->dbf = pemalloc(sizeof(dba_gdbm_data), info->flags&DBA_PERSISTENT);
6363
memset(info->dbf, 0, sizeof(dba_gdbm_data));
6464
((dba_gdbm_data *) info->dbf)->dbf = dbf;
6565
return SUCCESS;
@@ -74,7 +74,7 @@ DBA_CLOSE_FUNC(gdbm)
7474

7575
if(dba->nextkey.dptr) free(dba->nextkey.dptr);
7676
gdbm_close(dba->dbf);
77-
efree(dba);
77+
pefree(dba, info->flags&DBA_PERSISTENT);
7878
}
7979

8080
DBA_FETCH_FUNC(gdbm)

ext/dba/php_dba.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typedef struct dba_info {
6363
#define DBA_LOCK_WCT (DBA_LOCK_WRITER|DBA_LOCK_CREAT|DBA_LOCK_TRUNC)
6464

6565
#define DBA_STREAM_OPEN (0x0010)
66+
#define DBA_PERSISTENT (0x0020)
6667

6768
extern zend_module_entry dba_module_entry;
6869
#define dba_module_ptr &dba_module_entry

ext/dba/tests/dba_cdb.phpt

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ DBA CDB handler test
44
<?php
55
require_once('skipif.inc');
66
if (!in_array('cdb', dba_handlers())) die('skip CDB handler not available');
7-
die('skip CDB does not support replace or delete');
7+
die('info CDB does not support replace or delete');
88
?>
99
--FILE--
1010
<?php
@@ -14,17 +14,17 @@ DBA CDB handler test
1414
?>
1515
--EXPECT--
1616
database handler: cdb
17-
3NYNYY
17+
5YYYYY
1818
Content String 2
19-
Content 2 replaced
20-
Read during write: not allowed
21-
Content 2 replaced 2nd time
22-
The 6th value
23-
array(3) {
24-
["key number 6"]=>
25-
string(13) "The 6th value"
19+
array(5) {
20+
["key1"]=>
21+
string(16) "Content String 1"
2622
["key2"]=>
27-
string(27) "Content 2 replaced 2nd time"
23+
string(16) "Content String 2"
24+
["key3"]=>
25+
string(20) "Third Content String"
26+
["key4"]=>
27+
string(22) "Another Content String"
2828
["key5"]=>
2929
string(23) "The last content string"
30-
}
30+
}

ext/dba/tests/dba_handler.inc

+33-20
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
dba_insert("key3", "Third Content String", $db_file);
77
dba_insert("key4", "Another Content String", $db_file);
88
dba_insert("key5", "The last content string", $db_file);
9-
dba_delete("key3", $db_file);
10-
dba_delete("key1", $db_file);
9+
if ($handler != 'cdb') {
10+
dba_delete("key3", $db_file);
11+
dba_delete("key1", $db_file);
12+
} else {
13+
dba_close($db_file);
14+
if (($db_file = dba_open($db_filename, 'r'.$lock_flag, $handler))===FALSE) {
15+
echo "Error reopening database\n";
16+
}
17+
}
1118
$a = dba_firstkey($db_file);
1219
$i=0;
1320
while($a) {
@@ -20,28 +27,32 @@
2027
}
2128
echo "\n";
2229
echo dba_fetch("key2", $db_file)."\n";
23-
dba_replace("key2", "Content 2 replaced", $db_file);
24-
echo dba_fetch("key2", $db_file)."\n";
30+
if ($handler != 'cdb') {
31+
dba_replace("key2", "Content 2 replaced", $db_file);
32+
echo dba_fetch("key2", $db_file)."\n";
33+
}
2534
dba_close($db_file);
2635
} else {
2736
echo "Error creating database\n";
2837
}
29-
$db_writer = dba_open($db_filename, 'w'.$lock_flag, $handler);
30-
if (($dba_reader = @dba_open($db_filename, 'r'.$lock_flag.($lock_flag ? 't' : ''), $handler))===false) {
31-
echo "Read during write: not allowed\n";
32-
} else {
33-
echo "Read during write: allowed\n";
34-
}
35-
if ($db_writer!==FALSE) {
36-
dba_insert("key number 6", "The 6th value", $db_writer);
37-
@dba_insert("key number 6", "The 6th value inserted again would be an error", $db_writer);
38-
dba_replace("key2", "Content 2 replaced 2nd time", $db_writer);
39-
dba_delete("key4", $db_writer);
40-
echo dba_fetch("key2", $db_writer)."\n";
41-
echo dba_fetch("key number 6", $db_writer)."\n";
42-
dba_close($db_writer); // when the writer is open at least db3 would fail because of buffered io.
43-
} else {
44-
die("Error reopening database\n");
38+
if ($handler != 'cdb') {
39+
$db_writer = dba_open($db_filename, 'w'.$lock_flag, $handler);
40+
if (($dba_reader = @dba_open($db_filename, 'r'.$lock_flag.($lock_flag ? 't' : ''), $handler))===false) {
41+
echo "Read during write: not allowed\n";
42+
} else {
43+
echo "Read during write: allowed\n";
44+
}
45+
if ($db_writer!==FALSE) {
46+
dba_insert("key number 6", "The 6th value", $db_writer);
47+
@dba_insert("key number 6", "The 6th value inserted again would be an error", $db_writer);
48+
dba_replace("key2", "Content 2 replaced 2nd time", $db_writer);
49+
dba_delete("key4", $db_writer);
50+
echo dba_fetch("key2", $db_writer)."\n";
51+
echo dba_fetch("key number 6", $db_writer)."\n";
52+
dba_close($db_writer); // when the writer is open at least db3 would fail because of buffered io.
53+
} else {
54+
die("Error reopening database\n");
55+
}
4556
}
4657
if (($db_file = dba_open($db_filename, 'r'.$lock_flag, $handler))!==FALSE) {
4758
$key = dba_firstkey($db_file);
@@ -59,4 +70,6 @@
5970
if ($dba_reader) {
6071
dba_close($dba_reader);
6172
}
73+
if (($db_file = dba_popen($db_filename, 'r'.($handler!='gdbm'?'-':''), $handler))!==FALSE) {
74+
}
6275
?>

0 commit comments

Comments
 (0)