[PATCH 1/4] xcb: Limit the amount of SHM used

Uli Schlachter psychon at znc.in
Sun Jan 30 07:19:50 PST 2011


Without this, one could make cairo attach to lots of shared memory. I managed to
get 1.5 GiB of shared memory according to top.

Signed-off-by: Uli Schlachter <psychon at znc.in>
---
 src/cairo-xcb-shm.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/cairo-xcb-shm.c b/src/cairo-xcb-shm.c
index c3e3d35..55be475 100644
--- a/src/cairo-xcb-shm.c
+++ b/src/cairo-xcb-shm.c
@@ -43,6 +43,8 @@
 #include <sys/shm.h>
 #include <errno.h>
 
+#define CAIRO_MAX_SHM_MEMORY (16*1024*1024)
+
 /* a simple buddy allocator for memory pools
  * XXX fragmentation? use Doug Lea's malloc?
  */
@@ -489,6 +491,7 @@ _cairo_xcb_connection_allocate_shm_info (cairo_xcb_connection_t *connection,
     cairo_xcb_shm_info_t *shm_info;
     cairo_xcb_shm_mem_pool_t *pool, *next;
     size_t bytes, maxbits = 16, minbits = 8;
+    size_t shm_allocated = 0;
     void *mem = NULL;
     cairo_status_t status;
 
@@ -513,9 +516,16 @@ _cairo_xcb_connection_allocate_shm_info (cairo_xcb_connection_t *connection,
 	    _cairo_xcb_connection_shm_detach (connection,
 					      pool->shmseg);
 	    _cairo_xcb_shm_mem_pool_destroy (pool);
+	} else {
+	    shm_allocated += pool->max_bytes;
 	}
     }
 
+    if (unlikely (shm_allocated >= CAIRO_MAX_SHM_MEMORY)) {
+	CAIRO_MUTEX_UNLOCK (connection->shm_mutex);
+	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
+    }
+
     pool = malloc (sizeof (cairo_xcb_shm_mem_pool_t));
     if (unlikely (pool == NULL)) {
 	CAIRO_MUTEX_UNLOCK (connection->shm_mutex);
-- 
1.7.2.3


--------------060000060309050604010504
Content-Type: text/x-diff;
 name="0002-xcb-Remove-a-duplicate-static-function.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0002-xcb-Remove-a-duplicate-static-function.patch"



More information about the cairo mailing list