2 // Copyright 2005 The Android Open Source Project
4 // Inter-process shared memory.
10 #error DO NOT USE THIS FILE IN THE DEVICE BUILD
13 #include "Semaphore.h"
18 * Platform-independent shared memory. Each object can be used to
19 * create a chunk of memory that is shared between processes.
21 * For convenience, a semaphore is associated with each segment.
22 * (Whether this should have been done in a subclass is debatable.)
24 * The "key" is usually the process ID of the process that created the
25 * segment. The goal is to avoid clashing with other processes that are
26 * trying to do the same thing. It's a little awkward to use when you
27 * want to have multiple shared segments created in one process. In
28 * SysV you can work around this by using a "private" key and sharing
29 * the shmid with your friends, in Win32 you can use a string, but we're
30 * in lowest-common-denominator mode here. Assuming we have 16-bit PIDs,
31 * the upper 16 bits can be used to serialize keys.
33 * When the object goes out of scope, the shared memory segment is
34 * detached from the process. If the object was responsible for creating
35 * the segment, it is also marked for destruction on SysV systems. This
36 * will make it impossible for others to attach to.
38 * On some systems, the length returned by getLength() may be different
39 * for parent and child due to page size rounding.
47 * Create a new shared memory segment, with the specified size. If
48 * "deleteExisting" is set, any existing segment will be deleted first
49 * (useful for SysV IPC).
51 * Returns "true" on success, "false" on failure.
53 bool create(int key, long size, bool deleteExisting);
56 * Attach to a shared memory segment. Use this from the process that
57 * didn't create the segment.
59 * Returns "true" on success, "false" on failure.
64 * Get the memory segment address and length. These will not change
65 * for the lifetime of the object, so it's okay to cache the results.
67 * On failure, getAddr() returns NULL and getLength() returns -1.
73 * Lock or unlock the shared memory segment. This is useful if you
74 * are updating pieces of shared data. The segment is initially
77 * This does *not* lock down the segment in the virtual paging system.
85 Semaphore mSem; // uses the same value for "key"
86 unsigned long mHandle; // shmid(int) or HANDLE
87 void* mAddr; // address
88 long mLength; // length of segment (cached)
89 bool mCreator; // true if we created the segment
90 int mKey; // key passed in as arg
93 }; // namespace android
95 #endif // __LIBS_SHMEM_H