Add port tcl-mmap -- Tcl interface to mmap(2).

This commit is contained in:
Mikhail Teterin 2013-02-05 00:15:35 +00:00
parent 61dd78bf20
commit fe8e26f42a
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=311658
8 changed files with 310 additions and 0 deletions

View file

@ -4222,6 +4222,7 @@
SUBDIR += talloc
SUBDIR += tbb
SUBDIR += tcl-memchan
SUBDIR += tcl-mmap
SUBDIR += tcl-signal
SUBDIR += tcl-trf
SUBDIR += tclcheck

27
devel/tcl-mmap/Makefile Normal file
View file

@ -0,0 +1,27 @@
# Created by: Mikhail Teterin <mi@aldan.algebra.com>
# $FreeBSD$
PORTNAME= tcl-mmap
PORTVERSION= 1.1
CATEGORIES= devel tcl
MASTER_SITES= SF
MAINTAINER= tcltk@FreeBSD.org
COMMENT= New TCL channel type using mmap-ed files
MAKEFILE= ${FILESDIR}/Makefile.bsd
USE_TCL= 84+
MAKE_ENV+= TCL_VER=${TCL_VER} MKDIR="${MKDIR}" \
PORTVERSION="${PORTVERSION}" \
FILESDIR="${FILESDIR}" \
TCLSH="${TCLSH}" \
INSTALL_DATA="${INSTALL_DATA}"
PLIST_DIRS= lib/tcl${TCL_VER}/mmap
PLIST_FILES= ${PLIST_DIRS}/pkgIndex.tcl ${PLIST_DIRS}/libmmap${PORTVERSION}.so
MANN= tcl-mmap.n
MANCOMPRESSED= maybe
.include <bsd.port.mk>

2
devel/tcl-mmap/distinfo Normal file
View file

@ -0,0 +1,2 @@
SHA256 (tcl-mmap-1.1.tar.gz) = e41dd103866437eb0e4ed42671be6587de0d2787998391cd5b9cc8fc67bf0bb0
SIZE (tcl-mmap-1.1.tar.gz) = 276254

View file

