[vlc-commits] Add block_shm_Alloc()

Rémi Denis-Courmont git at videolan.org
Sat Jan 12 22:50:28 CET 2013


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Thu Jul 22 23:15:48 2010 +0300| [bce10e7649067b4b214f5fa4eed32e46ed6d2ddf] | committer: Rémi Denis-Courmont

Add block_shm_Alloc()

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=bce10e7649067b4b214f5fa4eed32e46ed6d2ddf
---

 include/vlc_block.h |    1 +
 src/libvlccore.sym  |    1 +
 src/misc/block.c    |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)

diff --git a/include/vlc_block.h b/include/vlc_block.h
index 2e256b4..e56e4c7 100644
--- a/include/vlc_block.h
+++ b/include/vlc_block.h
@@ -168,6 +168,7 @@ static inline void block_Release( block_t *p_block )
 
 VLC_API block_t *block_heap_Alloc(void *, size_t) VLC_USED VLC_MALLOC;
 VLC_API block_t *block_mmap_Alloc(void *addr, size_t length) VLC_USED VLC_MALLOC;
+VLC_API block_t * block_shm_Alloc(void *addr, size_t length) VLC_USED VLC_MALLOC;
 VLC_API block_t *block_File(int fd) VLC_USED VLC_MALLOC;
 VLC_API block_t *block_FilePath(const char *) VLC_USED VLC_MALLOC;
 
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index f917612..d9caefb 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -32,6 +32,7 @@ block_FilePath
 block_heap_Alloc
 block_Init
 block_mmap_Alloc
+block_shm_Alloc
 block_Realloc
 config_AddIntf
 config_ChainCreate
diff --git a/src/misc/block.c b/src/misc/block.c
index 069628a..b9a3ddb 100644
--- a/src/misc/block.c
+++ b/src/misc/block.c
@@ -335,6 +335,55 @@ block_t *block_mmap_Alloc (void *addr, size_t length)
 }
 #endif
 
+#ifdef HAVE_SYS_SHM_H
+# include <sys/shm.h>
+
+typedef struct block_shm_t
+{
+    block_t     self;
+    void       *base_addr;
+} block_shm_t;
+
+static void block_shm_Release (block_t *block)
+{
+    block_shm_t *p_sys = (block_shm_t *)block;
+
+    shmdt (p_sys->base_addr);
+    free (p_sys);
+}
+
+/**
+ * Creates a block from a System V shared memory segment (shmget()).
+ * This is provided by LibVLC so that segments can safely be deallocated
+ * even after the allocating plugin has been unloaded from memory.
+ *
+ * @param addr base address of the segment (as returned by shmat())
+ * @param length length (bytes) of the segment (as passed to shmget())
+ * @return NULL if an error occurred (in that case, shmdt(addr) is invoked
+ * before returning NULL).
+ */
+block_t *block_shm_Alloc (void *addr, size_t length)
+{
+    block_shm_t *block = malloc (sizeof (*block));
+    if (unlikely(block == NULL))
+    {
+        shmdt (addr);
+        return NULL;
+    }
+
+    block_Init (&block->self, (uint8_t *)addr, length);
+    block->self.pf_release = block_shm_Release;
+    block->base_addr = addr;
+    return &block->self;
+}
+#else
+block_t *block_shm_Alloc (void *addr, size_t length)
+{
+    (void) addr; (void) length;
+    abort ();
+}
+#endif
+
 
 #ifdef WIN32
 # include <io.h>



More information about the vlc-commits mailing list