2012-01-31 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libjava / gnu / gcj / natCore.cc
blob2d650f7101dbbe0c28d5cb7f3d7170971b520807
1 // natCore -- C++ side of Core
3 /* Copyright (C) 2001, 2002, 2003, 2005, 2006 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 /* Author: Anthony Green <green@redhat.com>. */
13 #include <config.h>
15 #include <gcj/cni.h>
16 #include <jvm.h>
17 #include <string.h>
18 #include <stdlib.h>
20 #include <java/lang/NullPointerException.h>
21 #include <java/io/IOException.h>
22 #include <gnu/gcj/Core.h>
24 // List of global core values.
25 static _Jv_core_chain *root;
27 static void
28 default_register_resource (_Jv_core_chain *node)
30 node->next = root;
31 root = node;
34 // This is set only when a lock is held on java.lang.Class.
35 // This function is called to handle a new core node.
36 void (*_Jv_RegisterCoreHook) (_Jv_core_chain *) = default_register_resource;
38 void
39 _Jv_RegisterResource (void *vptr)
41 char *rptr = (char *) vptr;
43 _Jv_core_chain *cc = (_Jv_core_chain *) _Jv_Malloc (sizeof (_Jv_core_chain));
45 cc->name_length = ((int *)rptr)[0];
46 cc->data_length = ((int *)rptr)[1];
47 cc->name = rptr + 2 * sizeof (int);
48 cc->data = cc->name + cc->name_length;
49 cc->next = NULL;
51 (*_Jv_RegisterCoreHook) (cc);
54 void
55 _Jv_FreeCoreChain (_Jv_core_chain *chain)
57 while (chain != NULL)
59 _Jv_core_chain *next = chain->next;
60 _Jv_Free (chain);
61 chain = next;
65 _Jv_core_chain *
66 _Jv_FindCore (_Jv_core_chain *node, jstring name)
68 char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (name) + 1);
69 jsize total = JvGetStringUTFRegion (name, 0, name->length(), buf);
70 buf[total] = '\0';
72 // Usually requests here end up as an absolute URL. We strip the
73 // initial `/'.
74 if (buf[0] == '/')
76 ++buf;
77 --total;
80 while (node)
82 if (total == node->name_length
83 && strncmp (buf, node->name, total) == 0)
84 return node;
85 node = node->next;
88 return NULL;
91 gnu::gcj::Core *
92 _Jv_create_core (_Jv_core_chain *node, jstring name)
94 node = _Jv_FindCore (node, name);
96 gnu::gcj::Core *core = NULL;
97 if (node)
99 core = new gnu::gcj::Core ();
100 core->ptr = (gnu::gcj::RawData *) node->data;
101 core->length = node->data_length;
103 return core;
106 gnu::gcj::Core *
107 gnu::gcj::Core::find (jstring name)
109 gnu::gcj::Core *core = _Jv_create_core (root, name);
110 return core;
113 gnu::gcj::Core *
114 gnu::gcj::Core::create (jstring name)
116 gnu::gcj::Core *core = _Jv_create_core (root, name);
117 if (core == NULL)
118 throw new ::java::io::IOException (JvNewStringLatin1 ("can't open core"));
119 return core;