MCPcopy Create free account
hub / github.com/LMCache/LMCache / alloc_shm_pinned_ptr

Function alloc_shm_pinned_ptr

lmcache/python_ops_fallback.py:455–482  ·  view source on GitHub ↗

Non-CUDA equivalent of allocating shared memory pinned pointer. Uses multiprocessing.shared_memory for cross-platform POSIX shm.

(size: int, shm_name: str = "")

Source from the content-addressed store, hash-verified

453
454
455def alloc_shm_pinned_ptr(size: int, shm_name: str = "") -> int:
456 """Non-CUDA equivalent of allocating shared memory pinned pointer.
457 Uses multiprocessing.shared_memory for cross-platform POSIX shm."""
458
459 # Strip leading '/' for SharedMemory name
460 name = shm_name.lstrip("/") if shm_name else None
461
462 # Clean up stale shm segment if it exists
463 if name:
464 try:
465 stale = shared_memory.SharedMemory(name=name, create=False)
466 stale.close()
467 stale.unlink()
468 except FileNotFoundError:
469 pass
470
471 shm = shared_memory.SharedMemory(name=name, create=True, size=size)
472
473 array_type = ctypes.c_uint8 * size
474 buf = array_type.from_buffer(shm.buf)
475 ptr = ctypes.addressof(buf)
476
477 # Store references to keep them alive
478 tensor = torch.frombuffer(buf, dtype=torch.uint8)
479 _tensor_registry[ptr] = tensor
480 _buf_registry[ptr] = buf
481 _shm_registry[ptr] = shm
482 return ptr
483
484
485def free_shm_pinned_ptr(ptr: int, size: int = 0, shm_name: str = "") -> None:

Callers

nothing calls this directly

Calls 1

closeMethod · 0.45

Tested by

no test coverage detected