* Re: shared memory
1997-10-18 4:00 shared memory Dean Hougen
@ 1997-10-18 4:00 ` Dirk Grunwald
0 siblings, 0 replies; 4+ messages in thread
From: Dirk Grunwald @ 1997-10-18 4:00 UTC (permalink / raw)
Here's a C++ class that may be helpful. It creates a shared memory
with (one page) of overhead that records where in the address space
the page should be mapped. This is intended to simplify applications
that need to have the same shared memory segment at the same address
repeatedly. It allows you to "name" the segments. Not fool-proof, but
it works. My intent was to eventually allow me to implement a malloc
in the overhead space, but haven't gotten to that yet.
main(int argc, char **argv)
{
HPCXXshmem mem("foo");
if (argc > 1) {
cout << "Initializing...\n";
int c = mem.create(1024 * 10);
if ( c < 0 ) {
cerr << "mem.create == " << c << ", errno = " << mem.errno() << endl;
exit(0);
}
}
int r = mem.attach();
if ( r < 0 ) {
cerr << "mem.create == " << r << ", errno = " << mem.errno() << endl;
exit(0);
}
cout << "address is 0x" << hex << (unsigned long) mem.data() << dec << ", which should be a pointer" << endl;
}
#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.2).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `!/bin/sh' line above, then type `sh FILE'.
#
# Made on 1997-10-18 11:24 MDT by <grunwald@foobar.cs.colorado.edu>.
# Source directory was `/tmp_mnt/home/foobar/grunwald/Research/HPCC-Threads/Shmem'.
#
# Existing files will *not* be overwritten unless `-c' is specified.
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 958 -rw-rw-r-- HPCXXshmem.h
# 4503 -rw-rw-r-- HPCXXshmem.cc
# 628 -rw-rw-r-- testshmem.cc
#
save_IFS="${IFS}"
IFS="${IFS}:"
gettext_dir=FAILED
locale_dir=FAILED
first_param="$1"
for dir in $PATH
do
if test "$gettext_dir" = FAILED && test -f $dir/gettext \
&& ($dir/gettext --version >/dev/null 2>&1)
then
set `$dir/gettext --version 2>&1`
if test "$3" = GNU
then
gettext_dir=$dir
fi
fi
if test "$locale_dir" = FAILED && test -f $dir/shar \
&& ($dir/shar --print-text-domain-dir >/dev/null 2>&1)
then
locale_dir=`$dir/shar --print-text-domain-dir`
fi
done
IFS="$save_IFS"
if test "$locale_dir" = FAILED || test "$gettext_dir" = FAILED
then
echo=echo
else
TEXTDOMAINDIR=$locale_dir
export TEXTDOMAINDIR
TEXTDOMAIN=sharutils
export TEXTDOMAIN
echo="$gettext_dir/gettext -s"
fi
touch -am 1231235999 $$.touch >/dev/null 2>&1
if test ! -f 1231235999 && test -f $$.touch; then
shar_touch=touch
else
shar_touch=:
echo
$echo 'WARNING: not restoring timestamps. Consider getting and'
$echo "installing GNU \`touch', distributed in GNU File Utilities..."
echo
fi
rm -f 1231235999 $$.touch
#
if mkdir _sh21658; then
$echo 'x -' 'creating lock directory'
else
$echo 'failed to create lock directory'
exit 1
fi
# ============= HPCXXshmem.h ==============
if test -f 'HPCXXshmem.h' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'HPCXXshmem.h' '(file already exists)'
else
$echo 'x -' extracting 'HPCXXshmem.h' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'HPCXXshmem.h' &&
//-*-c++-*-
#ifndef _HPCXXshmem_h_
#define _HPCXXshmem_h_
X
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
X
//
// HPC++ interface to shared memory
//
class HPCXXshmem
{
X char *shm_name;
X key_t shm_key;
X int shm_segment_id;
X size_t shm_size;
X caddr_t shm_base;
X
X int shmid;
X int last_errno;
X
X size_t shm_user_size;
X caddr_t shm_user_data;
X
X struct internal_header_struct {
X caddr_t allocated_at;
X caddr_t desired_address;
X };
X
X internal_header_struct* internal_header;
X
X int internal_set_mode(int, int);
X
public:
X
X HPCXXshmem(char *name);
X HPCXXshmem(key_t shm_key);
X HPCXXshmem();
X
X ~HPCXXshmem();
X
X int create(const size_t size,
X const int mode = 0700,
X const caddr_t desired_address = NULL);
X
X int set_mode(int);
X
X int attach();
X int remove();
X
X caddr_t data() {
X return shm_user_data;
X }
X
X size_t size() {
X return shm_user_size;
X }
X
X int errno() {
X return last_errno;
X }
X
};
X
#endif
SHAR_EOF
$shar_touch -am 0908174497 'HPCXXshmem.h' &&
chmod 0664 'HPCXXshmem.h' ||
$echo 'restore of' 'HPCXXshmem.h' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'HPCXXshmem.h:' 'MD5 check failed'
d3ea616b5e60a9807a58a5540dbfb9da HPCXXshmem.h
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'HPCXXshmem.h'`"
test 958 -eq "$shar_count" ||
$echo 'HPCXXshmem.h:' 'original size' '958,' 'current size' "$shar_count!"
fi
fi
# ============= HPCXXshmem.cc ==============
if test -f 'HPCXXshmem.cc' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'HPCXXshmem.cc' '(file already exists)'
else
$echo 'x -' extracting 'HPCXXshmem.cc' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'HPCXXshmem.cc' &&
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "HPCXXshmem.h"
#include <unistd.h>
#include <errno.h>
X
HPCXXshmem::HPCXXshmem(char *name)
{
X shm_name = name;
X //
X // Compute a key id using a hash function based on the name
X //
X shm_key = 0;
X int l = strlen(shm_name);
X for (int i = 0; i < l; i++) {
X shm_key *= 71;
X shm_key += int(shm_name[i]);
X }
}
X
HPCXXshmem::HPCXXshmem(key_t key)
{
X char buff[128];
X sprintf(buff, "<key: %ld>", (long) key);
X int l = strlen(buff);
X assert (l < 128);
X
X shm_name = new char[l];
X strcpy(shm_name, buff);
X
X shm_key = key;
}
X
HPCXXshmem::HPCXXshmem()
{
X shm_name = "<private>";
X shm_key = IPC_PRIVATE;
}
X
HPCXXshmem::~HPCXXshmem()
{
X if ( shm_segment_id != 0 ) {
X shmdt(shm_base);
X }
}
X
int
HPCXXshmem::remove()
{
X struct shmid_ds buf;
X int result = shmctl(shm_segment_id, IPC_RMID, &buf);
X if ( result < 0 ) {
X last_errno = ::errno;
X return -1;
X }
X
X return 0;
}
X
X
int
HPCXXshmem::internal_set_mode(int s, int mode)
{
X struct shmid_ds buf;
X int result = shmctl(s, IPC_STAT, &buf);
X if ( result < 0 ) {
X last_errno = ::errno;
X return -1;
X }
X
X buf.shm_perm.mode = mode;
X
X result = shmctl(s, IPC_SET, &buf);
X if ( result < 0 ) {
X last_errno = ::errno;
X return -1;
X }
X return 0;
}
X
int
HPCXXshmem::set_mode(int mode)
{
X return internal_set_mode(shm_segment_id, mode);
}
X
int
HPCXXshmem::create(const size_t size,
X const int mode,
X const caddr_t desired_address)
{
X shm_user_size = size;
X long pagesize = sysconf(_SC_PAGESIZE);
X shm_size = shm_user_size + pagesize;
X
X //
X // Create with all priv's for user (they need to diddle this if
X // they want to change it..
X //
X shm_segment_id = shmget(shm_key, shm_size, IPC_CREAT | IPC_EXCL | 0700);
X if ( shm_segment_id < 0 ) {
X last_errno = ::errno;
X return -1;
X }
X
X //
X // Set the mode..
X //
X int r = internal_set_mode(shm_segment_id, mode);
X
X if ( r < 0 ) {
X last_errno = ::errno;
X remove();
X return -1;
X }
X
X //
X // If we're the primary creator of this segment, initialize
X // the internal data structures..
X //
X
X
X caddr_t allocated_at;
X caddr_t user_at;
X
X if ( desired_address == NULL ) {
X allocated_at = (caddr_t) shmat(shm_segment_id, NULL, SHM_RND);
X user_at = allocated_at + pagesize;
X
X if ( allocated_at == (caddr_t) -1 ) {
X last_errno = ::errno;
X return -1;
X }
X } else {
X allocated_at = (caddr_t) shmat(shm_segment_id, desired_address - pagesize, SHM_RND);
X user_at = allocated_at + pagesize;
X if ( allocated_at == (caddr_t) -1 ) {
X last_errno = ::errno;
X return -1;
X }
X
X //
X // Check is user constraint was matched..
X //
X if ( allocated_at != desired_address ) {
X shmdt(allocated_at);
X shm_segment_id = 0;
X
X last_errno = EINVAL;
X return -1;
X }
X }
X
X //
X // Fill in the internal header information..
X //
X internal_header = (internal_header_struct *) allocated_at;
X internal_header -> allocated_at = allocated_at;
X internal_header -> desired_address = user_at;
X
X
X //
X // Detach the memory segment (enforing attach semantics)
X //
X shmdt(allocated_at);
X shm_segment_id = 0;
X return 0;
}
X
int
HPCXXshmem::attach()
{
X //
X // Get the shm_segment_id..
X //
X shm_segment_id = shmget(shm_key, 0, 0700);
X if ( shm_segment_id == -1 ) {
X last_errno = ::errno;
X return -1;
X }
X
X //
X // Attach it anywhere...
X //
X caddr_t result = (caddr_t) shmat(shm_segment_id, NULL, 0);
X if ( result == (caddr_t) -1) {
X last_errno = ::errno;
X return -1;
X }
X //
X // Determine actual desired location...
X //
X
X internal_header = (internal_header_struct *) result;
X caddr_t allocated_at = internal_header -> allocated_at;
X caddr_t desired_address = internal_header -> desired_address;
X
X //
X // Sanity check..
X //
X long pagesize = sysconf(_SC_PAGESIZE);
X if ( allocated_at + pagesize != desired_address ) {
X shmdt(allocated_at);
X shm_segment_id = 0;
X last_errno = EINVAL;
X return -1;
X }
X //
X // Detach it, attach again at proper address...
X //
X shmdt(allocated_at);
X
X result = (caddr_t) shmat(shm_segment_id, allocated_at, 0400);
X if ( result == (caddr_t) -1) {
X last_errno = ::errno;
X return -1;
X }
X
X //
X // Ok, we're clean -- set up variables and return to user..
X //
X
X shm_user_data = desired_address;
X internal_header = (internal_header_struct *)allocated_at;
X shm_base = allocated_at;
X
X return 0;
}
SHAR_EOF
$shar_touch -am 0908182897 'HPCXXshmem.cc' &&
chmod 0664 'HPCXXshmem.cc' ||
$echo 'restore of' 'HPCXXshmem.cc' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'HPCXXshmem.cc:' 'MD5 check failed'
a9e51d92b9505dbf64905030b5749a3f HPCXXshmem.cc
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'HPCXXshmem.cc'`"
test 4503 -eq "$shar_count" ||
$echo 'HPCXXshmem.cc:' 'original size' '4503,' 'current size' "$shar_count!"
fi
fi
# ============= testshmem.cc ==============
if test -f 'testshmem.cc' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'testshmem.cc' '(file already exists)'
else
$echo 'x -' extracting 'testshmem.cc' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'testshmem.cc' &&
#include <stream.h>
#include <stdlib.h>
#include "HPCXXshmem.h"
X
main(int argc, char **argv)
{
X HPCXXshmem mem("foo");
X
X if (argc > 1) {
X cout << "Initializing...\n";
X int c = mem.create(1024 * 10);
X if ( c < 0 ) {
X cerr << "mem.create == " << c << ", errno = " << mem.errno() << endl;
X exit(0);
X }
X }
X
X int r = mem.attach();
X if ( r < 0 ) {
X cerr << "mem.create == " << r << ", errno = " << mem.errno() << endl;
X exit(0);
X }
X
X cout << "address is 0x" << hex << (unsigned long) mem.data() << dec << ", which should be a pointer" << endl;
}
X
void
foo(int i)
{
X cerr << "I is " << i << endl;
}
SHAR_EOF
$shar_touch -am 0909164297 'testshmem.cc' &&
chmod 0664 'testshmem.cc' ||
$echo 'restore of' 'testshmem.cc' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'testshmem.cc:' 'MD5 check failed'
9efc80f2916902fe922d382178a996f5 testshmem.cc
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'testshmem.cc'`"
test 628 -eq "$shar_count" ||
$echo 'testshmem.cc:' 'original size' '628,' 'current size' "$shar_count!"
fi
fi
rm -fr _sh21658
exit 0
--
Dirk Grunwald Assoc. Prof, Univ. of Colorado at Boulder
^ permalink raw reply [flat|nested] 4+ messages in thread