1 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /* open a heap-database */
24 
25 #include <errno.h>
26 #include <sys/types.h>
27 
28 #include "my_dbug.h"
29 #include "my_inttypes.h"
30 #include "my_sys.h"
31 #include "mysql/service_mysql_alloc.h"
32 #include "storage/heap/heapdef.h"
33 
34 /*
35   Open heap table based on HP_SHARE structure
36 
37   NOTE
38     This doesn't register the table in the open table list.
39 */
40 
heap_open_from_share(HP_SHARE * share,int mode)41 HP_INFO *heap_open_from_share(HP_SHARE *share, int mode) {
42   HP_INFO *info;
43   DBUG_TRACE;
44 
45   if (!(info = (HP_INFO *)my_malloc(
46             hp_key_memory_HP_INFO,
47             (uint)sizeof(HP_INFO) + 2 * share->max_key_length,
48             MYF(MY_ZEROFILL)))) {
49     return nullptr;
50   }
51   share->open_count++;
52   /*
53     Don't initialize THR_LOCK_DATA for internal temporary tables as it
54     is not used for them anyway (and THR_LOCK is not initialized for them
55     too).
56   */
57   if (share->open_list.data != nullptr)
58     thr_lock_data_init(&share->lock, &info->lock, nullptr);
59   info->s = share;
60   info->lastkey = (uchar *)(info + 1);
61   info->recbuf = (uchar *)(info->lastkey + share->max_key_length);
62   info->mode = mode;
63   info->current_record = (ulong)~0L; /* No current record */
64   info->lastinx = info->errkey = -1;
65 #ifndef DBUG_OFF
66   info->opt_flag = READ_CHECK_USED; /* Check when changing */
67 #endif
68   DBUG_PRINT("exit", ("heap: %p  reclength: %d  records_in_block: %d", info,
69                       share->reclength, share->block.records_in_block));
70   return info;
71 }
72 
73 /*
74   Open heap table based on HP_SHARE structure and register it
75 */
76 
heap_open_from_share_and_register(HP_SHARE * share,int mode)77 HP_INFO *heap_open_from_share_and_register(HP_SHARE *share, int mode) {
78   HP_INFO *info;
79   DBUG_TRACE;
80 
81   mysql_mutex_lock(&THR_LOCK_heap);
82   if ((info = heap_open_from_share(share, mode))) {
83     info->open_list.data = (void *)info;
84     heap_open_list = list_add(heap_open_list, &info->open_list);
85     /* Unpin the share, it is now pinned by the file. */
86     share->open_count--;
87   }
88   mysql_mutex_unlock(&THR_LOCK_heap);
89   return info;
90 }
91 
92 /**
93   Dereference a HEAP share and free it if it's not referenced.
94   We needn't check open_count for single instances.
95 */
heap_release_share(HP_SHARE * share,bool single_instance)96 void heap_release_share(HP_SHARE *share, bool single_instance) {
97   /* Couldn't open table; Remove the newly created table */
98   if (single_instance)
99     hp_free(share);
100   else {
101     mysql_mutex_lock(&THR_LOCK_heap);
102     if (--share->open_count == 0) hp_free(share);
103     mysql_mutex_unlock(&THR_LOCK_heap);
104   }
105 }
106 
107 /*
108   Open heap table based on name
109 
110   NOTE
111     This register the table in the open table list. so that it can be
112     found by future heap_open() calls.
113 */
114 
heap_open(const char * name,int mode)115 HP_INFO *heap_open(const char *name, int mode) {
116   HP_INFO *info;
117   HP_SHARE *share;
118   DBUG_TRACE;
119 
120   mysql_mutex_lock(&THR_LOCK_heap);
121   if (!(share = hp_find_named_heap(name))) {
122     set_my_errno(ENOENT);
123     mysql_mutex_unlock(&THR_LOCK_heap);
124     return nullptr;
125   }
126   if ((info = heap_open_from_share(share, mode))) {
127     info->open_list.data = (void *)info;
128     heap_open_list = list_add(heap_open_list, &info->open_list);
129   }
130   mysql_mutex_unlock(&THR_LOCK_heap);
131   return info;
132 }
133 
134 /* map name to a heap-nr. If name isn't found return 0 */
135 
hp_find_named_heap(const char * name)136 HP_SHARE *hp_find_named_heap(const char *name) {
137   LIST *pos;
138   HP_SHARE *info;
139   DBUG_TRACE;
140   DBUG_PRINT("enter", ("name: %s", name));
141 
142   for (pos = heap_share_list; pos; pos = pos->next) {
143     info = (HP_SHARE *)pos->data;
144     if (!strcmp(name, info->name)) {
145       DBUG_PRINT("exit", ("Old heap_database: %p", info));
146       return info;
147     }
148   }
149   return (HP_SHARE *)nullptr;
150 }
151