Skip to content
Snippets Groups Projects
Select Git revision
  • d2cd2d85e7c994fcb88a2aee79c3429b0dbb62c6
  • master default protected
  • L2SS-1914-fix_job_dispatch
  • TMSS-3170
  • TMSS-3167
  • TMSS-3161
  • TMSS-3158-Front-End-Only-Allow-Changing-Again
  • TMSS-3133
  • TMSS-3319-Fix-Templates
  • test-fix-deploy
  • TMSS-3134
  • TMSS-2872
  • defer-state
  • add-custom-monitoring-points
  • TMSS-3101-Front-End-Only
  • TMSS-984-choices
  • SDC-1400-Front-End-Only
  • TMSS-3079-PII
  • TMSS-2936
  • check-for-max-244-subbands
  • TMSS-2927---Front-End-Only-PXII
  • Before-Remove-TMSS
  • LOFAR-Release-4_4_318 protected
  • LOFAR-Release-4_4_317 protected
  • LOFAR-Release-4_4_316 protected
  • LOFAR-Release-4_4_315 protected
  • LOFAR-Release-4_4_314 protected
  • LOFAR-Release-4_4_313 protected
  • LOFAR-Release-4_4_312 protected
  • LOFAR-Release-4_4_311 protected
  • LOFAR-Release-4_4_310 protected
  • LOFAR-Release-4_4_309 protected
  • LOFAR-Release-4_4_308 protected
  • LOFAR-Release-4_4_307 protected
  • LOFAR-Release-4_4_306 protected
  • LOFAR-Release-4_4_304 protected
  • LOFAR-Release-4_4_303 protected
  • LOFAR-Release-4_4_302 protected
  • LOFAR-Release-4_4_301 protected
  • LOFAR-Release-4_4_300 protected
  • LOFAR-Release-4_4_299 protected
41 results

SharedMemory.h

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    SharedMemory.h 1.66 KiB
    #ifndef SHARED_MEMORY
    #define SHARED_MEMORY
    
    #include <Common/Exception.h>
    #include <Interface/Allocator.h>
    
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    namespace LOFAR {
    namespace RTCP {
    
    /*
     * A memory region manager for shared memory, to be used by
     * allocators defined in Interface/Allocator.h
     */
    class SharedMemoryArena: public FixedArena {
    public:
      EXCEPTION_CLASS(TimeOutException, LOFAR::Exception);
    
      enum Mode {
        CREATE,
        CREATE_EXCL,
        READ,
        READWRITE
      };
    
      /* Create a shared memory region, or attach to an existing one. The timeout
       * specifies how long the constructor will wait for the memory region to
       * appear if mode == READ or mode == READWRITE.
       */
      SharedMemoryArena( key_t key, size_t size, Mode mode = CREATE, time_t timeout = 60 );
      ~SharedMemoryArena();
    
      template <typename T> T* ptr( size_t offset = 0 ) const {
        return reinterpret_cast<T*>(reinterpret_cast<char*>(itsBegin) + offset);
      }
    
    private:
      const key_t key;
      const Mode mode;
      int shmid;
    };
    
    /*
     * Provides an interface for any struct stored as a shared memory region.
     */
    template<typename T> class SharedStruct {
    public:
      SharedStruct( key_t key, bool create = false, time_t timeout = 60 );
    
      T &get() {
        return *data.ptr<T>();
      }
    
      T &get() const {
        return *data.ptr<T>();
      }
    
    private:
      SharedMemoryArena data;
    
      SharedStruct( const SharedStruct & );
      SharedStruct &operator=( const SharedStruct & );
    };
    
    
    template<typename T> SharedStruct<T>::SharedStruct( key_t key, bool create, time_t timeout )
    :
      data(key, sizeof(T), create ? SharedMemoryArena::CREATE : SharedMemoryArena::READWRITE, timeout)
    {
    }
    
    }
    }
    
    #endif