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., 51 Franklin Street, Fifth Floor,
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 ();
40 #define _(String) dgettext ("m17n-lib", String)
42 #define _(String) (String)
45 /** Return with code RET while setting merror_code to ERR. */
47 #define MERROR(err, ret) \
49 merror_code = (err); \
55 #define MERROR_GOTO(err, label) \
58 merror_code = (err); \
64 #define MWARNING(err) \
76 #define MFAILP(cond) ((cond) ? 0 : mdebug_hook ())
78 #define M_CHECK_CHAR(c, ret) \
79 if ((c) < 0 || (c) > MCHAR_MAX) \
80 MERROR (MERROR_CHAR, (ret)); \
84 /** Memory allocation stuffs. */
86 /* Call a handler function for memory full situation with argument
87 ERR. ERR must be one of enum MErrorCode. By default, the
88 handler function just calls exit () with argument ERR. */
90 #define MEMORY_FULL(err) \
92 (*m17n_memory_full_handler) (err); \
97 /** The macro MTABLE_MALLOC () allocates memory (by malloc) for an
98 array of SIZE objects. The size of each object is determined by
99 the type of P. Then, it sets P to the allocated memory. ERR must
100 be one of enum MErrorCode. If the allocation fails, the macro
101 MEMORY_FULL () is called with argument ERR. */
103 #define MTABLE_MALLOC(p, size, err) \
105 if (! ((p) = (void *) malloc (sizeof (*(p)) * (size)))) \
110 /** The macro MTABLE_CALLOC() is like the macro MTABLE_MALLOC but use
111 calloc instead of malloc, thus the allocated memory are zero
114 #define MTABLE_CALLOC(p, size, err) \
116 if (! ((p) = (void *) calloc (sizeof (*(p)), size))) \
121 /** The macro MTABLE_REALLOC () changes the size of memory block
122 pointed to by P to a size suitable for an array of SIZE objects.
123 The size of each object is determined by the type of P. ERR must
124 be one of enum MErrorCode. If the allocation fails, the macro
125 MEMORY_FULL () is called with argument ERR. */
127 #define MTABLE_REALLOC(p, size, err) \
129 if (! ((p) = (void *) realloc ((p), sizeof (*(p)) * (size)))) \
134 /** The macro MTABLE_ALLOCA () allocates memory (by alloca) for an
135 array of SIZE objects. The size of each object is determined by
136 the type of P. Then, it sets P to the allocated memory. ERR must
137 be one of enum MErrorCode. If the allocation fails, the macro
138 MEMORY_FULL () is called with argument ERR. */
140 #define MTABLE_ALLOCA(p, size, err) \
142 int allocasize = sizeof (*(p)) * (size); \
143 if (! ((p) = (void *) alloca (allocasize))) \
145 memset ((p), 0, allocasize); \
149 /** short description of MSTRUCT_MALLOC */
150 /** The macro MSTRUCT_MALLOC () allocates memory (by malloc) for an
151 object whose size is determined by the type of P, and sets P to
152 the allocated memory. ERR must be one of enum MErrorCode. If
153 the allocation fails, the macro MEMORY_FULL () is called with
156 #define MSTRUCT_MALLOC(p, err) \
158 if (! ((p) = (void *) malloc (sizeof (*(p))))) \
163 #define MSTRUCT_CALLOC(p, err) MTABLE_CALLOC ((p), 1, (err))
165 #define USE_SAFE_ALLOCA \
166 int sa_must_free = 0, sa_size = 0
168 /* P must be the same in all calls to SAFE_ALLOCA and SAFE_FREE in a
171 #define SAFE_ALLOCA(P, SIZE) \
173 if (sa_size < (SIZE)) \
176 (P) = realloc ((P), (SIZE)); \
179 (P) = alloca ((SIZE)); \
182 (P) = malloc (SIZE); \
192 #define SAFE_FREE(P) \
194 if (sa_must_free && sa_size > 0) \
197 sa_must_free = sa_size = 0; \
202 /** Extendable array. */
204 #define MLIST_RESET(list) \
208 #define MLIST_INIT1(list, mem, increment) \
210 (list)->size = (list)->used = 0; \
211 (list)->inc = (increment); \
212 (list)->mem = NULL; \
216 #define MLIST_APPEND1(list, mem, elt, err) \
218 if ((list)->inc <= 0) \
220 if ((list)->size == (list)->used) \
222 (list)->size += (list)->inc; \
223 MTABLE_REALLOC ((list)->mem, (list)->size, (err)); \
225 (list)->mem[(list)->used++] = (elt); \
229 #define MLIST_PREPEND1(list, mem, elt, err) \
231 if ((list)->inc <= 0) \
233 if ((list)->size == (list)->used) \
235 (list)->size += (list)->inc; \
236 MTABLE_REALLOC ((list)->mem, (list)->size, (err)); \
238 memmove ((list)->mem + 1, (list)->mem, \
239 sizeof *((list)->mem) * ((list)->used)); \
240 (list)->mem[0] = (elt); \
245 #define MLIST_INSERT1(list, mem, idx, len, err) \
247 while ((list)->used + (len) > (list)->size) \
249 (list)->size += (list)->inc; \
250 MTABLE_REALLOC ((list)->mem, (list)->size, (err)); \
252 memmove ((list)->mem + ((idx) + (len)), (list)->mem + (idx), \
253 (sizeof *((list)->mem)) * ((list)->used - (idx))); \
254 (list)->used += (len); \
258 #define MLIST_DELETE1(list, mem, idx, len) \
260 memmove ((list)->mem + (idx), (list)->mem + (idx) + (len), \
261 (sizeof *((list)->mem)) * ((list)->used - (idx) - (len))); \
262 (list)->used -= (len); \
266 #define MLIST_COPY1(list0, list1, mem, err) \
268 (list0)->size = (list0)->used = (list1)->used; \
270 MTABLE_MALLOC ((list0)->mem, (list0)->used, (err)); \
271 memcpy ((list0)->mem, (list1)->mem, \
272 (sizeof (list0)->mem) * (list0)->used); \
276 #define MLIST_FREE1(list, mem) \
279 free ((list)->mem); \
280 (list)->mem = NULL; \
281 (list)->size = (list)->used = 0; \
289 void (*freer) (void *);
296 /**en Reference count of the object. */
297 /**ja ¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô. */
298 unsigned ref_count : 16;
300 unsigned ref_count_extended : 1;
302 /**en A flag bit used for various perpose. */
303 /**ja ¤µ¤Þ¤¶¤Þ¤ÊÌÜŪ¤ËÍѤ¤¤é¤ì¤ë¥Õ¥é¥°¥Ó¥Ã¥È. */
307 /**en If <ref_count_extended> is zero, a function to free the
309 /**ja <ref_count_extended> ¤¬ 0 ¤Ê¤é¤Ð¥ª¥Ö¥¸¥§¥¯¥È¤ò²òÊü¤¹¤ë´Ø¿ô. */
310 void (*freer) (void *);
311 /**en If <ref_count_extended> is nonzero, a pointer to the
312 struct M17NObjectRecord. */
313 /**ja <ref_count_extended> ¤¬ 0 ¤Ç¤Ê¤±¤ì¤Ð¹½Â¤ÂÎ M17NObjectRecord ¤Ø¤Î¥Ý¥¤¥ó¥¿. */
314 M17NObjectRecord *record;
319 /** Allocate a managed object OBJECT which has freer FREE_FUNC. */
321 #define M17N_OBJECT(object, free_func, err) \
323 MSTRUCT_CALLOC ((object), (err)); \
324 ((M17NObject *) (object))->ref_count = 1; \
325 ((M17NObject *) (object))->u.freer = free_func; \
329 /**en Increment the reference count of OBJECT if the count is not
331 /**ja OBJECT ¤Î»²¾È¿ô¤¬ 0 ¤Ç¤Ê¤±¤ì¤Ð 1 Áý¤ä¤¹. */
333 #define M17N_OBJECT_REF(object) \
335 if (((M17NObject *) (object))->ref_count_extended) \
336 m17n_object_ref (object); \
337 else if (((M17NObject *) (object))->ref_count > 0) \
339 ((M17NObject *) (object))->ref_count++; \
340 if (! ((M17NObject *) (object))->ref_count) \
342 ((M17NObject *) (object))->ref_count--; \
343 m17n_object_ref (object); \
349 #define M17N_OBJECT_REF_NTIMES(object, n) \
353 if (((M17NObject *) (object))->ref_count_extended) \
354 for (i = 0; i < n; i++) \
355 m17n_object_ref (object); \
356 else if (((M17NObject *) (object))->ref_count > 0) \
358 int orig_ref_count = ((M17NObject *) (object))->ref_count; \
360 for (i = 0; i < n; i++) \
361 if (! ++((M17NObject *) (object))->ref_count) \
363 ((M17NObject *) (object))->ref_count = orig_ref_count; \
364 for (i = 0; i < n; i++) \
365 m17n_object_ref (object); \
371 /**en Decrement the reference count of OBJECT if the count is greater
372 than 0. In that case, if the count becomes 0, free OBJECT. */
373 /**ja OBJECT ¤Î»²¾È¿ô¤¬ 0 ¤è¤êÂ礤±¤ì¤Ð 1 ¸º¤é¤¹¡£¸º¤é¤·¤Æ 0 ¤Ë¤Ê¤ì¤Ð
374 OBJECT ¤ò²òÊü¤¹¤ë. */
376 #define M17N_OBJECT_UNREF(object) \
380 if (((M17NObject *) (object))->ref_count_extended) \
381 m17n_object_unref (object); \
382 else if (((M17NObject *) (object))->ref_count == 0) \
386 ((M17NObject *) (object))->ref_count--; \
387 if (((M17NObject *) (object))->ref_count == 0) \
389 if (((M17NObject *) (object))->u.freer) \
390 (((M17NObject *) (object))->u.freer) (object); \
399 typedef struct _M17NObjectArray M17NObjectArray;
401 struct _M17NObjectArray
407 M17NObjectArray *next;
410 extern void mdebug__add_object_array (M17NObjectArray *array, char *name);
412 #define M17N_OBJECT_ADD_ARRAY(array, name) \
413 if (mdebug__flag & MDEBUG_FINI) \
414 mdebug__add_object_array (&array, name); \
417 extern void mdebug__register_object (M17NObjectArray *array, void *object);
419 #define M17N_OBJECT_REGISTER(array, object) \
420 if (mdebug__flag & MDEBUG_FINI) \
421 mdebug__register_object (&array, object); \
424 extern void mdebug__unregister_object (M17NObjectArray *array, void *object);
426 #define M17N_OBJECT_UNREGISTER(array, object) \
427 if (mdebug__flag & MDEBUG_FINI) \
428 mdebug__unregister_object (&array, object); \
437 MTEXT_COVERAGE_ASCII,
438 MTEXT_COVERAGE_UNICODE,
446 unsigned format : 16;
447 unsigned coverage : 16;
449 /**en Number of characters in the M-text */
450 /**ja M-text Ãæ¤Îʸ»ú¿ô */
453 /**en Number of bytes used to represent the characters in the M-text. */
454 /**ja M-text Ãæ¤Îʸ»ú¤òɽ¤ï¤¹¤¿¤á¤ËÍѤ¤¤é¤ì¤ë¥Ð¥¤¥È¿ô */
457 /**en Character sequence of the M-text. */
458 /**ja M-text Ãæ¤Îʸ»úÎó */
461 /**en Number of bytes allocated for the @c data member. */
462 /**ja ¥á¥ó¥Ð @c data ¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¥Ð¥¤¥È¿ô */
465 /**en Pointer to the property list of the M-text. */
466 /**ja M-text ¤Î¥×¥í¥Ñ¥Æ¥£¥ê¥¹¥È¤Ø¤Î¥Ý¥¤¥ó¥¿ */
467 struct MTextPlist *plist;
469 /**en Caches of the character position and the corresponding byte position. */
470 /**ja ʸ»ú°ÌÃÖ¤ª¤è¤ÓÂбþ¤¹¤ë¥Ð¥¤¥È°ÌÃ֤Υ¥ã¥Ã¥·¥å */
471 int cache_char_pos, cache_byte_pos;
474 /** short description of M_CHECK_POS */
475 /** longer description of M_CHECK_POS */
477 #define M_CHECK_POS(mt, pos, ret) \
479 if ((pos) < 0 || (pos) >= (mt)->nchars) \
480 MERROR (MERROR_RANGE, (ret)); \
484 /** short description of M_CHECK_POS_X */
485 /** longer description of M_CHECK_POS_X */
487 #define M_CHECK_POS_X(mt, pos, ret) \
489 if ((pos) < 0 || (pos) > (mt)->nchars) \
490 MERROR (MERROR_RANGE, (ret)); \
494 /** short description of M_CHECK_RANGE */
495 /** longer description of M_CHECK_RANGE */
497 #define M_CHECK_RANGE(mt, from, to, ret, ret2) \
499 if ((from) < 0 || (to) < (from) || (to) > (mt)->nchars) \
500 MERROR (MERROR_RANGE, (ret)); \
501 if ((from) == (to)) \
505 #define M_CHECK_RANGE_X(mt, from, to, ret) \
507 if ((from) < 0 || (to) < (from) || (to) > (mt)->nchars) \
508 MERROR (MERROR_RANGE, (ret)); \
512 #define M_CHECK_POS_NCHARS(mt, pos, nchars, ret, ret2) \
514 int to = (pos) + (nchars); \
516 M_CHECK_RANGE ((mt), (pos), (to), (ret), (ret2)); \
520 #define MTEXT_READ_ONLY_P(mt) ((mt)->allocated < 0)
522 #define M_CHECK_READONLY(mt, ret) \
524 if ((mt)->allocated < 0) \
525 MERROR (MERROR_MTEXT, (ret)); \
528 #define mtext_nchars(mt) ((mt)->nchars)
530 #define mtext_nbytes(mt) ((mt)->nbytes)
532 #define mtext_allocated(mt) ((mt)->allocated)
534 #define mtext_reset(mt) (mtext_del ((mt), 0, (mt)->nchars))
542 MDEBUG_CHARSET = 0x04,
543 MDEBUG_CODING = 0x08,
544 MDEBUG_DATABASE = 0x10,
545 MDEBUG_FONT = 0x0100,
546 MDEBUG_FONT_FLT = 0x0200,
547 MDEBUG_FONT_OTF = 0x0400,
548 MDEBUG_INPUT = 0x0800,
553 extern int mdebug__flag;
554 extern FILE *mdebug__output;
555 extern void mdebug__push_time ();
556 extern void mdebug__pop_time ();
557 extern void mdebug__print_time ();
559 #define MDEBUG_PRINT0(FPRINTF) \
561 if (mdebug__flag & mdebug_mask) \
564 fflush (mdebug__output); \
568 #define MDEBUG_PRINT(msg) \
569 MDEBUG_PRINT0 (fprintf (mdebug__output, "%s", (msg)))
571 #define MDEBUG_PRINT1(fmt, arg) \
572 MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg)))
574 #define MDEBUG_PRINT2(fmt, arg1, arg2) \
575 MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg1), (arg2)))
577 #define MDEBUG_PRINT3(fmt, arg1, arg2, arg3) \
578 MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg1), (arg2), (arg3)))
580 #define MDEBUG_PRINT4(fmt, arg1, arg2, arg3, arg4) \
581 MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg1), (arg2), (arg3), (arg4)))
583 #define MDEBUG_PRINT5(fmt, arg1, arg2, arg3, arg4, arg5) \
584 MDEBUG_PRINT0 (fprintf (mdebug__output, (fmt), (arg1), (arg2), (arg3), (arg4), (arg5)))
586 #define MDEBUG_DUMP(prefix, postfix, call) \
588 if (mdebug__flag & mdebug_mask) \
590 fprintf (mdebug__output, "%s", prefix); \
592 fprintf (mdebug__output, "%s", postfix); \
593 fflush (mdebug__output); \
597 #define MDEBUG_PUSH_TIME() \
599 if (mdebug__flag & mdebug_mask) \
600 mdebug__push_time (); \
604 #define MDEBUG_POP_TIME() \
606 if (mdebug__flag & mdebug_mask) \
607 mdebug__pop_time (); \
611 #define MDEBUG_PRINT_TIME(tag, ARG_LIST) \
613 if (mdebug__flag & mdebug_mask) \
615 fprintf (mdebug__output, " [%s] ", tag); \
616 mdebug__print_time (); \
618 fprintf (mdebug__output, "\n"); \
623 #define SWAP_16(c) (((c) >> 8) | (((c) & 0xFF) << 8))
626 (((c) >> 24) | (((c) >> 8) & 0xFF00) \
627 | (((c) & 0xFF00) << 8) | (((c) & 0xFF) << 24))
630 extern void *(*mdatabase__finder) (MSymbol tag1, MSymbol tag2,
631 MSymbol tag3, MSymbol tag4);
632 extern void *(*mdatabase__loader) (void *);
634 /* Initialize/finalize function. */
636 extern int msymbol__init ();
637 extern void msymbol__fini ();
639 extern int mplist__init ();
640 extern void mplist__fini ();
642 extern int mtext__init ();
643 extern void mtext__fini ();
645 extern int mtext__prop_init ();
646 extern void mtext__prop_fini ();
648 extern int mchartable__init ();
649 extern void mchartable__fini ();
651 extern int mcharset__init ();
652 extern void mcharset__fini ();
654 extern int mcoding__init ();
655 extern void mcoding__fini ();
657 extern int mdatabase__init (void);
658 extern void mdatabase__fini (void);
660 extern int mchar__init ();
661 extern void mchar__fini ();
663 extern int mlang__init ();
664 extern void mlang__fini ();
666 extern int mlocale__init ();
667 extern void mlocale__fini ();
669 extern int minput__init ();
670 extern void minput__fini ();
672 #endif /* _M17N_INTERNAL_H_ */