]> git.sesse.net Git - vlc/blobdiff - src/misc/block.c
Add block_shm_Alloc()
[vlc] / src / misc / block.c
index e4850e0aaf2cf38f0180c9328ac40ceb69e72457..b9a3ddb9335f3abb28f1d571a47c38496e6aa4ee 100644 (file)
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif
+#include <fcntl.h>
 
 #include <vlc_common.h>
 #include <vlc_block.h>
-#include <vlc_fs.h> /* For 64-bits lseek() definition */
+#include <vlc_fs.h>
 
 /**
  * @section Block handling functions.
@@ -74,7 +75,6 @@ static void block_Invalidate (block_t *block)
     block->p_next = NULL;
     block_Check (block);
     block->pf_release = BlockNoRelease;
-    barrier (); /* prevent compiler from optimizing this assignment out */
 }
 #else
 # define block_Check(b) ((void)(b))
@@ -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>
@@ -358,10 +407,13 @@ ssize_t pread (int fd, void *buf, size_t count, off_t offset)
 #endif
 
 /**
- * Loads a file into a block of memory. If possible a private file mapping is
- * created. Otherwise, the file is read normally. On 32-bits platforms, this
- * function will not work for very large files, due to memory space
- * constraints. Cancellation point.
+ * Loads a file into a block of memory through a file descriptor.
+ * If possible a private file mapping is created. Otherwise, the file is read
+ * normally. This function is a cancellation point.
+ *
+ * @note On 32-bits platforms,
+ * this function will not work for very large files,
+ * due to memory space constraints.
  *
  * @param fd file descriptor to load from
  * @return a new block with the file content at p_buffer, and file length at
@@ -433,6 +485,21 @@ block_t *block_File (int fd)
     return block;
 }
 
+/**
+ * Loads a file into a block of memory from the file path.
+ * See also block_File().
+ */
+block_t *block_FilePath (const char *path)
+{
+    int fd = vlc_open (path, O_RDONLY);
+    if (fd == -1)
+        return NULL;
+
+    block_t *block = block_File (fd);
+    close (fd);
+    return block;
+}
+
 /**
  * @section Thread-safe block queue functions
  */