summaryrefslogtreecommitdiff
path: root/src/libnml/os_intf/shm.cc
blob: 9da4ce3d9b81cecbbf4510c9a2956573ee2031a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/********************************************************************
* Description: shm.cc
*
*   Derived from a work by Fred Proctor & Will Shackleford
*
* Author:
* License: LGPL Version 2
* System: Linux
*    
* Copyright (c) 2004 All rights reserved.
*
* Last change: 
********************************************************************/

#include "shm.hh"
#include "_shm.h"		/* rcs_shm_open(), rcs_shm_close() */
#include "rcs_print.hh"		// rcs_print_error()

extern "C" {
#include <stdio.h>		/* fprintf(), stderr */
#include <sys/types.h>		/* key_t */
#include <stddef.h>		/* size_t */
#include <errno.h>		// errno
#include <sys/ipc.h>		// IPC_CREAT
}

RCS_SHAREDMEM::RCS_SHAREDMEM(key_t key, size_t size, int oflag, int mode)
{
    shm = NULL;
    addr = NULL;
    delete_totally = 0;
    create_errno = 0;
    created = 0;

    if (oflag & RCS_SHAREDMEM_CREATE) {
	/* create shared memory */
#ifdef USE_POSIX_SHAREDMEM
	shm = rcs_shm_open(key, size, O_CREAT, mode);
#else
	shm = rcs_shm_open(key, size, IPC_CREAT, mode);
#endif
	if (shm == NULL) {
	    create_errno = errno;
	    rcs_print_error("can't create shared memory\n");
	    return;
	}
    } else {
	/* attach to existing shared memory */
	shm = rcs_shm_open(key, size, 0);
	if (shm == NULL) {
	    create_errno = errno;
	    rcs_print_error
		("can't attach to shared memory-- is master started?\n");
	    return;
	}
    }
    create_errno = shm->create_errno;
    created = shm->created;
    /* duplicate the pointer, so users don't have to dig into shm->addr */
    addr = shm->addr;
}

RCS_SHAREDMEM::~RCS_SHAREDMEM()
{
    if (shm == NULL) {
	return;
    } else {
	if (delete_totally) {
	    rcs_shm_delete(shm);
	} else {
	    rcs_shm_close(shm);
	}
	shm = NULL;
    }
}

int
  RCS_SHAREDMEM::nattch()
{
    if (shm == NULL) {
	return -1;
    } else {
	return rcs_shm_nattch(shm);
    }
}

// This constructor declared private to prevent copying
RCS_SHAREDMEM::RCS_SHAREDMEM(RCS_SHAREDMEM & shm)
{
}