Home | History | Annotate | Download | only in Platform
      1 /*
      2  * Copyright (C) 2010 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef SharedMemory_h
     27 #define SharedMemory_h
     28 
     29 #include <wtf/Noncopyable.h>
     30 #include <wtf/PassRefPtr.h>
     31 #include <wtf/RefCounted.h>
     32 
     33 #if PLATFORM(QT) || PLATFORM(GTK)
     34 #include "Attachment.h"
     35 #include <wtf/text/WTFString.h>
     36 #endif
     37 
     38 namespace CoreIPC {
     39     class ArgumentDecoder;
     40     class ArgumentEncoder;
     41 }
     42 
     43 namespace WebKit {
     44 
     45 class SharedMemory : public RefCounted<SharedMemory> {
     46 public:
     47     enum Protection {
     48         ReadOnly,
     49         ReadWrite
     50     };
     51 
     52     class Handle {
     53         WTF_MAKE_NONCOPYABLE(Handle);
     54     public:
     55         Handle();
     56         ~Handle();
     57 
     58         bool isNull() const;
     59 
     60         void encode(CoreIPC::ArgumentEncoder*) const;
     61         static bool decode(CoreIPC::ArgumentDecoder*, Handle&);
     62 
     63 #if USE(UNIX_DOMAIN_SOCKETS)
     64         CoreIPC::Attachment releaseToAttachment() const;
     65         void adoptFromAttachment(int fileDescriptor, size_t);
     66 #endif
     67     private:
     68         friend class SharedMemory;
     69 #if PLATFORM(MAC)
     70         mutable mach_port_t m_port;
     71 #elif PLATFORM(WIN)
     72         mutable HANDLE m_handle;
     73 #elif USE(UNIX_DOMAIN_SOCKETS)
     74         mutable int m_fileDescriptor;
     75 #endif
     76         size_t m_size;
     77     };
     78 
     79     // Create a shared memory object with the given size. Will return 0 on failure.
     80     static PassRefPtr<SharedMemory> create(size_t);
     81 
     82     // Create a shared memory object from the given handle and the requested protection. Will return 0 on failure.
     83     static PassRefPtr<SharedMemory> create(const Handle&, Protection);
     84 
     85 #if PLATFORM(WIN)
     86     static PassRefPtr<SharedMemory> adopt(HANDLE, size_t, Protection);
     87 #endif
     88 
     89     ~SharedMemory();
     90 
     91     bool createHandle(Handle&, Protection);
     92 
     93     size_t size() const { return m_size; }
     94     void* data() const { return m_data; }
     95 
     96     // Return the system page size in bytes.
     97     static unsigned systemPageSize();
     98 
     99 private:
    100     size_t m_size;
    101     void* m_data;
    102 #if PLATFORM(MAC)
    103     mach_port_t m_port;
    104 #elif PLATFORM(WIN)
    105     HANDLE m_handle;
    106 #elif USE(UNIX_DOMAIN_SOCKETS)
    107     int m_fileDescriptor;
    108 #endif
    109 };
    110 
    111 };
    112 
    113 #endif // SharedMemory_h
    114