~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_plugin.cc

  • Committer: Monty Taylor
  • Date: 2008-12-06 07:22:02 UTC
  • mto: (656.1.7 devel) (660.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 665.
  • Revision ID: monty@inaugust.com-20081206072202-2g25o9doqr1l8euu
OOOh doggie. Got rid of my_alloca.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <drizzled/session.h>
33
33
 
34
34
#include <string>
 
35
#include <vector>
35
36
 
36
37
#include <drizzled/error.h>
37
38
#include <drizzled/gettext.h>
718
719
 
719
720
static void reap_plugins(void)
720
721
{
721
 
  uint32_t count, idx;
722
 
  struct st_plugin_int *plugin, **reap, **list;
 
722
  size_t count;
 
723
  uint32_t idx;
 
724
  struct st_plugin_int *plugin;
 
725
  vector<st_plugin_int *> reap;
 
726
  vector<st_plugin_int *>::reverse_iterator riter;
723
727
 
724
728
  if (!reap_needed)
725
729
    return;
726
730
 
 
731
  
727
732
  reap_needed= false;
728
733
  count= plugin_array.elements;
729
 
  reap= (struct st_plugin_int **)my_alloca(sizeof(plugin)*(count+1));
730
 
  *(reap++)= NULL;
 
734
  reap.reserve(count);
731
735
 
732
736
  for (idx= 0; idx < count; idx++)
733
737
  {
736
740
    {
737
741
      /* change the status flag to prevent reaping by another thread */
738
742
      plugin->state= PLUGIN_IS_DYING;
739
 
      *(reap++)= plugin;
 
743
      reap.push_back(plugin);
740
744
    }
741
745
  }
742
746
 
743
 
  list= reap;
744
 
  while ((plugin= *(--list)))
 
747
  for (riter= reap.rbegin(); plugin= *riter, riter != reap.rend(); riter++)
745
748
    plugin_deinitialize(plugin, true);
746
749
 
747
 
  while ((plugin= *(--reap)))
 
750
  for (riter= reap.rbegin(); plugin= *riter, riter != reap.rend(); riter++)
748
751
    plugin_del(plugin);
749
752
 
750
 
  my_afree(reap);
751
753
}
752
754
 
753
755
static void intern_plugin_unlock(LEX *lex, plugin_ref plugin)
904
906
*/
905
907
int plugin_init(int *argc, char **argv, int flags)
906
908
{
907
 
  uint32_t i;
 
909
  uint32_t idx;
908
910
  struct st_mysql_plugin **builtins;
909
911
  struct st_mysql_plugin *plugin;
910
 
  struct st_plugin_int tmp, *plugin_ptr, **reap;
 
912
  struct st_plugin_int tmp, *plugin_ptr;
 
913
  vector<st_plugin_int *> reap;
 
914
  vector<st_plugin_int *>::reverse_iterator riter;
911
915
  MEM_ROOT tmp_root;
912
916
 
913
917
  if (initialized)
927
931
                            sizeof(struct st_plugin_int *),16,16))
928
932
    goto err;
929
933
 
930
 
  for (i= 0; i < DRIZZLE_MAX_PLUGIN_TYPE_NUM; i++)
 
934
  for (idx= 0; idx < DRIZZLE_MAX_PLUGIN_TYPE_NUM; idx++)
