(mfont_list): If FONT is null, use a temporary font.
[m17n/m17n-lib.git] / src / internal.h
index 13f2b49..8bb2318 100644 (file)
     longer version of internal.h description 
 */
 
+extern int m17n__core_initialized;
+extern int m17n__shell_initialized;
+extern int m17n__gui_initialized;
+
 extern int mdebug_hook ();
 
 /** Return with code RET while setting merror_code to ERR.  */
@@ -56,6 +60,11 @@ extern int mdebug_hook ();
     goto warning;      \
   } while (0)
 
+#define MFATAL(err)    \
+  do {                 \
+    mdebug_hook ();    \
+    exit (err);                \
+  } while (0)
 
 #define M_CHECK_CHAR(c, ret)           \
   if ((c) < 0 || (c) > MCHAR_MAX)      \
@@ -145,6 +154,42 @@ extern int mdebug_hook ();
 
 #define MSTRUCT_CALLOC(p, err) MTABLE_CALLOC ((p), 1, (err))
 
+#define USE_SAFE_ALLOCA \
+  int sa_must_free = 0, sa_size = 0
+
+/* P must be the same in all calls to SAFE_ALLOCA and SAFE_FREE in a
+   function.  */
+
+#define SAFE_ALLOCA(P, SIZE)           \
+  do {                                 \
+    if (sa_size < (SIZE))              \
+      {                                        \
+       if (sa_must_free)               \
+         (P) = realloc ((P), (SIZE));  \
+       else                            \
+         {                             \
+           (P) = alloca ((SIZE));      \
+           if (! (P))                  \
+             {                         \
+               (P) = malloc (SIZE);    \
+               sa_must_free = 1;       \
+             }                         \
+         }                             \
+       if (! (P))                      \
+         MEMORY_FULL (1);              \
+       sa_size = (SIZE);               \
+      }                                        \
+  } while (0)
+
+#define SAFE_FREE(P)                   \
+  do {                                 \
+    if (sa_must_free && sa_size > 0)   \
+      {                                        \
+       free ((P));                     \
+       sa_must_free = sa_size = 0;     \
+      }                                        \
+  } while (0)
+
 \f
 /** Extendable array.  */
 
@@ -241,19 +286,23 @@ typedef struct
 typedef struct
 {
   /**en Reference count of the object.  */
+  /**ja ¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô.  */
   unsigned ref_count : 16;
 
   unsigned ref_count_extended : 1;
 
   /**en A flag bit used for various perpose.  */
+  /**ja ¤µ¤Þ¤¶¤Þ¤ÊÌÜŪ¤ËÍѤ¤¤é¤ì¤ë¥Õ¥é¥°¥Ó¥Ã¥È.  */
   unsigned flag : 15;
 
   union {
     /**en If <ref_count_extended> is zero, a function to free the
        object.  */
+    /**ja <ref_count_extended> ¤¬ 0 ¤Ê¤é¤Ð¥ª¥Ö¥¸¥§¥¯¥È¤ò²òÊü¤¹¤ë´Ø¿ô.  */
     void (*freer) (void *);
     /**en If <ref_count_extended> is nonzero, a pointer to the
        struct M17NObjectRecord.  */
+    /**ja <ref_count_extended> ¤¬ 0 ¤Ç¤Ê¤±¤ì¤Ð¹½Â¤ÂΠM17NObjectRecord ¤Ø¤Î¥Ý¥¤¥ó¥¿.  */
     M17NObjectRecord *record;
   } u;
 } M17NObject;
@@ -271,6 +320,7 @@ typedef struct
 
 /**en Increment the reference count of OBJECT if the count is not
    0.  */
+/**ja OBJECT ¤Î»²¾È¿ô¤¬ 0 ¤Ç¤Ê¤±¤ì¤Ð 1 Áý¤ä¤¹.  */
 
 #define M17N_OBJECT_REF(object)                                \
   do {                                                 \
@@ -311,35 +361,50 @@ typedef struct
 
 
 /**en Decrement the reference count of OBJECT if the count is greater
-    than 0.  In that case, if the count becomes 0, free OBJECT.  */
+      than 0.  In that case, if the count becomes 0, free OBJECT.  */
+/**ja OBJECT ¤Î»²¾È¿ô¤¬ 0 ¤è¤êÂ礭¤±¤ì¤Ð 1 ¸º¤é¤¹¡£¸º¤é¤·¤Æ 0 ¤Ë¤Ê¤ì¤Ð
+      OBJECT ¤ò²òÊü¤¹¤ë.  */
 
-#define M17N_OBJECT_UNREF(object)                              \
-  do {                                                         \
-    if (object)                                                        \
-      {                                                                \
-       if (((M17NObject *) (object))->ref_count_extended)      \
-         m17n_object_unref (object);                           \
-       else if (((M17NObject *) (object))->ref_count == 0)     \
-         break;                                                \
-       else if (((M17NObject *) (object))->ref_count > 1)      \
-         ((M17NObject *) (object))->ref_count--;               \
-       else                                                    \
-         {                                                     \
-           if (((M17NObject *) (object))->u.freer)             \
-             (((M17NObject *) (object))->u.freer) (object);    \
-           else                                                \
-             free (object);                                    \
-         }                                                     \
-      }                                                                \
+#define M17N_OBJECT_UNREF(object)                                      \
+  do {                                                                 \
+    if (object)                                                                \
+      {                                                                        \
+       if (((M17NObject *) (object))->ref_count_extended)              \
+         m17n_object_unref (object);                                   \
+       else if (((M17NObject *) (object))->ref_count == 0)             \
+         break;                                                        \
+       else                                                            \
+         {                                                             \
+           ((M17NObject *) (object))->ref_count--;                     \
+           if (((M17NObject *) (object))->ref_count == 0)              \
+             {                                                         \
+               if (((M17NObject *) (object))->u.freer)                 \
+                 (((M17NObject *) (object))->u.freer) (object);        \
+               else                                                    \
+                 free (object);                                        \
+               (object) = NULL;                                        \
+             }                                                         \
+         }                                                             \
+      }                                                                        \
   } while (0)
 
+typedef struct _M17NObjectArray M17NObjectArray;
 
-typedef struct
+struct _M17NObjectArray
 {
+  char *name;
   int count;
   int size, inc, used;
   void **objects;
-} M17NObjectArray;
+  M17NObjectArray *next;
+};
+
+extern void mdebug__add_object_array (M17NObjectArray *array, char *name);
+
+#define M17N_OBJECT_ADD_ARRAY(array, name)     \
+  if (mdebug__flag & MDEBUG_FINI)              \
+    mdebug__add_object_array (&array, name);   \
+  else
 
 extern void mdebug__register_object (M17NObjectArray *array, void *object);
 
@@ -355,8 +420,6 @@ extern void mdebug__unregister_object (M17NObjectArray *array, void *object);
     mdebug__unregister_object (&array, object);        \
   else
 
-extern void mdebug__report_object (char *name, M17NObjectArray *array);
-
 \f
 
 struct MTextPlist;