[gs-cvs] rev 8639 - branches/mtrender/src
ray at ghostscript.com
ray at ghostscript.com
Sun Apr 13 21:53:26 PDT 2008
Author: ray
Date: 2008-04-13 21:53:26 -0700 (Sun, 13 Apr 2008)
New Revision: 8639
Added:
branches/mtrender/src/gsmchunk.c
branches/mtrender/src/gsmchunk.h
Modified:
branches/mtrender/src/gxclthrd.c
branches/mtrender/src/gxclthrd.h
branches/mtrender/src/gxclthrd1.c
branches/mtrender/src/lib.mak
Log:
Add a 'chunk' memory allocator wrapper for threads so that the mutex contention
will be minimized. On files with extreme memory monitor mutex contention, this
results in a SUBSTANTIAL performance improvement when multiple threads are used.
Prior to this, some files would run faster with single thread processing
(-dNumRenderingThreads=0) than with -dNumRenderingThreads=2 on a a dual core
system.
DETAILS:
The chunk memory layer is implemented as a 'wrapper' on the underlying
gs_memory_t allocator, so that this can be used with PCL (plalloc.c) as
well as on any other base allocator, whether or not it is 'wrapped' in
a 'locking' allocator.
This wrapper is thread safe, so that each thread in a multi-threaded
rendering system is given its own chunk based wrapper. This means that
the mutex locking need only occur when the chunk wrapper allocates or
frees axn chunk.
The chunk management and object management within the chunks expect
that 'small' objects are the most common. 'LARGE' objects are put in
their own chunk (to prevent sandbars) and the chunks for small objects
are placed in chunks at the 'front' of the list. The objects are
optimized for LIFO allocation and free patterns -- the ordered free
list provides for annealing of adjacent free blocks, both preceding
and following, so that sandbars within the chunks are minimized
(given the care taken in most of the code to observe LIFO alloc/
free rules).
Added: branches/mtrender/src/gsmchunk.c
===================================================================
--- branches/mtrender/src/gsmchunk.c 2008-04-14 02:46:20 UTC (rev 8638)
+++ branches/mtrender/src/gsmchunk.c 2008-04-14 04:53:26 UTC (rev 8639)
@@ -0,0 +1,694 @@
+/* Copyright (C) 2001-2006 Artifex Software, Inc.
+ All Rights Reserved.
+
+ This software is provided AS-IS with no warranty, either express or
+ implied.
+
+ This software is distributed under license and may not be copied, modified
+ or distributed except as expressly authorized under the terms of that
+ license. Refer to licensing information at http://www.artifex.com/
+ or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
+ San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
+*/
+
+/* $Id:$ */
+/* chunk consolidating wrapper on a base memory allocator */
+
+#include "gx.h"
+#include "gsstype.h"
+#include "gserrors.h"
+#include "gsmchunk.h"
+
+/* Raw memory procedures */
+static gs_memory_proc_alloc_bytes(chunk_alloc_bytes_immovable);
+static gs_memory_proc_resize_object(chunk_resize_object);
+static gs_memory_proc_free_object(chunk_free_object);
+static gs_memory_proc_stable(chunk_stable);
+static gs_memory_proc_status(chunk_status);
+static gs_memory_proc_free_all(chunk_free_all);
+static gs_memory_proc_consolidate_free(chunk_consolidate_free);
+
+/* Object memory procedures */
+static gs_memory_proc_alloc_bytes(chunk_alloc_bytes);
+static gs_memory_proc_alloc_struct(chunk_alloc_struct);
+static gs_memory_proc_alloc_struct(chunk_alloc_struct_immovable);
+static gs_memory_proc_alloc_byte_array(chunk_alloc_byte_array);
+static gs_memory_proc_alloc_byte_array(chunk_alloc_byte_array_immovable);
+static gs_memory_proc_alloc_struct_array(chunk_alloc_struct_array);
+static gs_memory_proc_alloc_struct_array(chunk_alloc_struct_array_immovable);
+static gs_memory_proc_object_size(chunk_object_size);
+static gs_memory_proc_object_type(chunk_object_type);
+static gs_memory_proc_alloc_string(chunk_alloc_string);
+static gs_memory_proc_alloc_string(chunk_alloc_string_immovable);
+static gs_memory_proc_resize_string(chunk_resize_string);
+static gs_memory_proc_free_string(chunk_free_string);
+static gs_memory_proc_register_root(chunk_register_root);
+static gs_memory_proc_unregister_root(chunk_unregister_root);
+static gs_memory_proc_enable_free(chunk_enable_free);
+static const gs_memory_procs_t chunk_procs =
+{
+ /* Raw memory procedures */
+ chunk_alloc_bytes_immovable,
+ chunk_resize_object,
+ chunk_free_object,
+ chunk_stable,
+ chunk_status,
+ chunk_free_all,
+ chunk_consolidate_free,
+ /* Object memory procedures */
+ chunk_alloc_bytes,
+ chunk_alloc_struct,
+ chunk_alloc_struct_immovable,
+ chunk_alloc_byte_array,
+ chunk_alloc_byte_array_immovable,
+ chunk_alloc_struct_array,
+ chunk_alloc_struct_array_immovable,
+ chunk_object_size,
+ chunk_object_type,
+ chunk_alloc_string,
+ chunk_alloc_string_immovable,
+ chunk_resize_string,
+ chunk_free_string,
+ chunk_register_root,
+ chunk_unregister_root,
+ chunk_enable_free
+};
+
+typedef struct chunk_obj_node_s {
+ struct chunk_obj_node_s *next;
+ uint size; /* objlist: client size */
+ /* if freelist: size of block (obj header and client area must fit in block) */
+ gs_memory_type_ptr_t type;
+} chunk_obj_node_t;
+
+/*
+ * Note: All objects within a chunk are 'aligned' since we round_up_to_align
+ * the free list pointer when removing part of a free area.
+ */
+typedef struct chunk_mem_node_s {
+ uint size;
+ uint largest_free; /* quick check when allocating */
+ struct chunk_mem_node_s *next;
+ chunk_obj_node_t *objlist; /* head of objects in this chunk (no order) */
+ chunk_obj_node_t *freelist; /* free list (ordered) */
+ /* chunk data follows immediately */
+} chunk_mem_node_t;
+
+typedef struct gs_memory_chunk_s {
+ gs_memory_common; /* interface outside world sees */
+ gs_memory_t *target; /* base allocator */
+ chunk_mem_node_t *head_chunk;
+} gs_memory_chunk_t;
+
+/* ---------- Public constructors/destructors ---------- */
+
+/* Initialize a gs_memory_chunk_t */
+int /* -ve error code or 0 */
+gs_memory_chunk_wrap( gs_memory_t **wrapped, /* chunk allocator init */
+ gs_memory_t * target ) /* base allocator */
+{
+ int code;
+ gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)
+ gs_alloc_bytes_immovable(target, sizeof(gs_memory_chunk_t),
+ "gs_malloc_wrap(chunk)");
+ *wrapped = NULL; /* don't leave garbage in case we fail */
+ if (cmem == 0)
+ return_error(gs_error_VMerror);
+ cmem->stable_memory = 0; /* ??? copied from locking wrapper */
+ cmem->procs = chunk_procs;
+ cmem->gs_lib_ctx = target->gs_lib_ctx;
+ cmem->non_gc_memory = (gs_memory_t *)cmem;
+ cmem->target = target;
+ cmem->head_chunk = NULL;
+
+ /* Init the chunk management values */
+
+ *wrapped = (gs_memory_t *)cmem;
+ return 0;
+}
+
+/* Release a chunk memory manager. */
+/* Note that this has no effect on the target. */
+void
+gs_memory_chunk_release(gs_memory_t *mem)
+{
+ gs_memory_free_all((gs_memory_t *)mem, FREE_ALL_EVERYTHING,
+ "gs_memory_chunk_release");
+}
+
+/* ---------- Accessors ------------- */
+
+/* Retrieve this allocator's target */
+gs_memory_t *
+gs_memory_chunk_target(const gs_memory_t *mem)
+{
+ gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;
+ return cmem->target;
+}
+
+/* -------- Private members --------- */
+
+/* Note that all of the data is 'immovable' and is opaque to the base allocator */
+/* thus even if it is a GC type of allocator, no GC functions will be applied */
+/* All allocations are done in the non_gc_memory of the base */
+
+/* Procedures */
+
+static void
+chunk_mem_node_free_all_remaining(gs_memory_chunk_t *cmem)
+{
+ chunk_mem_node_t *head = cmem->head_chunk;
+ gs_memory_t * const target = cmem->target;
+ chunk_mem_node_t *current;
+ chunk_mem_node_t *next;
+
+ current = head;
+ while ( current != NULL ) {
+ next = current->next;
+ gs_free_object(target, current, "chunk_mem_node_remove");
+ current = next;
+ }
+ cmem->head_chunk = NULL;
+}
+
+static void
+chunk_free_all(gs_memory_t * mem, uint free_mask, client_name_t cname)
+{
+ gs_memory_chunk_t * const cmem = (gs_memory_chunk_t *)mem;
+ gs_memory_t * const target = cmem->target;
+
+ /* Only free the structures and the allocator itself. */
+ if (mem->stable_memory) {
+ if (mem->stable_memory != mem)
+ gs_memory_free_all(mem->stable_memory, free_mask, cname);
+ if (free_mask & FREE_ALL_ALLOCATOR)
+ mem->stable_memory = 0;
+ }
+ if (free_mask & FREE_ALL_DATA) {
+ chunk_mem_node_free_all_remaining(cmem);
+ }
+ if (free_mask & FREE_ALL_STRUCTURES) {
+ cmem->target = 0;
+ }
+ if (free_mask & FREE_ALL_ALLOCATOR)
+ gs_free_object(target, cmem, cname);
+}
+
+extern const gs_memory_struct_type_t st_bytes;
+
+/* round up objects to make sure we have room for a header left */
+inline static uint
+round_up_to_align(uint size)
+{
+ uint num_node_headers = (size + sizeof(chunk_obj_node_t) - 1) / sizeof(chunk_obj_node_t);
+
+ return num_node_headers * sizeof(chunk_obj_node_t);
+}
+
+#define IS_SINGLE_OBJ_SIZE(chunk_size) \
+ (chunk_size > (CHUNK_SIZE>>1))
+#define MULTIPLE_OBJ_CHUNK_SIZE \
+ (sizeof(chunk_mem_node_t) + round_up_to_align(CHUNK_SIZE))
+
+
+/* return -1 on error, 0 on success */
+int
+chunk_mem_node_add(gs_memory_chunk_t *cmem, uint size_needed, chunk_mem_node_t **newchunk)
+{
+ chunk_mem_node_t *node, *prev_node;
+ gs_memory_t *target = cmem->target;
+ /* Allocate enough for the chunk header, and the size_needed */
+ /* The size needed already includes the object header from caller */
+ /* and is already rounded up to the obj_node_t sized elements */
+ uint chunk_size = size_needed + sizeof(chunk_mem_node_t);
+ bool is_multiple_object_node = false;
+
+ /* Objects > half the default chunk size get their own chunk */
+ if ( ! IS_SINGLE_OBJ_SIZE(chunk_size)) {
+ chunk_size = MULTIPLE_OBJ_CHUNK_SIZE; /* the size for collections of objects */
+ is_multiple_object_node = true;
+ }
+
+ *newchunk = NULL;
+ node = (chunk_mem_node_t *)gs_alloc_bytes_immovable(target, chunk_size, "chunk_mem_node_add");
+ if ( node == NULL )
+ return -1;
+ node->size = chunk_size; /* how much we allocated */
+ node->largest_free = chunk_size - sizeof(chunk_mem_node_t);
+ node->objlist = NULL;
+ node->freelist = (chunk_obj_node_t *)((byte *)(node) + sizeof(chunk_mem_node_t));
+ node->freelist->next = NULL;
+ node->freelist->size = node->largest_free;
+
+ prev_node = NULL;
+ if (!is_multiple_object_node) {
+ chunk_mem_node_t *scan_node;
+
+ /* Scan past chunks that are collections of smaller chunks */
+ /* This allows the most frequently accessed chunks to be near the head */
+ for (scan_node = cmem->head_chunk; scan_node != NULL; scan_node = scan_node->next) {
+ if (scan_node->size != MULTIPLE_OBJ_CHUNK_SIZE)
+ break;
+ prev_node = scan_node;
+ }
+ }
+ if (prev_node == NULL) {
+ if (cmem->head_chunk == NULL) {
+ cmem->head_chunk = node;
+ node->next = NULL;
+ } else {
+ node->next = cmem->head_chunk;
+ cmem->head_chunk = node;
+ }
+ } else {
+ node->next = prev_node->next;
+ prev_node->next = node;
+ }
+
+ *newchunk = node; /* return the chunk we just allocated */
+ return 0;
+}
+
+static int
+chunk_mem_node_remove(gs_memory_chunk_t *cmem, chunk_mem_node_t *addr)
+{
+ chunk_mem_node_t *head = cmem->head_chunk;
+ gs_memory_t * const target = cmem->target;
+
+ /* check the head first */
+ if (head == NULL) {
+ dprintf("FAIL - no nodes to be removed\n" );
+ return -1;
+ }
+ if (head == addr) {
+ cmem->head_chunk = head->next;
+ gs_free_object(target, head, "chunk_mem_node_remove");
+ } else {
+ chunk_mem_node_t *current;
+ bool found = false;
+
+ /* scan the list, stopping in front of element */
+ for (current = head; current != NULL; current = current->next) {
+ if ( current->next && (current->next == addr) ) {
+ current->next = current->next->next; /* de-link it */
+ gs_free_object(target, addr, "chunk_mem_node_remove");
+ found = true;
+ break;
+ }
+ }
+ if ( !found ) {
+ dprintf1("FAIL freeing wild pointer freed address %x not found\n", (uint)addr );
+ return -1;
+ }
+ }
+ return 0;
+}
+
+/* all of the allocation routines reduce to the this function */
+static byte *
+chunk_obj_alloc(gs_memory_t *mem, uint size, gs_memory_type_ptr_t type, client_name_t cname)
+{
+ gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;
+ chunk_mem_node_t *head = cmem->head_chunk;
+ uint newsize, free_size;
+ chunk_obj_node_t *newobj = NULL;
+ chunk_obj_node_t *free_obj, *prev_free, *new_free;
+ chunk_mem_node_t *current;
+ bool rescan_free_list;
+
+ newsize = round_up_to_align(size + sizeof(chunk_obj_node_t)); /* space we will need */
+
+ /* Search the chunks for one with a large enough free area */
+ for (current = head; current != NULL; current = current->next) {
+ if ( current->largest_free >= newsize)
+ break;
+ }
+ if (current == NULL) {
+ /* No chunks with enough space, allocate one */
+ if (chunk_mem_node_add(cmem, newsize, ¤t) < 0)
+ return NULL;
+ }
+ /* Find the first free area in the current chunk that is big enough */
+ /* LATER: might be better to find the 'best fit' */
+ prev_free = NULL; /* NULL means chunk */
+ for (free_obj = current->freelist; free_obj != NULL; free_obj=free_obj->next) {
+ if (free_obj->size >= newsize)
+ break;
+ prev_free = free_obj; /* keep track so we can update link */
+ }
+
+ if (free_obj == NULL) {
+ dprintf2("largest_free value = %d is too large, cannot find room for size = %d\n",
+ current->largest_free, newsize);
+ return NULL;
+ }
+
+ /* If this free object's size == largest_free, we'll have to re-scan */
+ rescan_free_list = free_obj->size == current->largest_free;
+
+ /* Make an object in the free_obj we found above, reducing it's size */
+ /* and adjusting the free list preserving alignment */
+ newobj = free_obj;
+ free_size = free_obj->size - newsize; /* amount remaining */
+ new_free = (chunk_obj_node_t *)((byte *)(free_obj) + newsize); /* start of remaining free area */
+ if (free_size >= sizeof(chunk_obj_node_t)) {
+ if (prev_free != NULL)
+ prev_free->next = new_free;
+ else
+ current->freelist = new_free;
+ new_free->next = free_obj->next;
+ new_free->size = free_size;
+ } else {
+ /* Not enough space remaining, just skip around it */
+ if (prev_free != NULL)
+ prev_free->next = free_obj->next;
+ else
+ current->freelist = free_obj->next;
+ }
+
+#ifdef DEBUG
+memset((byte *)(newobj) + sizeof(chunk_obj_node_t), 0xa1, newsize - sizeof(chunk_obj_node_t));
+memset((byte *)(newobj) + sizeof(chunk_obj_node_t), 0xac, size);
+#endif
+
+ newobj->next = current->objlist; /* link to start of list */
+ current->objlist = newobj;
+ newobj->size = size; /* client requested size */
+ newobj->type = type; /* and client desired type */
+
+ /* If we flagged for re-scan to find the new largest_free, do it now */
+ if (rescan_free_list) {
+ current->largest_free = 0;
+ for (free_obj = current->freelist; free_obj != NULL; free_obj=free_obj->next)
+ if (free_obj->size > current->largest_free)
+ current->largest_free = free_obj->size;
+ }
+
+ /* return the client area of the object we allocated */
+ return (byte *)(newobj) + sizeof(chunk_obj_node_t);
+}
+
+static byte *
+chunk_alloc_bytes_immovable(gs_memory_t * mem, uint size, client_name_t cname)
+{
+ return chunk_obj_alloc(mem, size, &st_bytes, cname);
+}
+
+static byte *
+chunk_alloc_bytes(gs_memory_t * mem, uint size, client_name_t cname)
+{
+ return chunk_obj_alloc(mem, size, &st_bytes, cname);
+}
+
+static void *
+chunk_alloc_struct_immovable(gs_memory_t * mem, gs_memory_type_ptr_t pstype,
+ client_name_t cname)
+{
+ return chunk_obj_alloc(mem, pstype->ssize, pstype, cname);
+}
+
+static void *
+chunk_alloc_struct(gs_memory_t * mem, gs_memory_type_ptr_t pstype,
+ client_name_t cname)
+{
+ return chunk_obj_alloc(mem, pstype->ssize, pstype, cname);
+}
+
+static byte *
+chunk_alloc_byte_array_immovable(gs_memory_t * mem, uint num_elements,
+ uint elt_size, client_name_t cname)
+{
+ return chunk_alloc_bytes(mem, num_elements * elt_size, cname);
+}
+
+static byte *
+chunk_alloc_byte_array(gs_memory_t * mem, uint num_elements, uint elt_size,
+ client_name_t cname)
+{
+ return chunk_alloc_bytes(mem, num_elements * elt_size, cname);
+}
+
+static void *
+chunk_alloc_struct_array_immovable(gs_memory_t * mem, uint num_elements,
+ gs_memory_type_ptr_t pstype, client_name_t cname)
+{
+ return chunk_obj_alloc(mem, num_elements * pstype->ssize, pstype, cname);
+}
+
+static void *
+chunk_alloc_struct_array(gs_memory_t * mem, uint num_elements,
+ gs_memory_type_ptr_t pstype, client_name_t cname)
+{
+ return chunk_obj_alloc(mem, num_elements * pstype->ssize, pstype, cname);
+}
+
+
+static void *
+chunk_resize_object(gs_memory_t * mem, void *ptr, uint new_num_elements, client_name_t cname)
+{
+ /* get the type from the old object */
+ chunk_obj_node_t *obj = ((chunk_obj_node_t *)ptr) - 1;
+ uint new_size = (obj->type->ssize * new_num_elements);
+
+ /* This isn't particularly efficient, but it is rarely used */
+ chunk_free_object(mem, ptr, cname);
+ return chunk_obj_alloc(mem, new_size, obj->type, cname);
+}
+
+static void
+chunk_free_object(gs_memory_t * mem, void *ptr, client_name_t cname)
+{
+ if (ptr == NULL )
+ return;
+ {
+ /* back up to obj header */
+ chunk_obj_node_t *obj = ((chunk_obj_node_t *)ptr) - 1;
+ void (*finalize)(void *ptr) = obj->type->finalize;
+ gs_memory_chunk_t * const cmem = (gs_memory_chunk_t *)mem;
+ chunk_mem_node_t *current;
+ chunk_obj_node_t *free_obj, *prev_free, *new_free;
+ chunk_obj_node_t *scan_obj, *prev_obj;
+ /* space we will free */
+ uint freed_size = round_up_to_align(obj->size + sizeof(chunk_obj_node_t));
+
+ if ( finalize != NULL )
+ finalize(ptr);
+
+ /* Find the chunk containing this object */
+ for (current = cmem->head_chunk; current != NULL; current = current->next) {
+ if (((byte *)obj > (byte *)current) && ((byte *)obj < (byte *)(current) + current->size))
+ break;
+ }
+ if (current == NULL) {
+ /* Object not found in any chunk */
+ dprintf1("chunk_free_obj failed, object %0x not in any chunk\n", obj);
+ return;
+ }
+
+ /* Scan obj list to find this element */
+ prev_obj = NULL; /* object is head, linked to mem node */
+ for (scan_obj = current->objlist; scan_obj != NULL; scan_obj = scan_obj->next) {
+ if (scan_obj == obj)
+ break;
+ prev_obj = scan_obj;
+ }
+ if (scan_obj == NULL) {
+ /* Object not found in expected chunk */
+ dprintf3("chunk_free_obj failed, object %0x not in chunk at %0x, size = %0x\n", obj, current, current->size);
+ return;
+ }
+ /* link around the object being freed */
+ if (prev_obj == NULL)
+ current->objlist = obj->next;
+ else
+ prev_obj->next = obj->next;
+
+ /* Add this object's space (including the header) to the free list */
+
+ /* Scan free list to find where this element goes */
+ obj->size = freed_size; /* adjust size to include chunk_obj_node and pad */
+
+ prev_free = NULL;
+ for (free_obj = current->freelist; free_obj != NULL; free_obj = free_obj->next) {
+ if (obj < free_obj)
+ break;
+ prev_free = free_obj;
+ }
+ if (prev_free == NULL) {
+ /* this object is before any other free objects */
+ obj->next = current->freelist;
+ current->freelist = obj;
+ } else {
+ obj->next = free_obj;
+ prev_free->next = obj;
+ }
+ /* If the end of this object is adjacent to the next free space,
+ * merge the two. Next we'll merge with predecessor (prev_free)
+ */
+ if (free_obj != NULL) {
+ byte *after_obj = (byte*)(obj) + freed_size;
+
+ if (free_obj <= (chunk_obj_node_t *)after_obj) {
+ /* Object is adjacent to following free space block -- merge it */
+ obj->next = free_obj->next; /* link around the one being absorbed */
+ obj->size = (byte *)(free_obj) - (byte *)(obj) + free_obj->size;
+ }
+ }
+ /* the prev_free object precedes this object that is now free,
+ * it _may_ be adjacent
+ */
+ if (prev_free != NULL) {
+ byte *after_free = (byte*)(prev_free) + prev_free->size;
+
+ if (obj <= (chunk_obj_node_t *)after_free) {
+ /* Object is adjacent to prior free space block -- merge it */
+ /* NB: this is the common case with LIFO alloc-free patterns */
+ /* (LIFO: Last-allocated, first freed) */
+ prev_free->size = (byte *)(obj) - (byte *)(prev_free) + obj->size;
+ prev_free->next = obj->next; /* link around 'obj' area */
+ obj = prev_free;
+ }
+ }
+#ifdef DEBUG
+memset((byte *)(obj) + sizeof(chunk_obj_node_t), 0xf1, obj->size - sizeof(chunk_obj_node_t));
+#endif
+ if (current->largest_free < obj->size)
+ current->largest_free = obj->size;
+
+ /* If this chunk is now totally empty, free it */
+ if (current->objlist == NULL) {
+ if (current->size != current->freelist->size + sizeof(chunk_mem_node_t))
+ dprintf2("chunk freelist size not correct, is: %d, should be: %d\n",
+ round_up_to_align(current->freelist->size + sizeof(chunk_mem_node_t)), current->size);
+ chunk_mem_node_remove(cmem, current);
+ }
+ }
+}
+
+static byte *
+chunk_alloc_string_immovable(gs_memory_t * mem, uint nbytes, client_name_t cname)
+{
+ /* we just alloc bytes here */
+ return chunk_alloc_bytes(mem, nbytes, cname);
+}
+
+static byte *
+chunk_alloc_string(gs_memory_t * mem, uint nbytes, client_name_t cname)
+{
+ /* we just alloc bytes here */
+ return chunk_alloc_bytes(mem, nbytes, cname);
+}
+
+static byte *
+chunk_resize_string(gs_memory_t * mem, byte * data, uint old_num, uint new_num,
+ client_name_t cname)
+{
+ /* just resize object - ignores old_num */
+ return chunk_resize_object(mem, data, new_num, cname);
+}
+
+static void
+chunk_free_string(gs_memory_t * mem, byte * data, uint nbytes,
+ client_name_t cname)
+{
+ chunk_free_object(mem, data, cname);
+}
+
+static void
+chunk_status(gs_memory_t * mem, gs_memory_status_t * pstat)
+{
+}
+
+static gs_memory_t *
+chunk_stable(gs_memory_t * mem)
+{
+ return mem;
+}
+
+static void
+chunk_enable_free(gs_memory_t * mem, bool enable)
+{
+}
+
+static void
+chunk_consolidate_free(gs_memory_t *mem)
+{
+}
+
+/* aceesors to get size and type given the pointer returned to the client */
+static uint
+chunk_object_size(gs_memory_t * mem, const void *ptr)
+{
+ chunk_obj_node_t *obj = ((chunk_obj_node_t *)ptr) - 1;
+
+ return obj->size;
+}
+
+static gs_memory_type_ptr_t
+chunk_object_type(const gs_memory_t * mem, const void *ptr)
+{
+ chunk_obj_node_t *obj = ((chunk_obj_node_t *)ptr) - 1;
+ return obj->type;
+}
+
+static int
+chunk_register_root(gs_memory_t * mem, gs_gc_root_t * rp, gs_ptr_type_t ptype,
+ void **up, client_name_t cname)
+{
+ return 0;
+}
+
+static void
+chunk_unregister_root(gs_memory_t * mem, gs_gc_root_t * rp, client_name_t cname)
+{
+}
+
+#ifdef DEBUG
+
+#define A(obj, size) \
+ if ((obj = gs_alloc_bytes(cmem, size, "chunk_alloc_unit_test")) == NULL) { \
+ dprintf("chunk alloc failed\n"); \
+ return_error(gs_error_VMerror); \
+ }
+
+#define F(obj) \
+ gs_free_object(cmem, obj, "chunk_alloc_unit_test");
+
+int
+chunk_allocator_unit_test(gs_memory_t *mem)
+{
+ int code;
+ gs_memory_t *cmem;
+ byte *obj1, *obj2, *obj3, *obj4, *obj5, *obj6, *obj7, *obj8, *obj9;
+
+ if ((code = gs_memory_chunk_wrap(&cmem, mem )) < 0) {
+ dprintf1("chunk_wrap returned error code: %d\n", code);
+ return code;
+ }
+
+ /* Allocate a large object */
+ A(obj1, 80000);
+ F(obj1);
+ A(obj1, 80000);
+
+ A(obj2, 3);
+ A(obj3, 7);
+ A(obj4, 15);
+ A(obj5, 16);
+ A(obj6, 16);
+ A(obj7, 16);
+
+ F(obj2);
+ F(obj1);
+ F(obj5);
+ F(obj4);
+ F(obj6);
+ F(obj7);
+ F(obj3);
+
+ /* cleanup */
+ gs_memory_chunk_release(cmem);
+ return 0;
+}
+
+#endif /* DEBUG */
Property changes on: branches/mtrender/src/gsmchunk.c
___________________________________________________________________
Name: svn:eolstyle
+ native
Added: branches/mtrender/src/gsmchunk.h
===================================================================
--- branches/mtrender/src/gsmchunk.h 2008-04-14 02:46:20 UTC (rev 8638)
+++ branches/mtrender/src/gsmchunk.h 2008-04-14 04:53:26 UTC (rev 8639)
@@ -0,0 +1,36 @@
+/* Copyright (C) 2001-2006 Artifex Software, Inc.
+ All Rights Reserved.
+
+ This software is provided AS-IS with no warranty, either express or
+ implied.
+
+ This software is distributed under license and may not be copied, modified
+ or distributed except as expressly authorized under the terms of that
+ license. Refer to licensing information at http://www.artifex.com/
+ or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
+ San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
+*/
+
+/* $Id:$ */
+/* chunk consolidating wrapper on a base memory allocator */
+
+#define CHUNK_SIZE 65536
+
+/* ---------- Public constructors/destructors ---------- */
+
+/* Initialize a gs_memory_chunk_t */
+ /* -ve error code or 0 */
+int gs_memory_chunk_wrap(gs_memory_t **wrapped, /* chunk allocator init */
+ gs_memory_t * target ); /* base allocator */
+
+/* Release a chunk memory manager and all of the memory it held */
+void gs_memory_chunk_release(gs_memory_t *cmem);
+
+/* ---------- Accessors ------------- */
+
+/* Retrieve this allocator's target */
+gs_memory_t *gs_memory_chunk_target(const gs_memory_t *cmem);
+
+#ifdef DEBUG
+int chunk_allocator_unit_test(gs_memory_t *mem);
+#endif /* DEBUG */
Property changes on: branches/mtrender/src/gsmchunk.h
___________________________________________________________________
Name: svn:eolstyle
+ native
Modified: branches/mtrender/src/gxclthrd.c
===================================================================
--- branches/mtrender/src/gxclthrd.c 2008-04-14 02:46:20 UTC (rev 8638)
+++ branches/mtrender/src/gxclthrd.c 2008-04-14 04:53:26 UTC (rev 8639)
@@ -1,29 +1,29 @@
-/* Copyright (C) 2001-2006 Artifex Software, Inc.
- All Rights Reserved.
-
- This software is provided AS-IS with no warranty, either express or
- implied.
-
- This software is distributed under license and may not be copied, modified
- or distributed except as expressly authorized under the terms of that
- license. Refer to licensing information at http://www.artifex.com/
- or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
- San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
-*/
-
-/*$Id$ */
-/* Command list - dummy thread hook */
-#include "gx.h"
-#include "gxdevice.h"
-#include "gxclist.h"
-
-int
-clist_enable_multi_thread_render(gx_device *dev)
-{
- return -1;
-}
-
-void
-clist_teardown_render_threads(gx_device *dev)
-{
-}
+/* Copyright (C) 2001-2006 Artifex Software, Inc.
+ All Rights Reserved.
+
+ This software is provided AS-IS with no warranty, either express or
+ implied.
+
+ This software is distributed under license and may not be copied, modified
+ or distributed except as expressly authorized under the terms of that
+ license. Refer to licensing information at http://www.artifex.com/
+ or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
+ San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
+*/
+
+/*$Id$ */
+/* Command list - dummy thread hook */
+#include "gx.h"
+#include "gxdevice.h"
+#include "gxclist.h"
+
+int
+clist_enable_multi_thread_render(gx_device *dev)
+{
+ return -1;
+}
+
+void
+clist_teardown_render_threads(gx_device *dev)
+{
+}
Modified: branches/mtrender/src/gxclthrd.h
===================================================================
--- branches/mtrender/src/gxclthrd.h 2008-04-14 02:46:20 UTC (rev 8638)
+++ branches/mtrender/src/gxclthrd.h 2008-04-14 04:53:26 UTC (rev 8639)
@@ -1,42 +1,43 @@
-/* Copyright (C) 2001-2006 Artifex Software, Inc.
- All Rights Reserved.
-
- This software is provided AS-IS with no warranty, either express or
- implied.
-
- This software is distributed under license and may not be copied, modified
- or distributed except as expressly authorized under the terms of that
- license. Refer to licensing information at http://www.artifex.com/
- or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
- San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
-*/
-
-/* $Id$ */
-/* Command list multiple rendering threads */
-/* Requires gxsync.h */
-
-#ifndef gxclthrd_INCLUDED
-# define gxcthrd_INCLUDED
-
-#include "gxsync.h"
-
-#define RENDER_THREAD_IDLE 0
-#define RENDER_THREAD_DONE 1
-#define RENDER_THREAD_BUSY 2
-
-#ifndef clist_render_thread_control_t_DEFINED
-# define clist_render_thread_control_t_DEFINED
-typedef struct clist_render_thread_control_s clist_render_thread_control_t;
-#endif
-
-typedef struct clist_render_thread_control_s {
- int status; /* 0: not started, 1: done, 2: busy, < 0: error */
- /* values allow waiting until status < 2 */
- gx_semaphore_t *sema_this;
- gx_semaphore_t *sema_group;
- gx_device *cdev; /* clist device copy */
- gx_device *bdev; /* this thread's buffer device */
- int band;
-};
-
-#endif /* gxclthrd_INCLUDED */
+/* Copyright (C) 2001-2006 Artifex Software, Inc.
+ All Rights Reserved.
+
+ This software is provided AS-IS with no warranty, either express or
+ implied.
+
+ This software is distributed under license and may not be copied, modified
+ or distributed except as expressly authorized under the terms of that
+ license. Refer to licensing information at http://www.artifex.com/
+ or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
+ San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
+*/
+
+/* $Id$ */
+/* Command list multiple rendering threads */
+/* Requires gxsync.h */
+
+#ifndef gxclthrd_INCLUDED
+# define gxcthrd_INCLUDED
+
+#include "gxsync.h"
+
+#define RENDER_THREAD_IDLE 0
+#define RENDER_THREAD_DONE 1
+#define RENDER_THREAD_BUSY 2
+
+#ifndef clist_render_thread_control_t_DEFINED
+# define clist_render_thread_control_t_DEFINED
+typedef struct clist_render_thread_control_s clist_render_thread_control_t;
+#endif
+
+typedef struct clist_render_thread_control_s {
+ int status; /* 0: not started, 1: done, 2: busy, < 0: error */
+ /* values allow waiting until status < 2 */
+ gs_memory_t *memory; /* thread's 'chunk' memory allocator */
+ gx_semaphore_t *sema_this;
+ gx_semaphore_t *sema_group;
+ gx_device *cdev; /* clist device copy */
+ gx_device *bdev; /* this thread's buffer device */
+ int band;
+};
+
+#endif /* gxclthrd_INCLUDED */
Modified: branches/mtrender/src/gxclthrd1.c
===================================================================
--- branches/mtrender/src/gxclthrd1.c 2008-04-14 02:46:20 UTC (rev 8638)
+++ branches/mtrender/src/gxclthrd1.c 2008-04-14 04:53:26 UTC (rev 8639)
@@ -27,13 +27,13 @@
#include "gxgetbit.h"
#include "gdevplnx.h"
#include "gsmemory.h"
+#include "gsmchunk.h"
#include "gxclthrd.h"
/* Forward reference prototypes */
static int clist_start_render_thread(gx_device *dev, int thread_index, int band);
static void clist_render_thread(void *param);
-
/* Set up and start the render threads */
static int
clist_setup_render_threads(gx_device *dev, int y)
@@ -105,9 +105,19 @@
gx_device_clist *ncldev;
gx_device_clist_common *ncdev;
clist_render_thread_control_t *thread = &(crdev->render_threads[i]);
+ gs_memory_t *tmem; /* per thread allocator (wrapper) */
+ /* Every thread will have a 'chunk allocator' to reduce the interaction
+ * with the 'base' allocator which has 'mutex' (locking) protection.
+ * This improves performance of the threads.
+ */
+ if ((code = gs_memory_chunk_wrap(&(thread->memory), mem )) < 0) {
+ eprintf1("chunk_wrap returned error code: %d\n", code);
+ break;
+ }
+
thread->band = -1; /* a value that won't match any valid band */
- if ((code = gs_copydevice((gx_device **) &ndev, protodev, mem)) < 0) {
+ if ((code = gs_copydevice((gx_device **) &ndev, protodev, thread->memory)) < 0) {
code = 0; /* even though we failed, no cleanup needed */
break;
}
@@ -115,7 +125,7 @@
ncdev = (gx_device_clist_common *)ndev;
gx_device_fill_in_procs(ndev);
((gx_device_printer *)ncdev)->buffer_memory = ncdev->memory =
- ncdev->bandlist_memory = mem;
+ ncdev->bandlist_memory = thread->memory;
gs_c_param_list_read(¶mlist);
ndev->PageCount = dev->PageCount; /* copy to prevent mismatch error */
if ((code = gs_putdeviceparams(ndev, (gs_param_list *)¶mlist)) < 0)
@@ -133,9 +143,9 @@
cdev->page_info.io_procs->fclose(ncdev->page_bfile, ncdev->page_bfname, true);
/* open the main thread's files for this thread */
if ((code=cdev->page_info.io_procs->fopen(cdev->page_cfname, fmode, &ncdev->page_cfile,
- mem, mem, true)) < 0 ||
+ thread->memory, thread->memory, true)) < 0 ||
(code=cdev->page_info.io_procs->fopen(cdev->page_bfname, fmode, &ncdev->page_bfile,
- mem, mem, false)) < 0)
+ thread->memory, thread->memory, false)) < 0)
break;
clist_render_init(ncldev); /* Initialize clist device for reading */
ncdev->page_bfile_end_pos = cdev->page_bfile_end_pos;
@@ -144,10 +154,10 @@
if ((code = gdev_create_buf_device(cdev->buf_procs.create_buf_device,
&(thread->bdev), cdev->target,
band*crdev->page_band_height, NULL,
- mem, clist_get_band_complexity(dev,y)) < 0))
+ thread->memory, clist_get_band_complexity(dev,y)) < 0))
break;
- if ((thread->sema_this = gx_semaphore_alloc(mem)) == NULL ||
- (thread->sema_group = gx_semaphore_alloc(mem)) == NULL) {
+ if ((thread->sema_this = gx_semaphore_alloc(thread->memory)) == NULL ||
+ (thread->sema_group = gx_semaphore_alloc(thread->memory)) == NULL) {
code = gs_error_VMerror;
break;
}
@@ -165,13 +175,18 @@
cdev->buf_procs.destroy_buf_device(crdev->render_threads[i].bdev);
if (crdev->render_threads[i].cdev != NULL) {
gdev_prn_free_memory((gx_device *)(crdev->render_threads[i].cdev));
- gs_free_object(mem, crdev->render_threads[i].cdev, "clist_setup_render_threads");
+ gs_free_object(crdev->render_threads[i].memory, crdev->render_threads[i].cdev,
+ "clist_setup_render_threads");
}
+ if (crdev->render_threads[i].memory != NULL)
+ gs_memory_chunk_release(crdev->render_threads[i].memory);
}
/* If we weren't able to create at least one thread, punt */
/* Although a single thread isn't any more efficient, the */
/* machinery still works, so that's OK. */
if (i == 0) {
+ if (crdev->render_threads[0].memory != NULL)
+ gs_memory_chunk_release(crdev->render_threads[0].memory);
gs_free_object(mem, crdev->render_threads, "clist_setup_render_threads");
crdev->render_threads = NULL;
pdev->num_render_threads_requested = 0; /* shut down thread support */
@@ -227,7 +242,8 @@
*/
gdev_prn_free_memory(thread->cdev);
/* Free the device copy this thread used */
- gs_free_object(mem, thread->cdev, "clist_teardown_render_threads");
+ gs_free_object(thread->memory, thread->cdev, "clist_teardown_render_threads");
+ gs_memory_chunk_release(thread->memory);
}
cdev->data = crdev->main_thread_data; /* restore the pointer for writing */
gs_free_object(mem, crdev->render_threads, "clist_teardown_render_threads");
@@ -252,11 +268,12 @@
{
gx_device_clist *cldev = (gx_device_clist *)dev;
gx_device_clist_reader *crdev = &cldev->reader;
+ gx_device_clist_common *cdev = (gx_device_clist_common *)dev;
int code;
crdev->render_threads[thread_index].band = band;
crdev->render_threads[thread_index].status = RENDER_THREAD_BUSY;
-
+
/* Finally, fire it up */
code = gp_create_thread(clist_render_thread, &(crdev->render_threads[thread_index]));
Modified: branches/mtrender/src/lib.mak
===================================================================
--- branches/mtrender/src/lib.mak 2008-04-14 02:46:20 UTC (rev 8638)
+++ branches/mtrender/src/lib.mak 2008-04-14 04:53:26 UTC (rev 8639)
@@ -1772,13 +1772,15 @@
$(GLCC) $(GLO_)gxclthrd.$(OBJ) $(C_) $(GLSRC)gxclthrd.c
# Support for multiple clist rendering threads.
-$(GLD)clthread1.dev: $(GLOBJ)gxclthrd1.$(OBJ) $(GLD)$(SYNC).dev
- $(SETMOD) $(GLD)clthread1 $(GLOBJ)gxclthrd1.$(OBJ)
+$(GLD)clthread1.dev: $(GLOBJ)gxclthrd1.$(OBJ) $(GLOBJ)gsmchunk.$(OBJ) $(GLD)$(SYNC).dev
+ $(SETMOD) $(GLD)clthread1 $(GLOBJ)gxclthrd1.$(OBJ) $(GLOBJ)gsmchunk.$(OBJ)
$(ADDMOD) $(GLD)clthread1 -include $(GLD)$(SYNC).dev
$(GLOBJ)gxclthrd1.$(OBJ) : $(GLSRC)gxclthrd1.c $(gxclist_h) $(gxsync_h) $(gxclthrd_h)
$(GLCC) $(GLO_)gxclthrd1.$(OBJ) $(C_) $(GLSRC)gxclthrd1.c
+$(GLOBJ)gsmchunk.$(OBJ) : $(GLSRC)gsmchunk.c $(gx_h) $(gsstype_h) $(gserrors_h)
+ $(GLCC) $(GLO_)gsmchunk.$(OBJ) $(C_) $(GLSRC)gsmchunk.c
# ---------------- Vector devices ---------------- #
# We include this here for the same reasons as page.dev.
More information about the gs-cvs
mailing list