1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2001 Aladdin Enterprises. All rights reserved.
2
3 This software is provided AS-IS with no warranty, either express or
4 implied.
5
6 This software is distributed under license and may not be copied,
7 modified or distributed except as expressly authorized under the terms
8 of the license contained in the file LICENSE in this distribution.
9
10 For more information about licensing, please refer to
11 http://www.ghostscript.com/licensing/. For information on
12 commercial licensing, go to http://www.artifex.com/licensing/ or
13 contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 San Rafael, CA 94903, U.S.A., +1(415)492-9861.
15 */
16
17 /* $Id: iplugin.c,v 1.5 2004/08/04 19:36:13 stefan Exp $ */
18 /* Plugin manager */
19
20 #include "malloc_.h"
21 #include "string_.h"
22 #include "ghost.h"
23 #include "gxalloc.h"
24 #include "ierrors.h"
25 #include "ialloc.h"
26 #include "iplugin.h"
27 #include "icstate.h"
28
29 /* Plugin list is being build in raw memory pool,
30 because it is irrelevant to PS virtual memory.
31 At least it must live during alloc_restore_all, allowing
32 plugins to handle the finalization of objects they manage.
33 */
34
35 extern_i_plugin_table();
36
i_plugin_mem_alloc(i_plugin_client_memory * mem,unsigned int nbytes,const char * cname)37 private void *i_plugin_mem_alloc(i_plugin_client_memory *mem, unsigned int nbytes, const char *cname)
38 { gs_memory_t *mem_raw = mem->client_data;
39 return mem_raw->procs.alloc_bytes_immovable(mem_raw, nbytes, cname);
40 }
41
i_plugin_mem_free(i_plugin_client_memory * mem,void * data,const char * cname)42 private void i_plugin_mem_free(i_plugin_client_memory *mem, void *data, const char *cname)
43 { gs_memory_t *mem_raw = mem->client_data;
44 mem_raw->procs.free_object(mem_raw, data, cname);
45 }
46
i_plugin_make_memory(i_plugin_client_memory * mem,gs_memory_t * mem_raw)47 void i_plugin_make_memory(i_plugin_client_memory *mem, gs_memory_t *mem_raw)
48 { mem->client_data = mem_raw;
49 mem->alloc = i_plugin_mem_alloc;
50 mem->free = i_plugin_mem_free;
51 }
52
i_plugin_init(i_ctx_t * i_ctx_p)53 int i_plugin_init(i_ctx_t *i_ctx_p)
54 { gs_memory_t *mem_raw = i_ctx_p->memory.current->non_gc_memory;
55 const i_plugin_instantiation_proc *p = i_plugin_table;
56 i_plugin_holder *h;
57 int code;
58 i_plugin_client_memory client_mem;
59 i_plugin_make_memory(&client_mem, mem_raw);
60 for (; *p != 0; p++) {
61 i_plugin_instance *instance = 0;
62 code = (*p)(i_ctx_p, &client_mem, &instance);
63 if (code != 0)
64 return code;
65 h = (i_plugin_holder *)gs_alloc_bytes_immovable(mem_raw, sizeof(i_plugin_holder), "plugin_holder");
66 if (h == 0)
67 return_error(e_Fatal);
68 h->I = instance;
69 h->next = i_ctx_p->plugin_list;
70 i_ctx_p->plugin_list = h;
71 }
72 return 0;
73 }
74
i_plugin_finit(gs_memory_t * mem_raw,i_plugin_holder * list)75 void i_plugin_finit(gs_memory_t *mem_raw, i_plugin_holder *list)
76 { i_plugin_client_memory client_mem;
77 i_plugin_make_memory(&client_mem, mem_raw);
78 while (list != 0) {
79 i_plugin_holder *h = list;
80 list = h->next;
81 h->I->d->finit(h->I, &client_mem);
82 gs_free_object(mem_raw, h, "plugin_holder");
83 }
84 }
85
i_plugin_get_list(i_ctx_t * i_ctx_p)86 i_plugin_holder * i_plugin_get_list(i_ctx_t *i_ctx_p)
87 { return i_ctx_p->plugin_list;
88 }
89
i_plugin_find(i_ctx_t * i_ctx_p,const char * type,const char * subtype)90 i_plugin_instance *i_plugin_find(i_ctx_t *i_ctx_p, const char *type, const char *subtype)
91 { i_plugin_holder *h = i_ctx_p->plugin_list;
92 for (; h != 0; h = h->next) {
93 i_plugin_instance *I = h->I;
94 if (!strcmp(I->d->type, type) && !strcmp(I->d->subtype, subtype))
95 return I;
96 }
97 return NULL;
98 }
99
100 /* todo: define plugin enumerator by 'type' */
101