931
935
  {
932
 
    if (hash_init(&plugin_hash[i], system_charset_info, 16, 0, 0,
 
936
    if (hash_init(&plugin_hash[idx], system_charset_info, 16, 0, 0,
933
937
                  get_plugin_hash_key, NULL, HASH_UNIQUE))
934
938
      goto err;
935
939
  }
988
992
  /*
989
993
    Now we initialize all remaining plugins
990
994
  */
991
 
 
992
 
  reap= (st_plugin_int **) my_alloca((plugin_array.elements+1) * sizeof(void*));
993
 
  *(reap++)= NULL;
994
 
 
995
 
  for (i= 0; i < plugin_array.elements; i++)
 
995
  reap.reserve(plugin_array.elements);
 
996
 
 
997
  for (idx= 0; idx < plugin_array.elements; idx++)
996
998
  {
997
 
    plugin_ptr= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
 
999
    plugin_ptr= *dynamic_element(&plugin_array, idx, struct st_plugin_int **);
998
1000
    if (plugin_ptr->state == PLUGIN_IS_UNINITIALIZED)
999
1001
    {
1000
1002
      if (plugin_initialize(plugin_ptr))
1001
1003
      {
1002
1004
        plugin_ptr->state= PLUGIN_IS_DYING;
1003
 
        *(reap++)= plugin_ptr;
 
1005
        reap[idx]= plugin_ptr;
1004
1006
      }
1005
1007
    }
1006
1008
  }
1008
1010
  /*
1009
1011
    Check if any plugins have to be reaped
1010
1012
  */
1011
 
  while ((plugin_ptr= *(--reap)))
 
1013
  for(riter= reap.rbegin();
 
1014
      plugin_ptr= *riter, riter != reap.rend();
 
1015
      riter++)
1012
1016
  {
1013
1017
    plugin_deinitialize(plugin_ptr, true);
1014
1018
    plugin_del(plugin_ptr);
1015
1019
  }
1016
1020
 
1017
 
  my_afree(reap);
1018
 
 
1019
1021
end:
1020
1022
  free_root(&tmp_root, MYF(0));
1021
1023
 
1135
1137
 
1136
1138
void plugin_shutdown(void)
1137
1139
{
1138
 
  uint32_t i, count= plugin_array.elements, free_slots= 0;
1139
 
  struct st_plugin_int **plugins, *plugin;
1140
 
  struct st_plugin_dl **dl;
 
1140
  uint32_t idx, free_slots= 0; 
 
1141
  size_t count= plugin_array.elements;
 
1142
  struct st_plugin_int *plugin;
 
1143
  vector<st_plugin_int *> plugins;
 
1144
  vector<st_plugin_dl *> dl;
1141
1145
 
1142
1146
  if (initialized)
1143
1147
  {
1153
1157
    while (reap_needed && (count= plugin_array.elements))
1154
1158
    {
1155
1159
      reap_plugins();
1156
 
      for (i= free_slots= 0; i < count; i++)
 
1160
      for (idx= free_slots= 0; idx < count; idx++)
1157
1161
      {
1158
 
        plugin= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
 
1162
        plugin= *dynamic_element(&plugin_array, idx, struct st_plugin_int **);
1159
1163
        switch (plugin->state) {
1160
1164
        case PLUGIN_IS_READY:
1161
1165
          plugin->state= PLUGIN_IS_DELETED;
1178
1182
    }
1179
1183
 
1180
1184
    if (count > free_slots)
1181
 
      sql_print_warning(_("Forcing shutdown of %d plugins"),
1182
 
                        count - free_slots);
 
1185
      sql_print_warning(_("Forcing shutdown of %"PRIu64" plugins"),
 
1186
                        (uint64_t)count - free_slots);
1183
1187
 
1184
 
    plugins= (struct st_plugin_int **) my_alloca(sizeof(void*) * (count+1));
 
1188
    plugins.reserve(count);
1185
1189
 
1186
1190
    /*
1187
1191
      If we have any plugins which did not die cleanly, we force shutdown
1188
1192
    */
1189
 
    for (i= 0; i < count; i++)
 
1193
    for (idx= 0; idx < count; idx++)
1190
1194
    {
1191
 
      plugins[i]= *dynamic_element(&plugin_array, i, struct st_plugin_int **);
 
1195
      plugins.push_back(*dynamic_element(&plugin_array, idx,
 
1196
                                         struct st_plugin_int **));
1192
1197
      /* change the state to ensure no reaping races */
1193
 
      if (plugins[i]->state == PLUGIN_IS_DELETED)
1194
 
        plugins[i]->state= PLUGIN_IS_DYING;
 
1198
      if (plugins[idx]->state == PLUGIN_IS_DELETED)
 
1199
        plugins[idx]->state= PLUGIN_IS_DYING;
1195
1200
    }
1196
1201
 
1197
1202
    /*
1198
1203
      We loop through all plugins and call deinit() if they have one.
1199
1204
    */
1200
 
    for (i= 0; i < count; i++)
1201
 
      if (!(plugins[i]->state & (PLUGIN_IS_UNINITIALIZED | PLUGIN_IS_FREED)))
 
1205
    for (idx= 0; idx < count; idx++)
 
1206
      if (!(plugins[idx]->state & (PLUGIN_IS_UNINITIALIZED | PLUGIN_IS_FREED)))
1202
1207
      {
1203
1208
        sql_print_information(_("Plugin '%s' will be forced to shutdown"),
1204
 
                              plugins[i]->name.str);
 
1209
                              plugins[idx]->name.str);
1205
1210
        /*
1206
1211
          We are forcing deinit on plugins so we don't want to do a ref_count
1207
1212
          check until we have processed all the plugins.
1208
1213
        */
1209
 
        plugin_deinitialize(plugins[i], false);
 
1214
        plugin_deinitialize(plugins[idx], false);
1210
1215
      }
1211
1216
 
1212
1217
    /*
1213
1218
      We defer checking ref_counts until after all plugins are deinitialized
1214
1219
      as some may have worker threads holding on to plugin references.
1215
1220
    */
1216
 
    for (i= 0; i < count; i++)
 
1221
    for (idx= 0; idx < count; idx++)
1217
1222
    {
1218
 
      if (plugins[i]->ref_count)
 
1223
      if (plugins[idx]->ref_count)
1219
1224
        sql_print_error(_("Plugin '%s' has ref_count=%d after shutdown."),
1220
 
                        plugins[i]->name.str, plugins[i]->ref_count);
1221
 
      if (plugins[i]->state & PLUGIN_IS_UNINITIALIZED)
1222
 
        plugin_del(plugins[i]);
 
1225
                        plugins[idx]->name.str, plugins[idx]->ref_count);
 
1226
      if (plugins[idx]->state & PLUGIN_IS_UNINITIALIZED)
 
1227
        plugin_del(plugins[idx]);
1223
1228
    }
1224
1229
 
1225
1230
    /*
1231
1236
 
1232
1237
    initialized= 0;
1233
1238
 
1234
 
    my_afree(plugins);
1235
1239
  }
1236
1240
 
1237
1241
  /* Dispose of the memory */
1238
1242
 
1239
 
  for (i= 0; i < DRIZZLE_MAX_PLUGIN_TYPE_NUM; i++)
1240
 
    hash_free(&plugin_hash[i]);
 
1243
  for (idx= 0; idx < DRIZZLE_MAX_PLUGIN_TYPE_NUM; idx++)
 
1244
    hash_free(&plugin_hash[idx]);
1241
1245
  delete_dynamic(&plugin_array);
1242
1246
 
1243
1247
  count= plugin_dl_array.elements;
1244
 
  dl= (struct st_plugin_dl **)my_alloca(sizeof(void*) * count);
1245
 
  for (i= 0; i < count; i++)
1246
 
    dl[i]= *dynamic_element(&plugin_dl_array, i, struct st_plugin_dl **);
1247
 
  for (i= 0; i < plugin_dl_array.elements; i++)
1248
 
    free_plugin_mem(dl[i]);
1249
 
  my_afree(dl);
 
1248
  dl.reserve(count);
 
1249
  for (idx= 0; idx < count; idx++)
 
1250
    dl.push_back(*dynamic_element(&plugin_dl_array, idx,
 
1251
                 struct st_plugin_dl **));
 
1252
  for (idx= 0; idx < count; idx++)
 
1253
    free_plugin_mem(dl[idx]);
1250
1254
  delete_dynamic(&plugin_dl_array);
1251
1255
 
1252
1256
  hash_free(&bookmark_hash);
1261
1265
bool plugin_foreach_with_mask(Session *session, plugin_foreach_func *func,
1262
1266
                       int type, uint32_t state_mask, void *arg)
1263
1267
{
1264
 
  uint32_t idx, total;
1265
 
  struct st_plugin_int *plugin, **plugins;
 
1268
  uint32_t idx;
 
1269
  size_t total;
 
1270
  struct st_plugin_int *plugin;
 
1271
  vector<st_plugin_int *> plugins;
1266
1272
  int version=plugin_array_version;
1267
1273
 
1268
1274
  if (!initialized)
1272
1278
 
1273
1279
  total= type == DRIZZLE_ANY_PLUGIN ? plugin_array.elements
1274
1280
                                  : plugin_hash[type].records;
1275
 
  /*
1276
 
    Do the alloca out here in case we do have a working alloca:
1277
 
        leaving the nested stack frame invalidates alloca allocation.
1278
 
  */
1279
 
  plugins=(struct st_plugin_int **)my_alloca(total*sizeof(plugin));
 
1281
  plugins.reserve(total);
 
1282
 
1280
1283
  if (type == DRIZZLE_ANY_PLUGIN)
1281
1284
  {
1282
1285
    for (idx= 0; idx < total; idx++)
1283
1286
    {
1284
1287
      plugin= *dynamic_element(&plugin_array, idx, struct st_plugin_int **);
1285
 
      plugins[idx]= !(plugin->state & state_mask) ? plugin : NULL;
 
1288
      plugins.push_back(!(plugin->state & state_mask) ? plugin : NULL);
1286
1289
    }
1287
1290
  }
1288
1291
  else
1291
1294
    for (idx= 0; idx < total; idx++)
1292
1295
    {
1293
1296
      plugin= (struct st_plugin_int *) hash_element(hash, idx);
1294
 
      plugins[idx]= !(plugin->state & state_mask) ? plugin : NULL;
 
1297
      plugins.push_back(!(plugin->state & state_mask) ? plugin : NULL);
1295
1298
    }
1296
1299
  }
1297
1300
  for (idx= 0; idx < total; idx++)
1308
1311
        goto err;
1309
1312
  }
1310
1313
 
1311
 
  my_afree(plugins);
1312
 
  return(false);
 
1314
  return false;
1313
1315
err:
1314
 
  my_afree(plugins);
1315
 
  return(true);
 
1316
  return true;
1316
1317
}
1317
1318
 
1318
1319
 
1667
1668
  if (plugin)
1668
1669
    pluginlen= strlen(plugin) + 1;
1669
1670
  length= namelen + pluginlen + 2;
1670
 
  varname= (char*) my_alloca(length);
 
1671
  varname= (char*) malloc(length);
1671
1672
 
1672
1673
  if (plugin)
1673
1674
  {
1684
1685
  result= (st_bookmark*) hash_search(&bookmark_hash,
1685
1686
                                     (const unsigned char*) varname, length - 1);
1686
1687
 
1687
 
  my_afree(varname);
 
1688
  free(varname);
1688
1689
  return result;
1689
1690
}
1690
1691
 
1727
1728
    return NULL;
1728
1729
  };
1729
1730
 
1730
 
  varname= ((char*) my_alloca(length));
 
1731
  varname= ((char*) malloc(length));
1731
1732
  strxmov(varname + 1, plugin, "_", name, NULL);
1732
1733
  for (p= varname + 1; *p; p++)
1733
1734
    if (*p == '-')
1788
1789
      assert(0);
1789
1790
    }
1790
1791
  }
1791
 
  my_afree(varname);
 
1792
  free(varname);
1792
1793
  return result;
1793
1794
}
1794
1795