summaryrefslogtreecommitdiffstats
path: root/fitsy++/sshare.C
blob: f2b94d93ff79cf0b18a35b176fa81046ae079d6e (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include "sshare.h"
#include "util.h"

#if (HAVE_SYS_SHM_H && !__WIN32)
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>

FitsSShareID::FitsSShareID(int hdrid, int shmid, const char* filter)
{
  // reset
  valid_ =0;

  {
    // find size
    struct shmid_ds info;
    if (shmctl(hdrid, IPC_STAT, &info)) {
      internalError("Fitsy++ sshare shctl failed");
      return;
    }
    hmapsize_ = info.shm_segsz;

    // Attach the memory segment
    if ((long)(hmapdata_ = (char*)shmat(hdrid, 0, SHM_RDONLY)) == -1) {
      internalError("Fitsy++ sshare shmat failed");
      return;
    }
  }

  {
    parse(filter);
    if (!valid_)
      return;

    // reset
    valid_ =0;

    // find size
    struct shmid_ds info;
    if (shmctl(shmid, IPC_STAT, &info)) {
      internalError("Fitsy++ sshare shctl failed");
      return;
    }
    mapsize_ = info.shm_segsz;

    // Attach the memory segment
    if ((long)(mapdata_ = (char*)shmat(shmid, 0, SHM_RDONLY)) == -1) {
      internalError("Fitsy++ sshare shmat failed");
      return;
    }
  }

  // so far so good
  valid_ = 1;
}

FitsSShareID::~FitsSShareID()
{
  shmdt(mapdata_);
}

FitsSShareKey::FitsSShareKey(int hdr, int key, const char* filter)
{
  // reset
  valid_ =0;

  {
    // get shmid
    int shmid;
    if ((shmid = shmget(hdr, 0, 0)) < 0) {
      internalError("Fitsy++ sshare shmget failed");
      return;
    }

    // find size
    struct shmid_ds info;
    if (shmctl(shmid, IPC_STAT, &info)) {
      internalError("Fitsy++ sshare shctl failed");
      return;
    }
    mapsize_ = info.shm_segsz;

    // Attach the memory segment
    if ((long)(mapdata_ = (char*)shmat(shmid, 0, SHM_RDONLY)) == -1) {
      internalError("Fitsy++ sshare shmat failed");
      return;
    }
  }

  {
    parse(filter);
    if (!valid_)
      return;

    // reset
    valid_ =0;

    // get shmid
    int shmid;
    if ((shmid = shmget(key, 0, 0)) < 0) {
      internalError("Fitsy++ sshare shmget failed");
      return;
    }

    // find size
    struct shmid_ds info;
    if (shmctl(shmid, IPC_STAT, &info)) {
      internalError("Fitsy++ sshare shmctl failed");
      return;
    }
    mapsize_ = info.shm_segsz;

    // Attach the memory segment
    if ((long)(mapdata_ = (char*)shmat(shmid, 0, SHM_RDONLY)) == -1) {
      internalError("Fitsy++ sshare shmat failed");
      return;
    }
  }

  // so far so good
  valid_ = 1;
}

FitsSShareKey::~FitsSShareKey()
{
  shmdt(mapdata_);
}

#else

FitsSShareID::FitsSShareID(int hdrid, int shmid, const char* filter)
{
  valid_ = 0;
}

FitsSShareID::~FitsSShareID() {}

FitsSShareKey::FitsSShareKey(int hdr, int key, const char* filter)
{
  valid_ = 0;
}

FitsSShareKey::~FitsSShareKey() {}

#endif