[SciPy-user] shared memory machines

Sturla Molden sturla at molden.no
Wed Feb 11 08:58:24 EST 2009


On 2/11/2009 2:51 PM, Sturla Molden wrote:

> As long as the Heap object is destroyed, 

eh, Handle object.

cdef extern from "stdio.h":
    void printf(char *str)

cdef class Handle:

     """ Automatic shared segment deattachment
         - without this object we would need to do reference
         counting manually, as shmdt is global to the process.
         Do not instantiate this class, except from within
         SharedMemoryBuffer.__init__.
     """

     cdef int shmid
     cdef object name
     cdef object cleanup
     cdef object __weakref__

     def __init__(Handle self, shmid, name):
         self.shmid = <int> shmid
         self.name = name

     def gethandle(Handle self):
         return int(self.shmid)

     def __dealloc__(Handle self):
         self.dealloc()

     def dealloc(Handle self):
         cdef shmid_ds buf
         cdef int _shmid= <int> self.shmid
         cdef void *addr
         cdef int ierr
         try:
             ma, size = __mapped_addresses[ self.name ]
             addr = <void *>(<unsigned long> ma)
             ierr = shmdt(addr)
             if (ierr < 0): raise MemoryError, "shmdt failed."
             del __mapped_addresses[ self.name ]
             print "Deallocated memory at %s" % ma #DBG

         except KeyError:
            print __mapped_addresses #DBG
            print self.name          #DBG
            print 'KeyError'         #DBG
            #pass
            # this may happen and is not a problem

        if (shmctl(_shmid, IPC_STAT, &buf) == -1):
            raise OSError, \
  "IPC_STAT failed, you could have a global memory leak!"

        if (buf.shm_nattch == 0):
            if( shmctl(_shmid, IPC_RMID, NULL) == -1 ):
                raise OSError, \
"IPC_RMID failed, you have a global memory leak!"
            else:
                printf("shared segment removed\n")



S.M.



More information about the SciPy-User mailing list