2005-12-23 Dick Porter <dick@ximian.com>
[mono.git] / mono / metadata / image.c
index 1db273008c8d2d1ae7889118b664a970c122717f..6d412ea565c21d1835c1fe36b004b7fc320a1bc8 100644 (file)
@@ -36,7 +36,8 @@ static GHashTable *loaded_images_hash;
 static GHashTable *loaded_images_guid_hash;
 static GHashTable *loaded_images_refonly_hash;
 static GHashTable *loaded_images_refonly_guid_hash;
-
+#define mono_images_lock() EnterCriticalSection (&images_mutex)
+#define mono_images_unlock() LeaveCriticalSection (&images_mutex)
 static CRITICAL_SECTION images_mutex;
 
 guint32
@@ -318,7 +319,12 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
        if (strncmp (ptr, "BSJB", 4) == 0){
                guint32 version_string_len;
 
-               ptr += 12;
+               ptr += 4;
+               image->md_version_major = read16 (ptr);
+               ptr += 4;
+               image->md_version_minor = read16 (ptr);
+               ptr += 4;
+
                version_string_len = read32 (ptr);
                ptr += 4;
                image->version = g_strndup (ptr, version_string_len);
@@ -805,9 +811,9 @@ mono_image_loaded_full (const char *name, gboolean refonly)
        MonoImage *res;
        GHashTable *loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
         
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        res = g_hash_table_lookup (loaded_images, name);
-       LeaveCriticalSection (&images_mutex);
+       mono_images_unlock ();
        return res;
 }
 
@@ -823,9 +829,9 @@ mono_image_loaded_by_guid_full (const char *guid, gboolean refonly)
        MonoImage *res;
        GHashTable *loaded_images = refonly ? loaded_images_refonly_guid_hash : loaded_images_guid_hash;
 
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        res = g_hash_table_lookup (loaded_images, guid);
-       LeaveCriticalSection (&images_mutex);
+       mono_images_unlock ();
        return res;
 }
 
@@ -841,13 +847,13 @@ register_image (MonoImage *image)
        MonoImage *image2;
        GHashTable *loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
 
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        image2 = g_hash_table_lookup (loaded_images, image->name);
 
        if (image2) {
                /* Somebody else beat us to it */
                mono_image_addref (image2);
-               LeaveCriticalSection (&images_mutex);
+               mono_images_unlock ();
                mono_image_close (image);
                return image2;
        }
@@ -855,7 +861,7 @@ register_image (MonoImage *image)
        if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == NULL))
                g_hash_table_insert (loaded_images, (char *) image->assembly_name, image);      
        g_hash_table_insert (image->ref_only ? loaded_images_refonly_guid_hash : loaded_images_guid_hash, image->guid, image);
-       LeaveCriticalSection (&images_mutex);
+       mono_images_unlock ();
 
        return image;
 }
@@ -923,17 +929,17 @@ mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean r
         * happen outside the mutex, and if multiple threads happen to load
         * the same image, we discard all but the first copy.
         */
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        loaded_images = refonly ? loaded_images_refonly_hash : loaded_images_hash;
        image = g_hash_table_lookup (loaded_images, absfname);
        g_free (absfname);
        
        if (image){
                mono_image_addref (image);
-               LeaveCriticalSection (&images_mutex);
+               mono_images_unlock ();
                return image;
        }
-       LeaveCriticalSection (&images_mutex);
+       mono_images_unlock ();
 
        image = do_mono_image_open (fname, status, TRUE, refonly);
        if (image == NULL)
@@ -1036,7 +1042,7 @@ mono_image_close (MonoImage *image)
        if (InterlockedDecrement (&image->ref_count))
                return;
        
-       EnterCriticalSection (&images_mutex);
+       mono_images_lock ();
        loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
        loaded_images_guid = image->ref_only ? loaded_images_refonly_guid_hash : loaded_images_guid_hash;
        image2 = g_hash_table_lookup (loaded_images, image->name);
@@ -1049,7 +1055,7 @@ mono_image_close (MonoImage *image)
        }
        if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == image))
                g_hash_table_remove (loaded_images, (char *) image->assembly_name);     
-       LeaveCriticalSection (&images_mutex);
+       mono_images_unlock ();
 
        if (image->file_descr) {
                fclose (image->file_descr);
@@ -1252,6 +1258,16 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
        }
 }
 
+/**
+ * mono_image_lookup_resource:
+ * @image: the image to look up the resource in
+ * @res_id: A MONO_PE_RESOURCE_ID_ that represents the resource ID to lookup.
+ * @lang_id: The language id.
+ * @name: the resource name to lookup.
+ *
+ * Returns: NULL if not found, otherwise a pointer to the in-memory representation
+ * of the given resource.
+ */
 gpointer
 mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)
 {
@@ -1310,12 +1326,33 @@ mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, g
        return(NULL);
 }
 
+/** 
+ * mono_image_get_entry_point:
+ * @image: the image where the entry point will be looked up.
+ *
+ * Use this routine to determine the metadata token for method that
+ * has been flagged as the entry point.
+ *
+ * Returns: the token for the entry point method in the image
+ */
 guint32
 mono_image_get_entry_point (MonoImage *image)
 {
        return ((MonoCLIImageInfo*)image->image_info)->cli_cli_header.ch_entry_point;
 }
 
+/**
+ * mono_image_get_resource:
+ * @image: the image where the resource will be looked up.
+ * @offset: The offset to add to the resource
+ * @size: a pointer to an int where the size of the resource will be stored
+ *
+ * This is a low-level routine that fetches a resource from the
+ * metadata that starts at a given @offset.  The @size parameter is
+ * filled with the data field as encoded in the metadata.
+ *
+ * Returns: the pointer to the resource whose offset is @offset.
+ */
 const char*
 mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)
 {