diff options
author | David Reid <dreid@apache.org> | 1999-10-20 20:22:05 +0000 |
---|---|---|
committer | David Reid <dreid@apache.org> | 1999-10-20 20:22:05 +0000 |
commit | 03daf63394c3ac82abd98b76a3dd7f294a3c10de (patch) | |
tree | 0a22e51653b9dc54321fa4d11b6b9c05118298e1 /mmap | |
parent | a064763307a8a953bff150fef8d665c102f538a2 (diff) | |
download | apr-03daf63394c3ac82abd98b76a3dd7f294a3c10de.tar.gz |
The beginning of mmap support for APR. Still early days but what's here
has been tested and seems to work. Next is the ability to read/send an
mmap'd file.
git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@59389 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'mmap')
-rw-r--r-- | mmap/beos/Makefile.in | 52 | ||||
-rw-r--r-- | mmap/beos/mmap.c | 156 | ||||
-rw-r--r-- | mmap/beos/mmap_h.h | 77 | ||||
-rw-r--r-- | mmap/unix/Makefile.in | 52 | ||||
-rw-r--r-- | mmap/unix/mmap.c | 136 | ||||
-rw-r--r-- | mmap/unix/mmap_h.h | 75 |
6 files changed, 548 insertions, 0 deletions
diff --git a/mmap/beos/Makefile.in b/mmap/beos/Makefile.in new file mode 100644 index 000000000..d03436ef7 --- /dev/null +++ b/mmap/beos/Makefile.in @@ -0,0 +1,52 @@ +#CFLAGS=$(OPTIM) $(CFLAGS1) $(EXTRA_CFLAGS) +#LIBS=$(EXTRA_LIBS) $(LIBS1) +#INCLUDES=$(INCLUDES1) $(INCLUDES0) $(EXTRA_INCLUDES) +#LDFLAGS=$(LDFLAGS1) $(EXTRA_LDFLAGS) + +RM=@RM@ +CC=@CC@ +RANLIB=@RANLIB@ +CFLAGS=@CFLAGS@ @OPTIM@ +LIBS=@LIBS@ +LDFLAGS=@LDFLAGS@ $(LIBS) +INCDIR=../../inc +INCDIR1=../../include +INCLUDES=-I$(INCDIR) -I$(INCDIR1) -I. + +LIB=libmmap.a + +OBJS=mmap.o + +.c.o: + $(CC) $(CFLAGS) -c $(INCLUDES) $< + +all: $(LIB) + +clean: + $(RM) -f *.o *.a *.so + +distclean: clean + -$(RM) -f Makefile + +$(OBJS): Makefile + +$(LIB): $(OBJS) + $(RM) -f $@ + $(AR) cr $@ $(OBJS) + $(RANLIB) $@ + +# +# We really don't expect end users to use this rule. It works only with +# gcc, and rebuilds Makefile.tmpl. You have to re-run Configure after +# using it. +# +depend: + cp Makefile.in Makefile.in.bak \ + && sed -ne '1,/^# DO NOT REMOVE/p' Makefile.in > Makefile.new \ + && gcc -MM $(INCLUDES) $(CFLAGS) *.c >> Makefile.new \ + && sed -e '1,$$s: $(INCDIR)/: $$(INCDIR)/:g' \ + -e '1,$$s: $(OSDIR)/: $$(OSDIR)/:g' Makefile.new \ + > Makefile.in \ + && rm Makefile.new + +# DO NOT REMOVE diff --git a/mmap/beos/mmap.c b/mmap/beos/mmap.c new file mode 100644 index 000000000..54b5b1586 --- /dev/null +++ b/mmap/beos/mmap.c @@ -0,0 +1,156 @@ +/* ==================================================================== + * Copyright (c) 1999 The Apache Group. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * 4. The names "Apache Server" and "Apache Group" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Group. + * For more information on the Apache Group and the Apache HTTP server + * project, please see <http://www.apache.org/>. + * + */ + +#include "mmap_h.h" +#include "apr_mmap.h" +#include "apr_general.h" +#include "apr_portable.h" +#include "apr_lib.h" +#include <kernel/OS.h> +#include <errno.h> +#include <string.h> +#include <stdio.h> + +ap_status_t mmap_cleanup(void *themmap) +{ + struct mmap_t *mm = themmap; + int rv; + rv = delete_area(mm->area); + + if (rv == 0) { + mm->mm = 0; + mm->area = -1; + return APR_SUCCESS; + } + else + return errno; +} + +ap_status_t ap_mmap_create(struct mmap_t **new, const char *fname, + ap_context_t *cont) +{ + struct stat st; + int fd; + void *mm; + struct mmap_t *next; + area_id aid; + char *areaname; + uint32 size; + int len; + + /* We really should check to see that we haven't already mmap'd + * this file before. Cycling the linked list will allow this. + */ + + (*new) = (struct mmap_t *)ap_palloc(cont, sizeof(struct mmap_t)); + + if (stat(fname, &st) == -1) { + /* we couldn't stat the file...probably doesn't exist! */ + return APR_ENOFILE; + } + if ((st.st_mode & S_IFMT) != S_IFREG) { + /* oh dear, we're only doing regular files at present... */ + return APR_EBADF; + } + size = ((st.st_size -1) / B_PAGE_SIZE) + 1; + + if ((fd = open(fname, O_RDONLY, 0)) == -1) { + return APR_EBADF; + } + + /* generate a unique name for this area */ + len = strlen(fname) > 22 ? 22 : strlen(fname); + areaname = malloc(sizeof(char) * 32); + strncpy(areaname, "beos_mmap:\0", 11); + strncat(areaname, fname + (strlen(fname)-len), len); + + aid = create_area(areaname, mm, B_ANY_ADDRESS, size * B_PAGE_SIZE, + B_FULL_LOCK, B_READ_AREA|B_WRITE_AREA); + free(areaname); + if (aid >= B_NO_ERROR) + read(fd, mm, st.st_size); + + close (fd); + if (aid < B_NO_ERROR) { + /* we failed to get an mmap'd file... */ + return APR_ENOMEM; + } + + (*new)->filename = ap_pstrdup(cont, fname); + (*new)->mm = mm; + (*new)->sinfo = st; + (*new)->size = st.st_size; + (*new)->area = aid; + (*new)->cntxt = cont; + + /* register the cleanup... */ + ap_register_cleanup((*new)->cntxt, (void*)(*new), mmap_cleanup, + ap_null_cleanup); + + return APR_SUCCESS; +} + +ap_status_t ap_mmap_delete(struct mmap_t *mmap) +{ + ap_status_t rv; + if (mm->area == -1) + return APR_ENOENT; + + if ((rv = mmap_cleanup(mmap)) == APR_SUCCESS) { + ap_kill_cleanup(mmap->cntxt, mmap, mmap_cleanup); + return APR_SUCCESS; + } + return rv; +} diff --git a/mmap/beos/mmap_h.h b/mmap/beos/mmap_h.h new file mode 100644 index 000000000..3a65f7622 --- /dev/null +++ b/mmap/beos/mmap_h.h @@ -0,0 +1,77 @@ +/* ==================================================================== + * Copyright (c) 1999 The Apache Group. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * 4. The names "Apache Server" and "Apache Group" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Group. + * For more information on the Apache Group and the Apache HTTP server + * project, please see <http://www.apache.org/>. + * + */ + +#ifndef MMAP_H_H +#define MMAP_H_H + +#include <sys/stat.h> +#include <kernel/OS.h> +#include "apr_general.h" +#include "apr_mmap.h" +#include "apr_errno.h" + +struct mmap_t { + ap_context_t *cntxt; + char *filename; /* the full path to the file */ + struct stat sinfo; + area_id area; + void *mm; + size_t size; +}; + +ap_status_t mmap_cleanup(void *); + +#endif /* ! FILE_IO_H */ + diff --git a/mmap/unix/Makefile.in b/mmap/unix/Makefile.in new file mode 100644 index 000000000..d03436ef7 --- /dev/null +++ b/mmap/unix/Makefile.in @@ -0,0 +1,52 @@ +#CFLAGS=$(OPTIM) $(CFLAGS1) $(EXTRA_CFLAGS) +#LIBS=$(EXTRA_LIBS) $(LIBS1) +#INCLUDES=$(INCLUDES1) $(INCLUDES0) $(EXTRA_INCLUDES) +#LDFLAGS=$(LDFLAGS1) $(EXTRA_LDFLAGS) + +RM=@RM@ +CC=@CC@ +RANLIB=@RANLIB@ +CFLAGS=@CFLAGS@ @OPTIM@ +LIBS=@LIBS@ +LDFLAGS=@LDFLAGS@ $(LIBS) +INCDIR=../../inc +INCDIR1=../../include +INCLUDES=-I$(INCDIR) -I$(INCDIR1) -I. + +LIB=libmmap.a + +OBJS=mmap.o + +.c.o: + $(CC) $(CFLAGS) -c $(INCLUDES) $< + +all: $(LIB) + +clean: + $(RM) -f *.o *.a *.so + +distclean: clean + -$(RM) -f Makefile + +$(OBJS): Makefile + +$(LIB): $(OBJS) + $(RM) -f $@ + $(AR) cr $@ $(OBJS) + $(RANLIB) $@ + +# +# We really don't expect end users to use this rule. It works only with +# gcc, and rebuilds Makefile.tmpl. You have to re-run Configure after +# using it. +# +depend: + cp Makefile.in Makefile.in.bak \ + && sed -ne '1,/^# DO NOT REMOVE/p' Makefile.in > Makefile.new \ + && gcc -MM $(INCLUDES) $(CFLAGS) *.c >> Makefile.new \ + && sed -e '1,$$s: $(INCDIR)/: $$(INCDIR)/:g' \ + -e '1,$$s: $(OSDIR)/: $$(OSDIR)/:g' Makefile.new \ + > Makefile.in \ + && rm Makefile.new + +# DO NOT REMOVE diff --git a/mmap/unix/mmap.c b/mmap/unix/mmap.c new file mode 100644 index 000000000..d9986dd78 --- /dev/null +++ b/mmap/unix/mmap.c @@ -0,0 +1,136 @@ +/* ==================================================================== + * Copyright (c) 1999 The Apache Group. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * 4. The names "Apache Server" and "Apache Group" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Group. + * For more information on the Apache Group and the Apache HTTP server + * project, please see <http://www.apache.org/>. + * + */ + +#include "mmap_h.h" +#include "apr_mmap.h" +#include "apr_general.h" +#include "apr_portable.h" +#include "apr_lib.h" +#include <sys/mman.h> +#include <errno.h> +#include <string.h> +#include <stdio.h> + +ap_status_t mmap_cleanup(void *themmap) +{ + struct mmap_t *mm = themmap; + int rv; + rv = munmap(mm->mm, mm->size); + + if (rv == 0) { + mm->mm = (caddr_t)-1; + return APR_SUCCESS; + } + else + return errno; +} + +ap_status_t ap_mmap_create(ap_mmap_t **new, const char * fname, + ap_context_t *cont) +{ + struct stat st; + int fd; + caddr_t mm; + ap_mmap_t next; + + /* before we go blindly in and create an mmap, we should probably check + * to make sure we haven't already mmap'd the file. + * I'll add this once I've got the basics working. + */ + + (*new) = (struct mmap_t *)ap_palloc(cont, sizeof(struct mmap_t)); + + if (stat(fname, &st) == -1) { + /* we couldn't stat the file...probably doesn't exist! */ + return APR_ENOFILE; + } + if ((st.st_mode & S_IFMT) != S_IFREG) { + /* oh dear, we're only doing regular files at present... */ + return APR_EBADF; + } + if ((fd = open(fname, O_RDONLY, 0)) == -1) { + return APR_EBADF; + } + mm = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd ,0); + close (fd); + if (mm == (caddr_t)-1) { + /* we failed to get an mmap'd file... */ + return APR_ENOMEM; + } + (*new)->filename = ap_pstrdup(cont, fname); + (*new)->mm = mm; + (*new)->sinfo = st; + (*new)->size = st.st_size; + (*new)->cntxt = cont; + + /* register the cleanup... */ + ap_register_cleanup((*new)->cntxt, (void*)(*new), mmap_cleanup, + ap_null_cleanup); + return APR_SUCCESS; +} + +ap_status_t ap_mmap_delete(struct mmap_t *mmap) +{ + ap_status_t rv; + + if (mmap->mm == (caddr_t) -1) + return APR_ENOENT; + + if ((rv = mmap_cleanup(mmap)) == APR_SUCCESS) { + ap_kill_cleanup(mmap->cntxt, mmap, mmap_cleanup); + return APR_SUCCESS; + } + return rv; +} diff --git a/mmap/unix/mmap_h.h b/mmap/unix/mmap_h.h new file mode 100644 index 000000000..0886573e0 --- /dev/null +++ b/mmap/unix/mmap_h.h @@ -0,0 +1,75 @@ +/* ==================================================================== + * Copyright (c) 1999 The Apache Group. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * 4. The names "Apache Server" and "Apache Group" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Group. + * For more information on the Apache Group and the Apache HTTP server + * project, please see <http://www.apache.org/>. + * + */ + +#ifndef MMAP_H_H +#define MMAP_H_H + +#include <sys/stat.h> +#include "apr_general.h" +#include "apr_mmap.h" +#include "apr_errno.h" + +struct mmap_t { + ap_context_t *cntxt; + char *filename; /* the full path to the file */ + struct stat sinfo; + void *mm; + size_t size; +}; + +ap_status_t mmap_cleanup(void *); + +#endif /* ! FILE_IO_H */ + |