def create_semaphore(space, name, val, max): sem = sem_open(name, os.O_CREAT | os.O_EXCL, 0600, val) try: sem_unlink(name) except OSError: pass else: rgc.add_memory_pressure(SEM_T_SIZE) return sem
def __init__(self, space, name): self.name = name self.digest_size = self.compute_digest_size() # Allocate a lock for each HASH object. # An optimization would be to not release the GIL on small requests, # and use a custom lock only when needed. self.lock = Lock(space) ctx = lltype.malloc(ropenssl.EVP_MD_CTX.TO, flavor='raw') rgc.add_memory_pressure(HASH_MALLOC_SIZE + self.digest_size) self.ctx = ctx
def allocate_ll_lock(): # track_allocation=False here; be careful to lltype.free() it. The # reason it is set to False is that we get it from all app-level # lock objects, as well as from the GIL, which exists at shutdown. ll_lock = lltype.malloc(TLOCKP.TO, flavor='raw', track_allocation=False) res = c_thread_lock_init(ll_lock) if rffi.cast(lltype.Signed, res) <= 0: lltype.free(ll_lock, flavor='raw', track_allocation=False) raise error("out of resources") # Add some memory pressure for the size of the lock because it is an # Opaque object rgc.add_memory_pressure(TLOCKP_SIZE) return ll_lock
def __init__(self, space, name): self.name = name digest_type = self.digest_type_by_name(space) self.digest_size = rffi.getintfield(digest_type, 'c_md_size') # Allocate a lock for each HASH object. # An optimization would be to not release the GIL on small requests, # and use a custom lock only when needed. self.lock = Lock(space) ctx = lltype.malloc(ropenssl.EVP_MD_CTX.TO, flavor='raw') rgc.add_memory_pressure(HASH_MALLOC_SIZE + self.digest_size) ropenssl.EVP_DigestInit(ctx, digest_type) self.ctx = ctx
def inflateInit(wbits=MAX_WBITS): """ Allocate and return an opaque 'stream' object that can be used to decompress data. """ stream = lltype.malloc(z_stream, flavor='raw', zero=True) rgc.add_memory_pressure(rffi.sizeof(z_stream)) err = _inflateInit2(stream, wbits) if err == Z_OK: return stream else: try: if err == Z_STREAM_ERROR: raise ValueError("Invalid initialization option") else: raise RZlibError.fromstream(stream, err, "while creating decompression object") finally: lltype.free(stream, flavor='raw')
def inflateInit(wbits=MAX_WBITS): """ Allocate and return an opaque 'stream' object that can be used to decompress data. """ stream = lltype.malloc(z_stream, flavor='raw', zero=True) rgc.add_memory_pressure(rffi.sizeof(z_stream)) err = _inflateInit2(stream, wbits) if err == Z_OK: return stream else: try: if err == Z_STREAM_ERROR: raise ValueError("Invalid initialization option") else: raise RZlibError.fromstream( stream, err, "while creating decompression object") finally: lltype.free(stream, flavor='raw')
def deflateInit(level=Z_DEFAULT_COMPRESSION, method=Z_DEFLATED, wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=Z_DEFAULT_STRATEGY): """ Allocate and return an opaque 'stream' object that can be used to compress data. """ stream = lltype.malloc(z_stream, flavor='raw', zero=True) rgc.add_memory_pressure(rffi.sizeof(z_stream)) err = _deflateInit2(stream, level, method, wbits, memLevel, strategy) if err == Z_OK: return stream else: try: if err == Z_STREAM_ERROR: raise ValueError("Invalid initialization option") else: raise RZlibError.fromstream(stream, err, "while creating compression object") finally: lltype.free(stream, flavor='raw')
def deflateInit(level=Z_DEFAULT_COMPRESSION, method=Z_DEFLATED, wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=Z_DEFAULT_STRATEGY): """ Allocate and return an opaque 'stream' object that can be used to compress data. """ stream = lltype.malloc(z_stream, flavor='raw', zero=True) rgc.add_memory_pressure(rffi.sizeof(z_stream)) err = _deflateInit2(stream, level, method, wbits, memLevel, strategy) if err == Z_OK: return stream else: try: if err == Z_STREAM_ERROR: raise ValueError("Invalid initialization option") else: raise RZlibError.fromstream( stream, err, "while creating compression object") finally: lltype.free(stream, flavor='raw')
def __init__(self): self.ctx = lltype.malloc(ropenssl.EVP_MD_CTX.TO, flavor='raw') digest = ropenssl.EVP_get_digestbyname('sha1') ropenssl.EVP_DigestInit(self.ctx, digest) rgc.add_memory_pressure(HASH_MALLOC_SIZE + 64)
def ParserCreate(space, w_encoding=None, w_namespace_separator=None, w_intern=NoneNotWrapped): """ParserCreate([encoding[, namespace_separator]]) -> parser Return a new XML parser object.""" if space.is_w(w_encoding, space.w_None): encoding = None elif space.is_true(space.isinstance(w_encoding, space.w_str)): encoding = space.str_w(w_encoding) else: type_name = space.type(w_encoding).getname(space) raise OperationError( space.w_TypeError, space.wrap('ParserCreate() argument 1 must be string or None,' ' not %s' % (type_name,))) if space.is_w(w_namespace_separator, space.w_None): namespace_separator = 0 elif space.is_true(space.isinstance(w_namespace_separator, space.w_str)): separator = space.str_w(w_namespace_separator) if len(separator) == 0: namespace_separator = 0 elif len(separator) == 1: namespace_separator = ord(separator[0]) else: raise OperationError( space.w_ValueError, space.wrap('namespace_separator must be at most one character,' ' omitted, or None')) else: type_name = space.type(w_namespace_separator).getname(space) raise OperationError( space.w_TypeError, space.wrap('ParserCreate() argument 2 must be string or None,' ' not %s' % (type_name,))) # Explicitly passing None means no interning is desired. # Not passing anything means that a new dictionary is used. if w_intern is None: w_intern = space.newdict() elif space.is_w(w_intern, space.w_None): w_intern = None if namespace_separator: xmlparser = XML_ParserCreateNS( encoding, rffi.cast(rffi.CHAR, namespace_separator)) else: xmlparser = XML_ParserCreate(encoding) # Currently this is just the size of the pointer and some estimated bytes. # The struct isn't actually defined in expat.h - it is in xmlparse.c # XXX: find a good estimate of the XML_ParserStruct rgc.add_memory_pressure(XML_Parser_SIZE + 300) if not xmlparser: raise OperationError(space.w_RuntimeError, space.wrap('XML_ParserCreate failed')) parser = W_XMLParserType(space, xmlparser, w_intern) XML_SetUnknownEncodingHandler( parser.itself, UnknownEncodingHandlerData_callback, rffi.cast(rffi.VOIDP, parser.id)) return space.wrap(parser)