25 const char SubprocessWindowBuffer::
26 _magic_number[SubprocessWindowBuffer::magic_number_length] =
"pNdaSWB";
31 void *SubprocessWindowBuffer::
32 operator
new(size_t,
void *addr) {
41 SubprocessWindowBuffer::
42 SubprocessWindowBuffer(
int x_size,
int y_size) {
43 memcpy(_this_magic, _magic_number, magic_number_length);
46 _row_size = _x_size * 4;
47 _framebuffer_size = _row_size * y_size;
53 _mmap_size =
sizeof(*this) + _framebuffer_size;
59 SubprocessWindowBuffer::
61 _mmap_size(copy._mmap_size),
62 _x_size(copy._x_size),
63 _y_size(copy._y_size),
64 _row_size(copy._row_size),
65 _framebuffer_size(copy._framebuffer_size)
67 memcpy(_this_magic, _magic_number, magic_number_length);
77 SubprocessWindowBuffer::
78 ~SubprocessWindowBuffer() {
96 int x_size,
int y_size) {
100 filename = tmpnam(
nullptr);
102 fd = open(filename.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
104 perror(filename.c_str());
110 mmap_size = temp._mmap_size;
113 size_t zero_size = 1024;
114 char zero[zero_size];
115 memset(zero, 0, zero_size);
116 for (
size_t bi = 0; bi < mmap_size; bi += zero_size) {
117 write(fd, zero, zero_size);
120 void *shared_mem = mmap(
nullptr, mmap_size, PROT_READ | PROT_WRITE,
122 if (shared_mem == (
void *)-1) {
142 buffer->~SubprocessWindowBuffer();
146 unlink(filename.c_str());
163 fd = open(filename.c_str(), O_RDWR);
165 perror(filename.c_str());
170 off_t file_size = lseek(fd, 0, SEEK_END);
172 cerr << filename <<
" not large enough.\n";
180 void *shared_mem = mmap(
nullptr, initial_size, PROT_READ,
182 if (shared_mem == (
void *)-1) {
184 cerr <<
"Couldn't map.\n";
192 cerr <<
"Not a subprocess window buffer: " << filename <<
"\n";
193 munmap(shared_mem, initial_size);
200 mmap_size = temp->_mmap_size;
203 munmap(shared_mem, initial_size);
205 if (file_size < mmap_size) {
206 cerr << filename <<
" not large enough.\n";
212 shared_mem = mmap(
nullptr, mmap_size, PROT_READ | PROT_WRITE,
214 if (shared_mem == (
void *)-1) {
216 cerr <<
"Couldn't map 2.\n";
227 assert(buffer->_mmap_size == mmap_size);
239 munmap((
void *)buffer, mmap_size);
248 return (memcmp(_this_magic, _magic_number, magic_number_length) == 0);
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool verify_magic_number() const
Returns true if the buffer's magic number matches, false otherwise.
static void close_buffer(int fd, size_t mmap_size, const std::string &filename, SubprocessWindowBuffer *buffer)
Closes a buffer object created via a previous call to open_buffer().
static SubprocessWindowBuffer * new_buffer(int &fd, size_t &mmap_size, std::string &filename, int x_size, int y_size)
Call this method to create a new buffer in shared memory space.
This is a special class that is designed to faciliate SubprocessWindow.
static void destroy_buffer(int fd, size_t mmap_size, const std::string &filename, SubprocessWindowBuffer *buffer)
Destroys a buffer object created via a previous call to new_buffer().
static SubprocessWindowBuffer * open_buffer(int &fd, size_t &mmap_size, const std::string &filename)
Call this method to open a reference to an existing buffer in shared memory space.