1 /* Copyright (c) 2000, 2011, 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 "heapdef.h"
26 #include "my_sys.h"
27
28 /*
29 Open heap table based on HP_SHARE structure
30
31 NOTE
32 This doesn't register the table in the open table list.
33 */
34
heap_open_from_share(HP_SHARE * share,int mode)35 HP_INFO *heap_open_from_share(HP_SHARE *share, int mode)
36 {
37 HP_INFO *info;
38 DBUG_ENTER("heap_open_from_share");
39
40 if (!(info= (HP_INFO*) my_malloc((uint) sizeof(HP_INFO) +
41 2 * share->max_key_length,
42 MYF(MY_ZEROFILL))))
43 {
44 DBUG_RETURN(0);
45 }
46 share->open_count++;
47 thr_lock_data_init(&share->lock,&info->lock,NULL);
48 info->s= share;
49 info->lastkey= (uchar*) (info + 1);
50 info->recbuf= (uchar*) (info->lastkey + share->max_key_length);
51 info->mode= mode;
52 info->current_record= (ulong) ~0L; /* No current record */
53 info->lastinx= info->errkey= -1;
54 #ifndef DBUG_OFF
55 info->opt_flag= READ_CHECK_USED; /* Check when changing */
56 #endif
57 DBUG_PRINT("exit",("heap: 0x%lx chunk_length: %d records_in_block: %d",
58 (long) info, share->recordspace.chunk_length,
59 share->recordspace.block.records_in_block));
60 DBUG_RETURN(info);
61 }
62
63
64 /*
65 Open heap table based on HP_SHARE structure and register it
66 */
67
heap_open_from_share_and_register(HP_SHARE * share,int mode)68 HP_INFO *heap_open_from_share_and_register(HP_SHARE *share, int mode)
69 {
70 HP_INFO *info;
71 DBUG_ENTER("heap_open_from_share_and_register");
72
73 mysql_mutex_lock(&THR_LOCK_heap);
74 if ((info= heap_open_from_share(share, mode)))
75 {
76 info->open_list.data= (void*) info;
77 heap_open_list= list_add(heap_open_list,&info->open_list);
78 /* Unpin the share, it is now pinned by the file. */
79 share->open_count--;
80 }
81 mysql_mutex_unlock(&THR_LOCK_heap);
82 DBUG_RETURN(info);
83 }
84
85
86 /**
87 Dereference a HEAP share and free it if it's not referenced.
88 We don't check open_count for internal tables since they
89 are always thread-local, i.e. referenced by a single thread.
90 */
heap_release_share(HP_SHARE * share,my_bool internal_table)91 void heap_release_share(HP_SHARE *share, my_bool internal_table)
92 {
93 /* Couldn't open table; Remove the newly created table */
94 if (internal_table)
95 hp_free(share);
96 else
97 {
98 mysql_mutex_lock(&THR_LOCK_heap);
99 if (--share->open_count == 0)
100 hp_free(share);
101 mysql_mutex_unlock(&THR_LOCK_heap);
102 }
103 }
104
105 /*
106 Open heap table based on name
107
108 NOTE
109 This register the table in the open table list. so that it can be
110 found by future heap_open() calls.
111 */
112
heap_open(const char * name,int mode)113 HP_INFO *heap_open(const char *name, int mode)
114 {
115 HP_INFO *info;
116 HP_SHARE *share;
117 DBUG_ENTER("heap_open");
118
119 mysql_mutex_lock(&THR_LOCK_heap);
120 if (!(share= hp_find_named_heap(name)))
121 {
122 my_errno= ENOENT;
123 mysql_mutex_unlock(&THR_LOCK_heap);
124 DBUG_RETURN(0);
125 }
126 if ((info= heap_open_from_share(share, mode)))
127 {
128 info->open_list.data= (void*) info;
129 heap_open_list= list_add(heap_open_list,&info->open_list);
130 }
131 mysql_mutex_unlock(&THR_LOCK_heap);
132 DBUG_RETURN(info);
133 }
134
135
136 /* map name to a heap-nr. If name isn't found return 0 */
137
hp_find_named_heap(const char * name)138 HP_SHARE *hp_find_named_heap(const char *name)
139 {
140 LIST *pos;
141 HP_SHARE *info;
142 DBUG_ENTER("heap_find");
143 DBUG_PRINT("enter",("name: %s",name));
144
145 for (pos= heap_share_list; pos; pos= pos->next)
146 {
147 info= (HP_SHARE*) pos->data;
148 if (!strcmp(name, info->name))
149 {
150 DBUG_PRINT("exit", ("Old heap_database: 0x%lx", (long) info));
151 DBUG_RETURN(info);
152 }
153 }
154 DBUG_RETURN((HP_SHARE *) 0);
155 }
156
157
158