[SciPy-user] shared memory machines

Sturla Molden sturla@molden...
Wed Feb 11 07:58:24 CST 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

     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):

     def dealloc(Handle self):
         cdef shmid_ds buf
         cdef int _shmid= <int> self.shmid
         cdef void *addr
         cdef int ierr
             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
            # 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!"
                printf("shared segment removed\n")


More information about the SciPy-user mailing list