]> git.sesse.net Git - vlc/blobdiff - src/misc/block.c
Win32 threads: remove functions forbidden on Windows Store
[vlc] / src / misc / block.c
index daad650525db4948b2c43ecd1cdfb655f0651883..a7dfed914e842ea9c894fe8abcee56c564076398 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>
 
 /**
  * @section Block handling functions.
@@ -48,6 +50,35 @@ static void BlockNoRelease( block_t *b )
     fprintf( stderr, "block %p has no release callback! This is a bug!\n", b );
     abort();
 }
+
+static void block_Check (block_t *block)
+{
+    while (block != NULL)
+    {
+        unsigned char *start = block->p_start;
+        unsigned char *end = block->p_start + block->i_size;
+        unsigned char *bufstart = block->p_buffer;
+        unsigned char *bufend = block->p_buffer + block->i_buffer;
+
+        assert (block->pf_release != BlockNoRelease);
+        assert (start <= end);
+        assert (bufstart <= bufend);
+        assert (bufstart >= start);
+        assert (bufend <= end);
+
+        block = block->p_next;
+    }
+}
+
+static void block_Invalidate (block_t *block)
+{
+    block->p_next = NULL;
+    block_Check (block);
+    block->pf_release = BlockNoRelease;
+}
+#else
+# define block_Check(b) ((void)(b))
+# define block_Invalidate(b) ((void)(b))
 #endif
 
 void block_Init( block_t *restrict b, void *buf, size_t size )
@@ -68,9 +99,12 @@ void block_Init( block_t *restrict b, void *buf, size_t size )
 #endif
 }
 
-static void BlockRelease( block_t *p_block )
+static void block_generic_Release (block_t *block)
 {
-    free( p_block );
+    /* That is always true for blocks allocated with block_Alloc(). */
+    assert (block->p_start == (unsigned char *)(block + 1));
+    block_Invalidate (block);
+    free (block);
 }
 
 static void BlockMetaCopy( block_t *restrict out, const block_t *in )
@@ -83,11 +117,15 @@ static void BlockMetaCopy( block_t *restrict out, const block_t *in )
     out->i_length  = in->i_length;
 }
 
-/* Memory alignment (must be a multiple of sizeof(void*) and a power of two) */
-#define BLOCK_ALIGN        16
-/* Initial reserved header and footer size (must be multiple of alignment) */
+/** Initial memory alignment of data block.
+ * @note This must be a multiple of sizeof(void*) and a power of two.
+ * libavcodec AVX optimizations require at least 32-bytes. */
+#define BLOCK_ALIGN        32
+
+/** Initial reserved header and footer size. */
 #define BLOCK_PADDING      32
-/* Maximum size of reserved footer before we release with realloc() */
+
+/* Maximum size of reserved footer before shrinking with realloc(). */
 #define BLOCK_WASTE_SIZE   2048
 
 block_t *block_Alloc (size_t size)
@@ -102,13 +140,13 @@ block_t *block_Alloc (size_t size)
     if (unlikely(b == NULL))
         return NULL;
 
-    block_Init (b, b + 1, alloc);
+    block_Init (b, b + 1, alloc - sizeof (*b));
     static_assert ((BLOCK_PADDING % BLOCK_ALIGN) == 0,
                    "BLOCK_PADDING must be a multiple of BLOCK_ALIGN");
     b->p_buffer += BLOCK_PADDING + BLOCK_ALIGN - 1;
     b->p_buffer = (void *)(((uintptr_t)b->p_buffer) & ~(BLOCK_ALIGN - 1));
     b->i_buffer = size;
-    b->pf_release = BlockRelease;
+    b->pf_release = block_generic_Release;
     return b;
 }
 
@@ -116,6 +154,8 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
 {
     size_t requested = i_prebody + i_body;
 
+    block_Check( p_block );
+
     /* Corner case: empty block requested */
     if( i_prebody <= 0 && i_body <= (size_t)(-i_prebody) )
     {
@@ -225,6 +265,7 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
 
 static void block_heap_Release (block_t *block)
 {
+    block_Invalidate (block);
     free (block->p_start);
     free (block);
 }
@@ -260,6 +301,7 @@ block_t *block_heap_Alloc (void *addr, size_t length)
 
 static void block_mmap_Release (block_t *block)
 {
+    block_Invalidate (block);
     munmap (block->p_start, block->i_size);
     free (block);
 }
@@ -297,12 +339,58 @@ 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
+#ifdef _WIN32
 # include <io.h>
-# ifdef UNDER_CE
-#  define _get_osfhandle(a) ((long) (a))
-# endif
 
 static
 ssize_t pread (int fd, void *buf, size_t count, off_t offset)
@@ -320,35 +408,16 @@ ssize_t pread (int fd, void *buf, size_t count, off_t offset)
         return written;
     return -1;
 }
-#elif !defined( HAVE_PREAD )
-static
-ssize_t pread(int fd, const void * buf, size_t size, off_t offset) {
-    off_t offs0;
-    ssize_t rd;
-    if ((offs0 = lseek(fd, 0, SEEK_CUR)) == (off_t)-1) return -1;
-    if (lseek(fd, offset, SEEK_SET) == (off_t)-1) return -1;
-    rd = read(fd, (void *)buf, size);
-    if (lseek(fd, offs0, SEEK_SET) == (off_t)-1) return -1;
-    return rd;
-}
-
-static
-ssize_t pwrite(int fd, const void * buf, size_t size, off_t offset) {
-    off_t offs0;
-    ssize_t wr;
-    if ((offs0 = lseek(fd, 0, SEEK_CUR)) == (off_t)-1) return -1;
-    if (lseek(fd, offset, SEEK_SET) == (off_t)-1) return -1;
-    wr = write(fd, (void *)buf, size);
-    if (lseek(fd, offs0, SEEK_SET) == (off_t)-1) return -1;
-    return wr;
-}
 #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
@@ -381,7 +450,7 @@ block_t *block_File (int fd)
     }
 
     /* Prevent an integer overflow in mmap() and malloc() */
-    if (st.st_size >= SIZE_MAX)
+    if ((uintmax_t)st.st_size >= SIZE_MAX)
     {
         errno = ENOMEM;
         return NULL;
@@ -420,6 +489,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
  */