natCore.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // natCore -- C++ side of Core
  2. /* Copyright (C) 2001, 2002, 2003, 2005, 2006 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. /* Author: Anthony Green <green@redhat.com>. */
  8. #include <config.h>
  9. #include <gcj/cni.h>
  10. #include <jvm.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <java/lang/NullPointerException.h>
  14. #include <java/io/IOException.h>
  15. #include <gnu/gcj/Core.h>
  16. // List of global core values.
  17. static _Jv_core_chain *root;
  18. static void
  19. default_register_resource (_Jv_core_chain *node)
  20. {
  21. node->next = root;
  22. root = node;
  23. }
  24. // This is set only when a lock is held on java.lang.Class.
  25. // This function is called to handle a new core node.
  26. void (*_Jv_RegisterCoreHook) (_Jv_core_chain *) = default_register_resource;
  27. void
  28. _Jv_RegisterResource (void *vptr)
  29. {
  30. char *rptr = (char *) vptr;
  31. _Jv_core_chain *cc = (_Jv_core_chain *) _Jv_Malloc (sizeof (_Jv_core_chain));
  32. cc->name_length = ((int *)rptr)[0];
  33. cc->data_length = ((int *)rptr)[1];
  34. cc->name = rptr + 2 * sizeof (int);
  35. cc->data = cc->name + cc->name_length;
  36. cc->next = NULL;
  37. (*_Jv_RegisterCoreHook) (cc);
  38. }
  39. void
  40. _Jv_FreeCoreChain (_Jv_core_chain *chain)
  41. {
  42. while (chain != NULL)
  43. {
  44. _Jv_core_chain *next = chain->next;
  45. _Jv_Free (chain);
  46. chain = next;
  47. }
  48. }
  49. _Jv_core_chain *
  50. _Jv_FindCore (_Jv_core_chain *node, jstring name)
  51. {
  52. char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (name) + 1);
  53. jsize total = JvGetStringUTFRegion (name, 0, name->length(), buf);
  54. buf[total] = '\0';
  55. // Usually requests here end up as an absolute URL. We strip the
  56. // initial `/'.
  57. if (buf[0] == '/')
  58. {
  59. ++buf;
  60. --total;
  61. }
  62. while (node)
  63. {
  64. if (total == node->name_length
  65. && strncmp (buf, node->name, total) == 0)
  66. return node;
  67. node = node->next;
  68. }
  69. return NULL;
  70. }
  71. gnu::gcj::Core *
  72. _Jv_create_core (_Jv_core_chain *node, jstring name)
  73. {
  74. node = _Jv_FindCore (node, name);
  75. gnu::gcj::Core *core = NULL;
  76. if (node)
  77. {
  78. core = new gnu::gcj::Core ();
  79. core->ptr = (gnu::gcj::RawData *) node->data;
  80. core->length = node->data_length;
  81. }
  82. return core;
  83. }
  84. gnu::gcj::Core *
  85. gnu::gcj::Core::find (jstring name)
  86. {
  87. gnu::gcj::Core *core = _Jv_create_core (root, name);
  88. return core;
  89. }
  90. gnu::gcj::Core *
  91. gnu::gcj::Core::create (jstring name)
  92. {
  93. gnu::gcj::Core *core = _Jv_create_core (root, name);
  94. if (core == NULL)
  95. throw new ::java::io::IOException (JvNewStringLatin1 ("can't open core"));
  96. return core;
  97. }