@ -0,0 +1,36 @@
LIBNAME= mmap${PORTVERSION}
SHLIB_NAME= lib${LIBNAME}.so
.PATH: ${.CURDIR}/unix ${.CURDIR}/doc
SRCS= mmap.c
MAN= tcl-mmap.n
TCL_VER?= 8.3
LOCALBASE?= /usr/local
CFLAGS+= -I${LOCALBASE}/include/tcl${TCL_VER}
LDADD= -L${LOCALBASE}/lib -ltcl${TCL_VER:S/.//}
all: pkgIndex.tcl test
pkgIndex.tcl:
echo 'package ifneeded mmap ${PORTVERSION} \
[list load [file join $$dir $(SHLIB_NAME)]]' > pkgIndex.tcl
DIR = lib/tcl${TCL_VER}/mmap
LIBDIR = ${PREFIX}/${DIR}
MANDIR = ${PREFIX}/man/man
WARNS = 3
${LIBDIR}:
${MKDIR} ${LIBDIR}
beforeinstall: ${LIBDIR} pkgIndex.tcl
${INSTALL_DATA} pkgIndex.tcl ${LIBDIR}/
.include <bsd.lib.mk>
test: ${SHLIB_NAME} pkgIndex.tcl
cd tests && ${TCLSH} ${FILESDIR}/alltests.tcl

View file

@ -0,0 +1,6 @@
lappend auto_path ..
foreach t [glob *.tcl] {
puts "Running tests from $t"
source $t
}

View file

@ -0,0 +1,79 @@
--- tests/mmap_err.tcl 2008-04-12 19:45:49.000000000 -0400
+++ tests/mmap_err.tcl 2013-02-04 18:38:40.000000000 -0500
@@ -1,29 +1,10 @@
#!/usr/bin/tclsh
package require mmap
-set fd [open /tmp/messages r]
-set mp [mmap -length 30 -offset 4095 $fd]
-close $fd
-
-puts Gets:
-for {set i 0} {$i < 10} {incr i} {
- gets $mp line
- puts $line
+set fd [open messages r]
+if {[catch {mmap -length 30 -offset 4095 $fd} mp]} {
+ close $fd
+ puts "Using offset 4095 resulted in error ($mp), as expected. Good"
+} else {
+ puts "Offset 4095 should've caused an error. Test failed"
+ exit 1
}
-close $mp
-
-set fd [open /tmp/a r+]
-set mp [mmap -shared -length 30 $fd]
-close $fd
-
-flush $mp
-
-seek $mp 1
-puts -nonewline $mp alex
-seek $mp 0
-
-puts Read:
-puts [read $mp]
-
-puts [eof $mp]
-
-close $mp
--- tests/test_mem2.tcl 2008-04-21 08:34:42.000000000 -0400
+++ tests/test_mem2.tcl 2013-02-04 18:41:57.000000000 -0500
@@ -2,10 +2,10 @@
package require mmap
-catch { file copy tests/testfile tests/testfile1 }
-set fd [open tests/testfile1 r+]
+catch { file copy testfile testfile1 }
+set fd [open testfile1 r+]
set mp [mmap -shared -length 30 $fd]
close $fd
-while {1} {
+for {set i 0} {$i < 3} {incr i} {
flush $mp
@@ -23,3 +23,3 @@
close $mp
-file delete tests/testfile1
+file delete testfile1
--- tests/test_mem.tcl 2008-04-21 08:35:36.000000000 -0400
+++ tests/test_mem.tcl 2013-02-04 18:43:29.000000000 -0500
@@ -2,7 +2,7 @@
package require mmap
-while {1} {
-catch { file copy tests/testfile tests/testfile1 }
-set fd [open tests/testfile1 r+]
+for {set i 0} {$i < 3} {incr i} {
+catch { file copy testfile testfile1 }
+set fd [open testfile1 r+]
set mp [mmap -shared -length 30 $fd]
close $fd
@@ -21,4 +21,4 @@
close $mp
-file delete tests/testfile1
+file delete testfile1
}

View file

@ -0,0 +1,148 @@
Submitted upstream as:
https://sourceforge.net/tracker/?func=detail&aid=3603360&group_id=224597&atid=1062254
--- unix/mmap.c 2008-04-21 08:47:04.000000000 -0400
+++ unix/mmap.c 2013-02-04 18:18:52.000000000 -0500
@@ -37,23 +37,16 @@
*/
-static int MmapCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]);
+Tcl_PackageInitProc Mmap_Init;
-void NewChannelName(char *name, CONST char* prefix);
+static Tcl_ObjCmdProc MmapCmd;
-static int Input(ClientData instanceData, char *buf, int bufSize, int *errorCodePtr);
+static void NewChannelName(char *name, const char* prefix);
-static int Output(ClientData instanceData, const char *buf, int bufSize, int *errorCodePtr);
-
-static int Seek(ClientData instanceData, long offset, int seekMode, int *errorCodePtr);
-
-static int Close(ClientData instanceData, Tcl_Interp *interp);
-
-static void Watch(ClientData instanceData, int mask);
-
-//static int BlockMode(ClientData instanceData, int mode);
-
-static int GetHandle(ClientData instanceData, int direction, ClientData *handlePtr);
-
-//static void catch_signal(int sig_num);
+static Tcl_DriverInputProc Input;
+static Tcl_DriverOutputProc Output;
+static Tcl_DriverSeekProc Seek;
+static Tcl_DriverCloseProc Close;
+static Tcl_DriverWatchProc Watch;
+static Tcl_DriverGetHandleProc GetHandle;
/*
@@ -62,19 +55,12 @@
static Tcl_ChannelType channelType = {
- "mmap", /* Channel type name */
- TCL_CHANNEL_VERSION_2,
- (Tcl_DriverCloseProc *) Close,
- (Tcl_DriverInputProc *) Input,
- (Tcl_DriverOutputProc *) Output,
- (Tcl_DriverSeekProc *) Seek,
- (Tcl_DriverSetOptionProc *) NULL, /* no channel type specific options */
- (Tcl_DriverGetOptionProc *) NULL, /* no channel type specific options */
- (Tcl_DriverWatchProc *) Watch,
- (Tcl_DriverGetHandleProc *) GetHandle,
- (Tcl_DriverClose2Proc *) NULL, /* no support for closing the read and write sides independently */
- (Tcl_DriverBlockModeProc *) NULL, /* mmap channels are always in non-blocking mode */
- //(Tcl_DriverBlockModeProc *) BlockMode,
- (Tcl_DriverFlushProc *) NULL, /* currently reserved for future use. It should be set to NULL */
- (Tcl_DriverHandlerProc *) NULL /* this is not a stacked channel driver */
+ .typeName = "mmap", /* Channel type name */
+ .version = TCL_CHANNEL_VERSION_2,
+ .closeProc = Close,
+ .inputProc = Input,
+ .outputProc = Output,
+ .seekProc = Seek,
+ .watchProc = Watch,
+ .getHandleProc = GetHandle
};
@@ -129,9 +115,9 @@
}
- if (bufSize > data->length - data->seek) {
+ if (bufSize > (int)(data->length - data->seek)) {
bufSize = data->length - data->seek;
}
- memcpy((char *)buf, (char *) (data->addr + data->seek), (size_t) bufSize);
+ memcpy(buf, data->addr + data->seek, (size_t)bufSize);
data->seek += bufSize;
@@ -144,10 +130,10 @@
int ret;
- if (bufSize > data->length - data->seek) {
+ if (bufSize > (int)(data->length - data->seek)) {
bufSize = data->length - data->seek;
- memcpy((char *) (data->addr + data->seek), (char *) buf, (size_t) bufSize);
+ memcpy(data->addr + data->seek, buf, (size_t)bufSize);
data->seek = 0;
} else {
- memcpy((char *) (data->addr + data->seek), (char *) buf, (size_t) bufSize);
+ memcpy(data->addr + data->seek, buf, (size_t)bufSize);
data->seek += bufSize;
}
@@ -163,4 +149,5 @@
static int Seek(ClientData instanceData, long offset, int seekMode, int *errorCodePtr) {
ChannelInstance* data;
+ off_t seek;
data = (ChannelInstance*) instanceData;
@@ -178,21 +165,20 @@
switch (seekMode) {
case SEEK_SET:
- data->seek = offset;
+ seek = offset;
break;
case SEEK_CUR:
- data->seek += offset;
+ seek += offset;
break;
case SEEK_END:
- data->seek = data->length + offset;
+ seek = data->length + offset;
break;
}
/* We check if seek is within range, and fix */
- if (data->seek < 0) data->seek = 0;
- if (data->seek > data->length) data->seek = data->length;
-
- return data->seek;
+ if (seek < 0) seek = 0;
+ if (seek > (off_t)data->length) seek = data->length;
+ return data->seek = seek;
}
@@ -255,5 +241,5 @@
*/
static int MmapCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
- static char* cmds[] = { "-shared", "-length", "-offset", NULL };
+ static const char* cmds[] = { "-shared", "-length", "-offset", NULL };
int index;
int ret;
@@ -329,5 +315,5 @@
ClientData cd;
ret = Tcl_GetChannelHandle(chan, mode, (ClientData *) &cd);
- fd = (int) cd; // This should be ok because the pointer size in bytes is at least as may bytes as 'int'
+ fd = (intptr_t) cd; // This should be ok because the pointer size in bytes is at least as may bytes as 'int'
if (ret != TCL_OK) {
Tcl_SetResult(interp, "Could not get file handle", TCL_STATIC);
@@ -384,5 +370,5 @@
}
-void NewChannelName(char *name, CONST char* prefix) {
+void NewChannelName(char *name, const char* prefix) {
TCL_DECLARE_MUTEX (mmapCounterMutex)
static unsigned long mmapCounter = 0;

11
devel/tcl-mmap/pkg-descr Normal file
View file

@ -0,0 +1,11 @@
This extension provides a Tcl interface to the mmap(2) POSIX system
call.
The functionality of 'mmap' is exported from this extension in the
form of a new Tcl channel type, named "mmap". A memory mapping is
established with the 'mmap' command. Following 'mmap' execution,
access to the memory mapped file is done via the standard Tcl
commands: puts/gets/seek/flush/close/fconfigure, only that this
time these commands operate on memory, rather than on a file.
WWW: http://tcl-mmap.sourceforge.net/