1 /* internal.h -- common header file for the internal CORE and SHELL APIs.
2 Copyright (C) 2003, 2004
3 National Institute of Advanced Industrial Science and Technology (AIST)
4 Registration Number H15PRO112
6 This file is part of the m17n library.
8 The m17n library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
13 The m17n library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the m17n library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #ifndef _M17N_INTERNAL_H_
24 #define _M17N_INTERNAL_H_
27 @brief a documentation for internal.h
29 longer version of internal.h description
32 extern int m17n__core_initialized;
33 extern int m17n__shell_initialized;
34 extern int m17n__gui_initialized;
36 extern int mdebug_hook ();
38 /** Return with code RET while setting merror_code to ERR. */
40 #define MERROR(err, ret) \
42 merror_code = (err); \
48 #define MERROR_GOTO(err, label) \
51 merror_code = (err); \
57 #define MWARNING(err) \
69 #define M_CHECK_CHAR(c, ret) \
70 if ((c) < 0 || (c) > MCHAR_MAX) \
71 MERROR (MERROR_CHAR, (ret)); \
75 /** Memory allocation stuffs. */
77 /* Call a handler function for memory full situation with argument
78 ERR. ERR must be one of enum MErrorCode. By default, the
79 handler function just calls exit () with argument ERR. */
81 #define MEMORY_FULL(err) \
83 (*m17n_memory_full_handler) (err); \
88 /** The macro MTABLE_MALLOC () allocates memory (by malloc) for an
89 array of SIZE objects. The size of each object is determined by
90 the type of P. Then, it sets P to the allocated memory. ERR must
91 be one of enum MErrorCode. If the allocation fails, the macro
92 MEMORY_FULL () is called with argument ERR. */
94 #define MTABLE_MALLOC(p, size, err) \
96 if (! ((p) = (void *) malloc (sizeof (*(p)) * (size)))) \
101 /** The macro MTABLE_CALLOC() is like the macro MTABLE_MALLOC but use
102 calloc instead of malloc, thus the allocated memory are zero
105 #define MTABLE_CALLOC(p, size, err) \
107 if (! ((p) = (void *) calloc (sizeof (*(p)), size))) \
112 /** The macro MTABLE_REALLOC () changes the size of memory block
113 pointed to by P to a size suitable for an array of SIZE objects.
114 The size of each object is determined by the type of P. ERR must
115 be one of enum MErrorCode. If the allocation fails, the macro
116 MEMORY_FULL () is called with argument ERR. */
118 #define MTABLE_REALLOC(p, size, err) \
120 if (! ((p) = (void *) realloc ((p), sizeof (*(p)) * (size)))) \
125 /** The macro MTABLE_ALLOCA () allocates memory (by alloca) for an
126 array of SIZE objects. The size of each object is determined by
127 the type of P. Then, it sets P to the allocated memory. ERR must
128 be one of enum MErrorCode. If the allocation fails, the macro
129 MEMORY_FULL () is called with argument ERR. */
131 #define MTABLE_ALLOCA(p, size, err) \
133 int allocasize = sizeof (*(p)) * (size); \
134 if (! ((p) = (void *) alloca (allocasize))) \
136 memset ((p), 0, allocasize); \
140 /** short description of MSTRUCT_MALLOC */
141 /** The macro MSTRUCT_MALLOC () allocates memory (by malloc) for an
142 object whose size is determined by the type of P, and sets P to
143 the allocated memory. ERR must be one of enum MErrorCode. If
144 the allocation fails, the macro MEMORY_FULL () is called with
147 #define MSTRUCT_MALLOC(p, err) \
149 if (! ((p) = (void *) malloc (sizeof (*(p))))) \
154 #define MSTRUCT_CALLOC(p, err) MTABLE_CALLOC ((p), 1, (err))
156 #define USE_SAFE_ALLOCA \
157 int sa_must_free = 0, sa_size = 0
159 /* P must be the same in all calls to SAFE_ALLOCA and SAFE_FREE in a
162 #define SAFE_ALLOCA(P, SIZE) \
164 if (sa_size < (SIZE)) \
167 (P) = realloc ((P), (SIZE)); \
170 (P) = alloca ((SIZE)); \
173 (P) = malloc (SIZE); \
183 #define SAFE_FREE(P) \
185 if (sa_must_free && sa_size > 0) \
188 sa_must_free = sa_size = 0; \
193 /** Extendable array. */
195 #define MLIST_RESET(list) \
199 #define MLIST_INIT1(list, mem, increment) \
201 (list)->size = (list)->used = 0; \
202 (list)->inc = (increment); \
203 (list)->mem = NULL; \
207 #define MLIST_APPEND1(list, mem, elt, err) \
209 if ((list)->inc <= 0) \
211 if ((list)->size == (list)->used) \
213 (list)->size += (list)->inc; \
214 MTABLE_REALLOC ((list)->mem, (list)->size, (err)); \
216 (list)->mem[(list)->used++] = (elt); \
220 #define MLIST_PREPEND1(list, mem, elt, err) \
222 if ((list)->inc <= 0) \
224 if ((list)->size == (list)->used) \
226 (list)->size += (list)->inc; \
227 MTABLE_REALLOC ((list)->mem, (list)->size, (err)); \
229 memmove ((list)->mem + 1, (list)->mem, \
230 sizeof *((list)->mem) * ((list)->used)); \
231 (list)->mem[0] = (elt); \
236 #define MLIST_INSERT1(list, mem, idx, len, err) \
238 while ((list)->used + (len) > (list)->size) \
240 (list)->size += (list)->inc; \
241 MTABLE_REALLOC ((list)->mem, (list)->size, (err)); \
243 memmove ((list)->mem + ((idx) + (len)), (list)->mem + (idx), \
244 (sizeof *((list)->mem)) * ((list)->used - (idx))); \
245 (list)->used += (len); \
249 #define MLIST_DELETE1(list, mem, idx, len) \
251 memmove ((list)->mem + (idx), (list)->mem + (idx) + (len), \
252 (sizeof *((list)->mem)) * ((list)->used - (idx) - (len))); \
253 (list)->used -= (len); \
257 #define MLIST_COPY1(list0, list1, mem, err) \
259 (list0)->size = (list0)->used = (list1)->used; \
261 MTABLE_MALLOC ((list0)->mem, (list0)->used, (err)); \
262 memcpy ((list0)->mem, (list1)->mem, \
263 (sizeof (list0)->mem) * (list0)->used); \
267 #define MLIST_FREE1(list, mem) \
270 free ((list)->mem); \
271 (list)->mem = NULL; \
272 (list)->size = (list)->used = 0; \
280 void (*freer) (void *);
287 /**en Reference count of the object. */
288 /**ja ¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô. */
289 unsigned ref_count : 16;
291 unsigned ref_count_extended : 1;
293 /**en A flag bit used for various perpose. */
294 /**ja ¤µ¤Þ¤¶¤Þ¤ÊÌÜŪ¤ËÍѤ¤¤é¤ì¤ë¥Õ¥é¥°¥Ó¥Ã¥È. */
298 /**en If <ref_count_extended> is zero, a function to free the
300 /**ja <ref_count_extended> ¤¬ 0 ¤Ê¤é¤Ð¥ª¥Ö¥¸¥§¥¯¥È¤ò²òÊü¤¹¤ë´Ø¿ô. */
301 void (*freer) (void *);
302 /**en If <ref_count_extended> is nonzero, a pointer to the
303 struct M17NObjectRecord. */
304 /**ja <ref_count_extended> ¤¬ 0 ¤Ç¤Ê¤±¤ì¤Ð¹½Â¤ÂÎ M17NObjectRecord ¤Ø¤Î¥Ý¥¤¥ó¥¿. */
305 M17NObjectRecord *record;
310 /** Allocate a managed object OBJECT which has freer FREE_FUNC. */
312 #define M17N_OBJECT(object, free_func, err) \
314 MSTRUCT_CALLOC ((object), (err)); \
315 ((M17NObject *) (object))->ref_count = 1; \
316 ((M17NObject *) (object))->u.freer = free_func; \
320 /**en Increment the reference count of OBJECT if the count is not
322 /**ja OBJECT ¤Î»²¾È¿ô¤¬ 0 ¤Ç¤Ê¤±¤ì¤Ð 1 Áý¤ä¤¹. */
324 #define M17N_OBJECT_REF(object) \
326 if (((M17NObject *) (object))->ref_count_extended) \
327 m17n_object_ref (object); \
328 else if (((M17NObject *) (object))->ref_count > 0) \
330 ((M17NObject *) (object))->ref_count++; \
331 if (! ((M17NObject *) (object))->ref_count) \
333 ((M17NObject *) (object))->ref_count--; \
334 m17n_object_ref (object); \
340 #define M17N_OBJECT_REF_NTIMES(object, n) \
344 if (((M17NObject *) (object))->ref_count_extended) \
345 for (i = 0; i < n; i++) \
346 m17n_object_ref (object); \
347 else if (((M17NObject *) (object))->ref_count > 0) \
349 int orig_ref_count = ((M17NObject *) (object))->ref_count; \
351 for (i = 0; i < n; i++) \
352 if (! ++((M17NObject *) (object))->ref_count) \
354 ((M17NObject *) (object))->ref_count = orig_ref_count; \
355 for (i = 0; i < n; i++) \
356 m17n_object_ref (object); \
362 /**en Decrement the reference count of OBJECT if the count is greater
363 than 0. In that case, if the count becomes 0, free OBJECT. */
364 /**ja OBJECT ¤Î»²¾È¿ô¤¬ 0 ¤è¤êÂ礤±¤ì¤Ð 1 ¸º¤é¤¹¡£¸º¤é¤·¤Æ 0 ¤Ë¤Ê¤ì¤Ð
365 OBJECT ¤ò²òÊü¤¹¤ë. */
367 #define M17N_OBJECT_UNREF(object) \
371 if (((M17NObject *) (object))->ref_count_extended) \
372 m17n_object_unref (object); \
373 else if (((M17NObject *) (object))->ref_count == 0) \
377 ((M17NObject *) (object))->ref_count--; \
378 if (((M17NObject *) (object))->ref_count == 0) \
380 if (((M17NObject *) (object))->u.freer) \
381 (((M17NObject *) (object))->u.freer) (object); \
390 typedef struct _M17NObjectArray M17NObjectArray;
392 struct _M17NObjectArray
398 M17NObjectArray *next;
401 extern void mdebug__add_object_array (M17NObjectArray *array, char *name);
403 #define M17N_OBJECT_ADD_ARRAY(array, name) \
404 if (mdebug__flag & MDEBUG_FINI) \
405 mdebug__add_object_array (&array, name); \
408 extern void mdebug__register_object (M17NObjectArray *array, void *object);
410 #define M17N_OBJECT_REGISTER(array, object) \
411 if (mdebug__flag & MDEBUG_FINI) \
412 mdebug__register_object (&array, object); \
415 extern void mdebug__unregister_object (M17NObjectArray *array, void *object);
417 #define M17N_OBJECT_UNREGISTER(array, object) \
418 if (mdebug__flag & MDEBUG_FINI) \
419 mdebug__unregister_object (&array, object); \
428 MTEXT_COVERAGE_ASCII,
429 MTEXT_COVERAGE_UNICODE,
437 enum MTextFormat format : 16;
438 enum MTextCoverage coverage : 16;
440 /**en Number of characters in the M-text */
441 /**ja M-text Ãæ¤Îʸ»ú¿ô */
444 /**en Number of bytes used to represent the characters in the M-text. */
445 /**ja M-text Ãæ¤Îʸ»ú¤òɽ¤ï¤¹¤¿¤á¤ËÍѤ¤¤é¤ì¤ë¥Ð¥¤¥È¿ô */
448 /**en Character sequence of the M-text. */
449 /**ja M-text Ãæ¤Îʸ»úÎó */
452 /**en Number of bytes allocated for the @c data member. */
453 /**ja ¥á¥ó¥Ð @c data ¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¥Ð¥¤¥È¿ô */
456 /**en Pointer to the property list of the M-text. */
457 /**ja M-text ¤Î¥×¥í¥Ñ¥Æ¥£¥ê¥¹¥È¤Ø¤Î¥Ý¥¤¥ó¥¿ */
458 struct MTextPlist *plist;
460 /**en Caches of the character position and the corresponding byte position. */
461 /**ja ʸ»ú°ÌÃÖ¤ª¤è¤ÓÂбþ¤¹¤ë¥Ð¥¤¥È°ÌÃ֤Υ¥ã¥Ã¥·¥å */
462 int cache_char_pos, cache_byte_pos;
465 /** short description of M_CHECK_POS */
466 /** longer description of M_CHECK_POS */
468 #define M_CHECK_POS(mt, pos, ret) \
470 if ((pos) < 0 || (pos) >= (mt)->nchars) \
471 MERROR (MERROR_RANGE, (ret)); \
475 /** short description of M_CHECK_POS_X */
476 /** longer description of M_CHECK_POS_X */
478 #define M_CHECK_POS_X(mt, pos, ret) \
480 if ((pos) < 0 || (pos) > (mt)->nchars) \
481 MERROR (MERROR_RANGE, (ret)); \
485 /** short description of M_CHECK_RANGE */
486 /** longer description of M_CHECK_RANGE */
488 #define M_CHECK_RANGE(mt, from, to, ret, ret2) \
490 if ((from) < 0 || (to) < (from) || (to) > (mt)->nchars) \
491 MERROR (MERROR_RANGE, (ret)); \
492 if ((from) == (to)) \
496 #define M_CHECK_RANGE_X(mt, from, to, ret) \
498 if ((from) < 0 || (to) < (from) || (to) > (mt)->nchars) \
499 MERROR (MERROR_RANGE, (ret)); \
503 #define M_CHECK_POS_NCHARS(mt, pos, nchars, ret, ret2) \
505 int to = (pos) + (nchars); \
507 M_CHECK_RANGE ((mt), (pos), (to), (ret), (ret2)); \
511 #define MTEXT_READ_ONLY_P(mt) ((mt)->allocated < 0)
513 #define M_CHECK_READONLY(mt, ret) \
515 if ((mt)->allocated < 0) \
516 MERROR (MERROR_MTEXT, (ret)); \
519 #define mtext_nchars(mt) ((mt)->nchars)
521 #define mtext_nbytes(mt) ((mt)->nbytes)
523 #define mtext_allocated(mt) ((mt)->allocated)
525 #define mtext_reset(mt) (mtext_del ((mt), 0, (mt)->nchars))
533 MDEBUG_CHARSET = 0x04,
534 MDEBUG_CODING = 0x08,
535 MDEBUG_DATABASE = 0x10,
536 MDEBUG_FONT = 0x0100,
537 MDEBUG_FONT_FLT = 0x0200,
538 MDEBUG_FONT_OTF = 0x0400,
539 MDEBUG_INPUT = 0x0800,
543 extern int mdebug__flag;
544 extern void mdebug__push_time ();
545 extern void mdebug__pop_time ();
546 extern void mdebug__print_time ();
548 #define MDEBUG_PRINT(msg) \
550 if (mdebug__flag & mdebug_mask) \
551 fprintf (stderr, (msg)); \
554 #define MDEBUG_PRINT1(fmt, arg) \
556 if (mdebug__flag & mdebug_mask) \
557 fprintf (stderr, (fmt), (arg)); \
560 #define MDEBUG_PRINT2(fmt, arg1, arg2) \
562 if (mdebug__flag & mdebug_mask) \
563 fprintf (stderr, (fmt), (arg1), (arg2)); \
566 #define MDEBUG_PRINT3(fmt, arg1, arg2, arg3) \
568 if (mdebug__flag & mdebug_mask) \
569 fprintf (stderr, (fmt), (arg1), (arg2), (arg3)); \
572 #define MDEBUG_PRINT4(fmt, arg1, arg2, arg3, arg4) \
574 if (mdebug__flag & mdebug_mask) \
575 fprintf (stderr, (fmt), (arg1), (arg2), (arg3), (arg4)); \
578 #define MDEBUG_PRINT5(fmt, arg1, arg2, arg3, arg4, arg5) \
580 if (mdebug__flag & mdebug_mask) \
581 fprintf (stderr, (fmt), (arg1), (arg2), (arg3), (arg4), (arg5)); \
585 #define MDEBUG_PUSH_TIME() \
587 if (mdebug__flag & mdebug_mask) \
588 mdebug__push_time (); \
592 #define MDEBUG_POP_TIME() \
594 if (mdebug__flag & mdebug_mask) \
595 mdebug__pop_time (); \
599 #define MDEBUG_PRINT_TIME(tag, ARG_LIST) \
601 if (mdebug__flag & mdebug_mask) \
603 fprintf (stderr, " [%s] ", tag); \
604 mdebug__print_time (); \
606 fprintf (stderr, "\n"); \
611 #define SWAP_16(c) (((c) >> 8) | (((c) & 0xFF) << 8))
614 (((c) >> 24) | (((c) >> 8) & 0xFF00) \
615 | (((c) & 0xFF00) << 8) | (((c) & 0xFF) << 24))
618 extern void *(*mdatabase__finder) (MSymbol tag1, MSymbol tag2,
619 MSymbol tag3, MSymbol tag4);
620 extern void *(*mdatabase__loader) (void *);
622 /* Initialize/finalize function. */
624 extern int msymbol__init ();
625 extern void msymbol__fini ();
627 extern int mplist__init ();
628 extern void mplist__fini ();
630 extern int mtext__init ();
631 extern void mtext__fini ();
633 extern int mtext__prop_init ();
634 extern void mtext__prop_fini ();
636 extern int mchartable__init ();
637 extern void mchartable__fini ();
639 extern int mcharset__init ();
640 extern void mcharset__fini ();
642 extern int mcoding__init ();
643 extern void mcoding__fini ();
645 extern int mdatabase__init (void);
646 extern void mdatabase__fini (void);
648 extern int mchar__init ();
649 extern void mchar__fini ();
651 extern int mlang__init ();
652 extern void mlang__fini ();
654 extern int mlocale__init ();
655 extern void mlocale__fini ();
657 extern int minput__init ();
658 extern void minput__fini ();
660 #endif /* _M17N_INTERNAL_H_ */