#include <unistd.h>
Ipc::Mem::Segment::Segment(const char *const id):
- theName(GenerateName(id)), theFD(-1), theSize(-1), theMem(NULL)
+ theName(GenerateName(id)), theFD(-1), theMem(NULL),
+ theSize(0), theReserved(0)
{
}
}
theSize = aSize;
+ theReserved = 0;
attach();
}
Ipc::Mem::Segment::attach()
{
assert(theFD >= 0);
- assert(theSize >= 0);
assert(!theMem);
void *const p =
theMem = 0;
}
+void *
+Ipc::Mem::Segment::reserve(size_t chunkSize)
+{
+ assert(chunkSize <= theSize);
+ assert(theReserved <= theSize - chunkSize);
+ theReserved += chunkSize;
+ return reinterpret_cast<char*>(mem()) + theReserved;
+}
+
/// Generate name for shared memory segment. Replaces all slashes with dots.
String
Ipc::Mem::Segment::GenerateName(const char *id)
const String &name() { return theName; } ///< shared memory segment name
int size() { return theSize; } ///< shared memory segment size
void *mem() { return theMem; } ///< pointer to mmapped shared memory segment
+ void *reserve(size_t chunkSize); ///< reserve and return the next chunk
+ // TODO: convert most mem() calls to reserve()
private:
void attach();
const String theName; ///< shared memory segment file name
int theFD; ///< shared memory segment file descriptor
- int theSize; ///< shared memory segment size
void *theMem; ///< pointer to mmapped shared memory segment
+ size_t theSize; ///< shared memory segment size
+ size_t theReserved; ///< the total number of reserve()d bytes
};
} // namespace Mem