Home | History | Annotate | Download | only in app
      1 //
      2 // Copyright 2005 The Android Open Source Project
      3 //
      4 // Inter-process shared memory.
      5 //
      6 #ifndef __LIBS_SHMEM_H
      7 #define __LIBS_SHMEM_H
      8 
      9 #ifdef HAVE_ANDROID_OS
     10 #error DO NOT USE THIS FILE IN THE DEVICE BUILD
     11 #endif
     12 
     13 #include "Semaphore.h"
     14 
     15 namespace android {
     16 
     17 /*
     18  * Platform-independent shared memory.  Each object can be used to
     19  * create a chunk of memory that is shared between processes.
     20  *
     21  * For convenience, a semaphore is associated with each segment.
     22  * (Whether this should have been done in a subclass is debatable.)
     23  *
     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.
     32  *
     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.
     37  *
     38  * On some systems, the length returned by getLength() may be different
     39  * for parent and child due to page size rounding.
     40  */
     41 class Shmem {
     42 public:
     43     Shmem(void);
     44     virtual ~Shmem(void);
     45 
     46     /*
     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).
     50      *
     51      * Returns "true" on success, "false" on failure.
     52      */
     53     bool create(int key, long size, bool deleteExisting);
     54 
     55     /*
     56      * Attach to a shared memory segment.  Use this from the process that
     57      * didn't create the segment.
     58      *
     59      * Returns "true" on success, "false" on failure.
     60      */
     61     bool attach(int key);
     62 
     63     /*
     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.
     66      *
     67      * On failure, getAddr() returns NULL and getLength() returns -1.
     68      */
     69     void* getAddr(void);
     70     long getLength(void);
     71 
     72     /*
     73      * Lock or unlock the shared memory segment.  This is useful if you
     74      * are updating pieces of shared data.  The segment is initially
     75      * "unlocked".
     76      *
     77      * This does *not* lock down the segment in the virtual paging system.
     78      * It's just a mutex.
     79      */
     80     void lock(void);
     81     void unlock(void);
     82     bool tryLock(void);
     83 
     84 private:
     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
     91 };
     92 
     93 }; // namespace android
     94 
     95 #endif // __LIBS_SHMEM_H
     96