Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 2010 LunarG Inc.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22.  * DEALINGS IN THE SOFTWARE.
  23.  *
  24.  * Authors:
  25.  *    Chia-I Wu <olv@lunarg.com>
  26.  */
  27.  
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <assert.h>
  31.  
  32. #include "u_current.h"
  33. #include "u_thread.h"
  34. #include "entry.h"
  35. #include "stub.h"
  36. #include "table.h"
  37.  
  38. #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
  39.  
  40. struct mapi_stub {
  41.    const void *name;
  42.    int slot;
  43.    mapi_func addr;
  44. };
  45.  
  46. /* define public_string_pool and public_stubs */
  47. #define MAPI_TMP_PUBLIC_STUBS
  48. #include "mapi_tmp.h"
  49.  
  50. static struct mapi_stub dynamic_stubs[MAPI_TABLE_NUM_DYNAMIC];
  51. static int num_dynamic_stubs;
  52. static int next_dynamic_slot = MAPI_TABLE_NUM_STATIC;
  53.  
  54. void
  55. stub_init_once(void)
  56. {
  57. #ifdef HAVE_PTHREAD
  58.    static pthread_once_t once = PTHREAD_ONCE_INIT;
  59.    pthread_once(&once, entry_patch_public);
  60. #else
  61.    static int first = 1;
  62.    if (first) {
  63.       first = 0;
  64.       entry_patch_public();
  65.    }
  66. #endif
  67. }
  68.  
  69. static int
  70. stub_compare(const void *key, const void *elem)
  71. {
  72.    const char *name = (const char *) key;
  73.    const struct mapi_stub *stub = (const struct mapi_stub *) elem;
  74.    const char *stub_name;
  75.  
  76.    stub_name = &public_string_pool[(unsigned long) stub->name];
  77.  
  78.    return strcmp(name, stub_name);
  79. }
  80.  
  81. /**
  82.  * Return the public stub with the given name.
  83.  */
  84. const struct mapi_stub *
  85. stub_find_public(const char *name)
  86. {
  87.    return (const struct mapi_stub *) bsearch(name, public_stubs,
  88.          ARRAY_SIZE(public_stubs), sizeof(public_stubs[0]), stub_compare);
  89. }
  90.  
  91. /**
  92.  * Add a dynamic stub.
  93.  */
  94. static struct mapi_stub *
  95. stub_add_dynamic(const char *name)
  96. {
  97.    struct mapi_stub *stub;
  98.    int idx;
  99.  
  100.    idx = num_dynamic_stubs;
  101.    /* minus 1 to make sure we can never reach the last slot */
  102.    if (idx >= MAPI_TABLE_NUM_DYNAMIC - 1)
  103.       return NULL;
  104.  
  105.    stub = &dynamic_stubs[idx];
  106.  
  107.    /* dispatch to the last slot, which is reserved for no-op */
  108.    stub->addr = entry_generate(
  109.          MAPI_TABLE_NUM_STATIC + MAPI_TABLE_NUM_DYNAMIC - 1);
  110.    if (!stub->addr)
  111.       return NULL;
  112.  
  113.    stub->name = (const void *) name;
  114.    /* to be fixed later */
  115.    stub->slot = -1;
  116.  
  117.    num_dynamic_stubs = idx + 1;
  118.  
  119.    return stub;
  120. }
  121.  
  122. /**
  123.  * Return the dynamic stub with the given name.  If no such stub exists and
  124.  * generate is true, a new stub is generated.
  125.  */
  126. struct mapi_stub *
  127. stub_find_dynamic(const char *name, int generate)
  128. {
  129.    u_mutex_declare_static(dynamic_mutex);
  130.    struct mapi_stub *stub = NULL;
  131.    int count, i;
  132.    
  133.    u_mutex_lock(dynamic_mutex);
  134.  
  135.    if (generate)
  136.       assert(!stub_find_public(name));
  137.  
  138.    count = num_dynamic_stubs;
  139.    for (i = 0; i < count; i++) {
  140.       if (strcmp(name, (const char *) dynamic_stubs[i].name) == 0) {
  141.          stub = &dynamic_stubs[i];
  142.          break;
  143.       }
  144.    }
  145.  
  146.    /* generate a dynamic stub */
  147.    if (generate && !stub)
  148.          stub = stub_add_dynamic(name);
  149.  
  150.    u_mutex_unlock(dynamic_mutex);
  151.  
  152.    return stub;
  153. }
  154.  
  155. static const struct mapi_stub *
  156. search_table_by_slot(const struct mapi_stub *table, size_t num_entries,
  157.                      int slot)
  158. {
  159.    size_t i;
  160.    for (i = 0; i < num_entries; ++i) {
  161.       if (table[i].slot == slot)
  162.          return &table[i];
  163.    }
  164.    return NULL;
  165. }
  166.  
  167. const struct mapi_stub *
  168. stub_find_by_slot(int slot)
  169. {
  170.    const struct mapi_stub *stub =
  171.       search_table_by_slot(public_stubs, ARRAY_SIZE(public_stubs), slot);
  172.    if (stub)
  173.       return stub;
  174.    return search_table_by_slot(dynamic_stubs, num_dynamic_stubs, slot);
  175. }
  176.  
  177. void
  178. stub_fix_dynamic(struct mapi_stub *stub, const struct mapi_stub *alias)
  179. {
  180.    int slot;
  181.  
  182.    if (stub->slot >= 0)
  183.       return;
  184.  
  185.    if (alias)
  186.       slot = alias->slot;
  187.    else
  188.       slot = next_dynamic_slot++;
  189.  
  190.    entry_patch(stub->addr, slot);
  191.    stub->slot = slot;
  192. }
  193.  
  194. /**
  195.  * Return the name of a stub.
  196.  */
  197. const char *
  198. stub_get_name(const struct mapi_stub *stub)
  199. {
  200.    const char *name;
  201.  
  202.    if (stub >= public_stubs &&
  203.        stub < public_stubs + ARRAY_SIZE(public_stubs))
  204.       name = &public_string_pool[(unsigned long) stub->name];
  205.    else
  206.       name = (const char *) stub->name;
  207.  
  208.    return name;
  209. }
  210.  
  211. /**
  212.  * Return the slot of a stub.
  213.  */
  214. int
  215. stub_get_slot(const struct mapi_stub *stub)
  216. {
  217.    return stub->slot;
  218. }
  219.  
  220. /**
  221.  * Return the address of a stub.
  222.  */
  223. mapi_func
  224. stub_get_addr(const struct mapi_stub *stub)
  225. {
  226.    assert(stub->addr || (unsigned int) stub->slot < MAPI_TABLE_NUM_STATIC);
  227.    return (stub->addr) ? stub->addr : entry_get_public(stub->slot);
  228. }
  229.