Back to index

im-sdk  12.3.91
SunIMMMan.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #ifdef USE_XSUNIM_ADAPTER
00043 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include "SunIM.h"
00048 
00049 /* Only for XSunIM based engines */
00050 typedef struct _old_iml_session {
00051     struct _old_iml_session *next ;
00052     unsigned char *classname ;
00053     unsigned char *enginename ;
00054     unsigned char *locale ;
00055     int language_engine ;
00056     int session_id ;
00057     int public_status ;
00058     int initial_public_status ;
00059     int private_status ;
00060     iml_inst        *remainder ;
00061     iml_inst        *remainder2 ;
00062     iml_inst       **rrv ;
00063     iml_inst_slot_t *short_term_slot;
00064     iml_inst_slot_t *long_term_slot;
00065 } old_iml_session;
00066 
00067 Private void *slot_manager(int, slot_op_t, register old_iml_session *, int);
00068 
00069 Public void
00070 xsunim_slot_manager_init(){
00071     /* xsunim based leif can't find _iml_new() */
00072 }
00073 
00074 /*
00075  * IMLogic_session link terminal
00076  */
00077 
00078 Private char *
00079 im_memalign(
00080     int align,
00081     int size
00082 )
00083 {
00084     char *p;
00085     
00086     p = (char *) malloc(size + align);
00087     p = (char *) ((((long) p + align - 1) / align) * align);
00088     return ((char *) p);
00089 }
00090 
00091 Private iml_inst_slot_t *free_slot_q1;
00092 Private iml_inst_slot_t *free_slot_q2;
00093 
00094 Public void *
00095 _iml_new(
00096     old_iml_session * s,
00097     int size
00098 )
00099 {
00100     /* to avoid memory trash bug temporary */
00101     size = (((size + SLOTMINSIZE) / SLOTMINSIZE) * SLOTMINSIZE);
00102     return (slot_manager(0, ALLOC_SLOT, s, size));
00103 }
00104 
00105 Public void *
00106 _iml_new2(
00107     old_iml_session * s,
00108     int size
00109 )
00110 {
00111     /* to avoid memory trash bug temporary */
00112     size = (((size + SLOTMINSIZE) / SLOTMINSIZE) * SLOTMINSIZE);
00113     return (slot_manager(1, ALLOC_SLOT, s, size));
00114 }
00115 
00116 Public void *
00117 _iml_delete(
00118     old_iml_session * s
00119 )
00120 {
00121     return (slot_manager(0, RELEASE_SLOT, s, 0));
00122 }
00123 
00124 Public void *
00125 _iml_delete2(
00126     old_iml_session * s
00127 )
00128 {
00129     return (slot_manager(1, RELEASE_SLOT, s, 0));
00130 }
00131 
00132 Private void *
00133 slot_manager(
00134     int luc,
00135     slot_op_t action,
00136     register old_iml_session * s,
00137     int size
00138 )
00139 {
00140     iml_inst_slot_t *free_slot;
00141     iml_inst_slot_t *new_slot, *p;
00142 #if defined(SLOW_SLOT_MANAGER)
00143     int link_depth = 0;
00144 #endif /* SLOW_SLOT_MANAGER */
00145     register iml_inst_slot_t *next_slot;
00146 
00147     if (luc) {
00148         next_slot = s->long_term_slot;
00149         free_slot = free_slot_q2;
00150     } else {
00151         next_slot = s->short_term_slot;
00152         free_slot = free_slot_q1;
00153     }
00154     
00155     switch (action) {
00156         case ALLOC_SLOT:
00157             if (free_slot) {
00158                 p = new_slot = free_slot;
00159                 while (new_slot && new_slot->size < size) {
00160                     p = new_slot;
00161                     new_slot = new_slot->next;
00162 #if defined(SLOW_SLOT_MANAGER)
00163                     link_depth++;
00164 #endif /* SLOW_SLOT_MANAGER */
00165                 }
00166                 if (!new_slot) {
00167                     goto allocate_new;
00168                 } else if (p != new_slot) {
00169                     p->next = new_slot->next;
00170                 } else {
00171                     /* p == new means got top of free_slot */
00172 #if !defined(SLOW_SLOT_MANAGER)
00173                     free_slot = free_slot->next;
00174 #else /* SLOW_SLOT_MANAGER */
00175                     if (free_slot->next) {
00176                         free_slot = free_slot->next;
00177                     } else {
00178                         free_slot = 0;
00179                     }
00180 #endif /* SLOW_SLOT_MANAGER */
00181                 }
00182                 new_slot->next = NULL;
00183             } else {
00184                 allocate_new:
00185 #if defined(SLOW_SLOT_MANAGER)
00186                 link_depth = -1;
00187 #endif /* SLOW_SLOT_MANAGER */
00188                 new_slot = (iml_inst_slot_t *) im_memalign(sizeof(iml_inst_slot_t *), sizeof(iml_inst_slot_t));
00189                 new_slot->size = size;
00190                 new_slot->ptr = im_memalign(_MAX(sizeof(int), sizeof(wchar_t)), size);
00191                 new_slot->next = 0;
00192             }
00193 #if !defined(SLOW_SLOT_MANAGER)
00194            if (luc) {
00195               new_slot->next = s->long_term_slot;
00196               s->long_term_slot = new_slot;
00197            } else {
00198               new_slot->next = s->short_term_slot;
00199               s->short_term_slot = new_slot;
00200            }
00201 #else /* SLOW_SLOT_MANAGER */
00202             new_slot->busy = BUSY;
00203             if (next_slot) {
00204                 while (next_slot->next) {
00205                     next_slot = next_slot->next;
00206                 }
00207                 next_slot->next = new_slot;
00208             } else {
00209                 if (luc) {
00210                     next_slot = s->long_term_slot = new_slot;
00211                 } else {
00212                     next_slot = s->short_term_slot = new_slot;
00213                 }
00214             }
00215 #endif /* SLOW_SLOT_MANAGER */
00216 
00217            /* need to call for XSunIM Adapter */
00218             memset(new_slot->ptr, 0, new_slot->size);
00219 
00220             if (luc) {
00221                 free_slot_q2 = free_slot;
00222             } else {
00223                 free_slot_q1 = free_slot;
00224             }
00225 
00226             return ((void *) new_slot->ptr);
00227             break;
00228         case RELEASE_SLOT:
00229             if (free_slot) {
00230                 p = free_slot;
00231                 while (p && p->next) {
00232                     p = p->next;
00233                 }
00234                 p->next = next_slot;
00235             } else {
00236                 p = free_slot = next_slot;
00237             }
00238             if (luc) {
00239                 s->long_term_slot = NULL;
00240             } else {
00241                 s->short_term_slot = NULL;
00242             }
00243 #if defined(SLOW_SLOT_MANAGER)
00244             while (p && p->next) {
00245                 p = p->next;
00246                 p->busy = FREE;
00247                 link_depth++;
00248             }
00249 #endif /* SLOW_SLOT_MANAGER */
00250             break;
00251     }
00252     if (luc) {
00253         free_slot_q2 = free_slot;
00254     } else {
00255         free_slot_q1 = free_slot;
00256     }
00257     return ((void *) 0);
00258 }
00259 
00260 #endif /* USE_XSUNIM_ADAPTER */